Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess redirect from GET variable to url string

I need to redirect

/search?keywords=somesearchterm

to

/search/somesearchterm

This seems incredibly basic but I've been pounding my head against it for an hour.

Thanks for taking the time to look at this.

like image 512
Eric_WVGG Avatar asked Aug 21 '10 23:08

Eric_WVGG


1 Answers

You want to implement what is called a "301 Redirect" with mod_rewrite.

RewriteEngine ON
RewriteRule ^/search\?keywords=somesearchterm$ /search/somesearchterm

adding regular expressions:

RewriteEngine ON
RewriteRule ^/search\?keywords=(.+) /search/$1 [R=301,L]

R=301 means provide a 301 Header redirect so the user's URL changes in the browser, and L means don't process any more rewrite rules if this one matches.


If you want to do the reverse -- in other words, if someone goes to mysite.com/search/asearchterm and you want the URL to stay the same, but "behind the scenes" you want it to load a certain server script, do this:

RewriteEngine ON
RewriteRule ^/search/(.+) /search.php\?keywords=$1 [L]
like image 53
Michael Butler Avatar answered Oct 15 '22 03:10

Michael Butler