kaliok
kaliok

Reputation: 11

htaccess rewrite/redirect

I am having some problems constructing a rewrite rule. The url I want to rewrite and ultimately redirect has a search query in it and looks like this:

http://www.mysite.com/pages.php?category=fruit

I would like to redirect it to:

http://www.mysite.com/pages.php/fruit

The original address does NOT exist any more. I have tried to construct a rewrite but this is not quite working how I want it to work

RewriteEngine on 
RewriteCond %{QUERY_STRING} =category=fruit` 
RewriteRule ^pages\.php$ pages.php/fruit/ [L,R=301]

goes to

http://www.mysite.com/home/linux123/m/mysite.com/user/htdocs/pages.php/fruit/

Any advice on fixing the construction of the rewrite rule would be great. Thanks in advance.

Upvotes: 0

Views: 392

Answers (1)

Aman
Aman

Reputation: 548

The way URL rewriting works is that it takes non-existant URL and rewrites it to point to the one that exists. The non-existant URL is more of presentation thing rather than a functional thing. You are doing it the other way round, the links on your web pages should be like http://www.mysite.com/pages.php/fruit and when the user clicks on them they should internally be forwarded to something like this http://www.mysite.com/pages.php?category=fruit. The rewrite rule has to be written accordingly which would be

^pages\.php/([A-Za-z])*$ pages.php?category=$1 [NC,L]

if the category is strictly alphabetical otherwise for alphanumeric

^pages\.php/([A-Za-z0-9])*$ pages.php?category=$1 [NC,L]

You can even test your regex rewrite rules using this online validator;

Regex Validator

Hope this helps..

Upvotes: 2

Related Questions