1

I need some help trying to redirect a query to a url.

www.example.com/?categoryID=79
needs to redirect to
www.example.com/catname

where catname is just a string, it has no variables.

Here's what I tried so far:
I began with a humble approach that failed horribly:

redirect 301 /?CategoryID=79 http://www.example.com/catname/   

then i moved on to mod_rewrite :  
RewriteCond %{QUERY_STRING} CategoryID=79$ 
RewriteRule (.*) /catname/? [R=301,L] 

Both did not work and I'm actually stomped.
any help would be appreciated.

Just to be clear, In the end i'll have many of these rules redirecting to various category names.

important - it's not enough for /catname to display the proper page, the request with the query parameter must redirect to the new url.

1 Answer 1

1

This rule should work for you as first rule in your .htaccess:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+\?CategoryID=79[&\s] [NC]
RewriteRule ^ /catname/? [R=302,L]
Sign up to request clarification or add additional context in comments.

6 Comments

That's amazing :) Any chance you can explain what's going on in those two lines of code? Or if you can refer me to any resources on the matter? Most resources are found I honestly could not follow.
Good resource is askapache.com and about this rule I am using %{THE_REQUEST} variable here. THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1
Thanks for that answer, Could you please specify a RewriteCond for a URL that looks www.example.com/?categoryID=79?OK=100 - I tried expanding from your example but that did not work too well :)
You cannot match URL and host name both in same RewriteCond For matching host name you need to use RewriteCond %{HTTP_HOST} ^www\.example\.com$ and another RewriteCond For matching URI.
For capturing 2 query parameters use: RewriteCond %{THE_REQUEST} \s/+\?CategoryID=79&OK=([^&]+)[&\s], then use %1 and %2 in the target URI.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.