Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

301 redirect with query string

3:30 pm on Jul 13, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 12, 2001
votes: 0

I've had a quick look through the forum posts and have not come across an answer to the following.

I have a site with urls in the following format:


I have set up rewrite rules so that page22.html gets rewritten to page.php?page=22 - this works fine.

The problem is that pages from the site are indexed by google etc and I want to have a 301 redirect so that anyone clicking on page.php?page=22 will get redirected to page22.html. Therefore in time these pages will be removed from search engines and replaced with the shorter friendlier ones.

I have tried the following but does not work.

RewriteRule ^/category.php?cat=([^/]+) /cat-$1.html [R=permanent]

Any advice much appreciated.

4:07 pm on July 13, 2004 (gmt 0)

Preferred Member

10+ Year Member

joined:May 4, 2004
votes: 0

The question mark has to be escaped, otherwise it is threated as a regular expression function (zero or one occurence of the preceding expression). Additionaly in the RewriteRule you can check the query string (because it is not included, only the path and the filename of the requested resource), that has to be evaluated by a preceding RewriteCond separately. Something like this:

RewriteCond %{QUERY_STRING} ^cat=([^&]+) [NC]
RewriteRule ^/category\.php$ /cat-%1.html [R=permanent,QSA,L]

See the mod_rewrite documentation [httpd.apache.org] for further details.
3:59 am on July 16, 2004 (gmt 0)

Full Member

10+ Year Member

joined:May 5, 2003
votes: 0


I believe that this is the same type of issue I am struggling to solve. Is my solution the same as the OP in this thread?

I have a link redirection cgi script, taking numeric link variables and sending the browser to the correct affiliated website. Here is my current working link style:

<a href="/cgi-bin/links.cgi?L=24">Product code 24</a>

I have a ton of links like this, with numbers starting at 1 and going up, and wish to abbreviate them even more, so that I can use a form like this to go to the product pages:

<a href="/c/l/24">Product code 24</a>

How can I use a RewriteRule in .htaccess to translate the abbreviated 7 character code into the full URI, including the Query string (?L={selected number} )?

Thanks in advance, Wiz

7:23 pm on July 19, 2004 (gmt 0)

Preferred Member

10+ Year Member

joined:May 4, 2004
votes: 0

It is not really recommended to change the cgi-bin to anything else; it could be that your cgi script stop working. If this happens you can try to add the T=application/x-httpd-cgi flag in addition to the existing RewriteRule flags, it might solve the problem then.

By the way, I don't really think that this solution is for you, since this rewrites urls with the query string to a pathname tweaked one, but only for the incoming requests. This solution you need only if you want to tell the search engines to change the location of the already indexed pages to a new one, but for a script which redirect the browser only it is just useless. What you need is just the opposite of this, rewrite a pathname tweaked url to a query string'd one. It is even easier than this one, since you need only a single RewriteRule directive. See the mod_rewrite documentation [httpd.apache.org] on how to accomplish on this.


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members