to write htaccess code for friendly url - mod-rewrite

I need a help, does anyone can tell me how to change a url
"http://www.domain.com/search.php?key=+Ebooks&type=title&Submit=Search"
to
"http://www.domain.com/keyword- keyword- keyword.html".
i have written following htaccess code but its not working.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule search-(.*)-(.*)-(.*)\.html search.php?key=$1&type=$2&page=$3
</IfModule>

What exactly does not work? Is anything happening?
I am not 100% sure but I think you have to do it this way:
search-(.*?)-(.*?)-(.*?)\.html search.php?key=$1&type=$2&page=$3
Notice the ? behind the asterisks. It indicates to match a string as early as possible.

Try this:
RewriteRule search-([^-]*)-([^-]*)-([^-]*)\.html search.php?key=$1&type=$2&page=$3

Related

Despite adding QSA to mod_rewrite, $_GET still returning empty variable

UPDATED 2/1/13: This question is really holding me up from getting some work done, so if anyone else has a suggestion, It'd be much appreciated.
I'm trying to make domain.com/notalone/viewpost/5/ act as domain.com/notalone/viewpost.php?id=5
My current .htaccess is this:
RewriteEngine On
RewriteRule ^notalone/viewpost/([0-9]+)/?$ /viewpost.php?id=$1 [QSA]
Currently, the page loads properly when you visit /notalone/viewpost/5/ but it does not pass $_GET['id']
Things I've Tried:
Adding Options -Multiviews ---> Results in a 404
Using Rewritebase / ---> No effect
RewriteRule ^viewpost/([0-9]+)/?$ /viewpost.php?id=$1 [QSA] ---> No effect
You say viewpost.php is in the notalone folder, but you htaccess assumes it is in the root.
So I think it should be:
Options -MultiViews
RewriteRule ^notalone/viewpost/([0-9]+)/?$ /notalone/viewpost.php?id=$1 [QSA]
(Note that I added the multiviews, because that will be your next issue. The fact that the old url worked was because of multiviews.)

mode rewrite redirecting directory to another wont work

alright so i have a need for mode rewrite and i am completely noob/new to it.
I've read sever guide. that explained mode rewrite goes - pattern - redirection - flags.
i am trying to redirect anyone that gets into domain.com/product/ to domain.com/product.php
and pass the variable url=232 however i am first trying to redirect /product/ to product.php
Here is what i got so far.
Options +FollowSymLinks
RewriteEngine On
RewriteRule \/product\/ /product.php
I've started even more basic then that by redirect product.php to index.php and that worked.
am i doing something wrong ?
RewriteEngine On
RewriteBase /
RewriteRule ^product/?$ /product.php?url=232

mod_rewrite for pretty url with uriencoded query strings

ok, so I've tried many things I've found on SO and elsewhere, but I just can't get it to work, always receiving a 404 error code.
I'd like to enter this url:
memorizeit.com/pics/220.0.8251.20120905002352.7982368227/Jameson+tested+the+MemorizeIt%21+Android+app%3A+With+Facebook+%26+Twitter.+Getting+grass+stains.
and have it invisibly convert to:
memorizeit.com/pics/index.php?pic=220.0.8251.20120905002352.7982368227&title=Jameson+tested+the+MemorizeIt%21+Android+app%3A+With+Facebook+%26+Twitter
The index.php page is looking for:
$pic = ($_GET["pic"]);
$title = ($_GET["title"]);
In my .htaccess file in the /pics directory I've got the following:
RewriteEngine on
Header set Cache-Control "max-age=2592000"
RewriteRule ^pics/([^\/]*)/([^\/]*)/([^\/]*)$ /pics/index.php?pic=$1&title=$2&extra=$3 [L,QSA]
RewriteRule ^pics/(.+)/(.+)$ /pics/index.php?pic=$1&title=$2 [L,QSA]
RewriteRule ^pics/([^\/]*)$ /pics/index.php?pic=$1 [L,QSA]
I've tried it without the QSA, I've tried it with either .+ (anything) and [^/]* (anything except /) I left both in so you can see how I've put them there (I think!). I do plan on making the $1 allowed to only include numbers and periods, but I'd just like to get it to work being wide open first.
I can't figure out why it isn't working. In my base url .htaccess file I have:
RewriteCond %{SERVER_PORT} ^80$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
And it always redirects to https so I know the .htaccess files are being read. I don't know what else to try. Any thoughts would be greatly appreciated. Thanks!
Turns out that because pics/ is a real directory the rewrite was trying harder to get there than to just go ahead and rewrite it. So I changed the pics/ to p/ in the matching statement and moved the rules to the base .htaccess file and it works as expected.
Wow that took a long painful time... Hope it saves someone else some time.

url rewriting doesnt work for pages in subdomain

I need to change abc.mydomain.com/xyz.php?id3=se
to abc.mydomain.com/xyz/se/ with url rewriting.
Here is my code in .htaccess (placed in abc folder)
Options +FollowSymLinks
RewriteEngine on
RewriteRule /xyz/([0-9a-zA-Z]+) /xyz.php?id3=$1
I'm using a hosted server.
also tried without Options +FollowSymLinks but still doesn't work. appreciate any advice from someone please.
I guess you have to remove the "/" from "/xyz.php?id3=$1".
You are in abc folder '/www/abc', and your .htaccess is in the same folder, so the "/" makes the server thinks that the page xyz.php is in the root folder which is /www and not in /www/abc !
I had the same problem and I solved it this way!
So try this code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule /xyz/([0-9a-zA-Z]+) xyz.php?id3=$1
Add the domainname in your rule:
RewriteRule http://example.com/xyz/([0-9a-zA-Z]+) http://example.com/xyz.php?id3=$1

Redirect an URL with all variables to another URL

I need to redirect a URL to another URL. So far I have done this in the .htaccess:
Redirect 301 http://domain.com/mypage.php http://newdomain.com/mynewpage
This works fine, but it copies the GET variables in the new URL as well. So I end up with
http://domain.com/mypage.php?item=32
being redirected to:
http://newdomain.com/mynewpage?item=32
I want remove all the variables in the new ULR. I know that this has to be done somehow with mod_rewrite, but I am struggling a lot with it and can not make it work.
Any help is appreciated.
Thanks
Ok,I got it myself... here is what I have done:
RewriteEngine On
RewriteCond %{QUERY_STRING} (.*)
RewriteRule (.*) %{REQUEST_URI}?

Resources