Forum Moderators: phranque

Message Too Old, No Replies

redirect multiple query string params

htaccess - redirect multiple query string to new seo friendly urls

         

simwiz

4:03 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



Hi, if anyone could help me with this it would be greatly appreciated.

I have three different url patterns

index.php?cat=11
index.php?subcat=18
index.php?cat=46&product=testing

I have the bellow in my htaccess

#redirect cat
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /testdir/index\.php\?cat=([a-z]+) [NC]
RewriteRule ^index\.php$ [domain.co.uk...] [R=301]
RewriteRule ^([a-z-]+)/?$ index.php?cat=$1 [NC]

# Redirect for cat and product

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /spw/home2garden/index\.php\?cat=([a-z]+)\&product=([a-z-]+)
RewriteRule ^index\.php$ [domain.co.uk...] [R=301]
RewriteRule ^cat/([a-z-]+)/([a-z-]+).html?$ index.php?cat=$1&product=$2 [NC]

They work perfectly if only one of the above is present, either cat or product and cat. When I run the two param query string the result is a re-written and redirect url with only the first query string param.

I feel one is over writing the other. I thought the purpose of the rewritecond is so the rewrite work on a certain condition.

my rewrites work well but I just need to re-direct all instances of the query string regardless of how many are within the url.

if anyone can give me any guidance it would be greatly appreciated.

I'm nearly there!

g1smd

4:54 pm on Mar 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Lit all redirects first (the rules with R=301, that is).

List all of the rewrites after all of the redirects.

Add [L] to the end of EVERY rule.

Make sure you have a non-www/www redirect fix listed as the very last redirect before the rewrites.

Flush browser cache and test again.

simwiz

7:16 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



the first set of rules for index.php?cat=testcat would give /cat/testcat/ GREAT! the second set of rules for index.php?cat=testcat&product=prodname would give cat/testcat/prodname.html GREAT!

now when both are enabled no # the second querystring cat/productname would give the same output as the first!

I just thought I would make this slightly clearer

they both work, just not together!

g1smd

7:22 pm on Mar 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Have you made the changes that I asked you to make yet?

Post your updated code here.

jdMorgan

10:18 pm on Mar 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rules in order: External redirects first, ordered from most-specific to least-specific, followed by internal rewrites, again ordered from most-specific to least-specific. Corrected regex patterns, made them consistent with each other, and added [L] flags:

# Redirect direct client requests for cat and product dynamic URL back to static URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /testdir/index\.php\?cat=([a-z\-]+)&product=([a-z\-]+)\ HTTP/ [NC]
RewriteRule ^index\.php$ http://example.co.uk/testdir/cat/%1/%2.html? [R=301,L]
#
# Redirect direct client requests for cat dynamic URL back to static URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /testdir/index\.php\?cat=([a-z\-]+)\ HTTP/ [NC]
RewriteRule ^index\.php$ http://example.co.uk/testdir/%1/? [R=301,L]
#
# Internally rewrite static cat and product URLs to dynamic filepaths
RewriteRule ^cat/([a-z\-]+)/([a-z\-]+)\.html?$ index.php?cat=$1&product=$2 [NC,L]
RewriteRule ^([a-z\-]+)/?$ index.php?cat=$1 [NC,L]

To the extent possible, I made these rules consistent with each other. However, it appears that your "cat" URLs end with a slash, while your cat+product URLs end with ".html". I'm not sure why you have this naming inconsistency, so I did not change it.

You should also pick one or the other of URLs ending with a trailing slash versus not ending with a trailing slash, and URLs ending with ".html" versus those ending with ".htm". Redirect the non-preferred URLs to the preferred ones, and then remove the "permissiveness" from the rules above. Allowing two different URLs to access the same content creates a duplicate-content issue -- one you can read about in many threads here.

Jim

simwiz

9:40 am on Mar 11, 2009 (gmt 0)

10+ Year Member



g1smd and Jim thank you for your replies and help. Since this is considered scripting I think my n00bish placement of redirect and rewriteRules were all over the place. I appreciate both your help immensely. THis is definitely a learning curve.

To answer your question to why the cat has a trailing forward slash is on the basis of the cat is a directory in my opinion and the product is a inner page so I wanted a file extension added to this.

The rewrite was intended to create categories as directory’s and products as html files. (I appreciate you not changing the pattern)!

Thanks again.