Forum Moderators: phranque

Message Too Old, No Replies

htaccess rewrite from query string to directory

changing?tag=x to /tag/x

         

bradlby

10:28 pm on Oct 17, 2007 (gmt 0)

10+ Year Member



I was using the new version of Wordpress which has a tag structure built in when I noticed I had redundant URLs

example.com/index.php?tag=foo
example.com/tag/foo

I am in the process of trying to get .htaccess to redirect from the query string to the directory, and so far this is what I've got:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^tag=(.*)$
RewriteRule .* /tag/%1 [R=301,L]

This does nothing. I also tried:

RewriteRule ^index\.php$ /tag/%1 [R=301,L]

but got no results. Any advice on where I've gone astray would be greatly appreciated.

jdMorgan

10:43 pm on Oct 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is the query string always going to be exactly "tag=<something>" -- never containing more parameters? If not, then the code will need to take those possible extra parameters into account.

However, despite the fact that "I get nothing" provides us with no information, I'd guess that mod_rewrite is not enabled on your server. Have you tried a dirt-simple mod_rewrite function, such as


RewriteRule ^foo\.html$ http://www.google.com [R=301,L]

With that code in place, requesting "example.com/foo.html" from your server should take you to google's home page.

If that doesn't work and does not create a meaningful entry in your server error log, then it's likely you don't have mod_rewrite installed or enabled.

Also make sure you are completely flushing your browser cache after any change to your .htaccess code or server configuration. If the URLs you use to test have been previously cached by your browser, then your test requests will be served from your browser cache, no requests will be sent to your server, and therefore, no server-side code can/will have any effect on the results.

Jim

bradlby

11:07 pm on Oct 17, 2007 (gmt 0)

10+ Year Member



Well in an embarrassing turn of events, moving my rule above a different rule in the .htaccess file actually made it start working. I can't say that I understand that, but I am no expert, so anything is possible I suppose!

jdMorgan, thanks for the idea to test a really simple rule, if I hadn't been tooling around with that I never would have figured it out.

also, in case anyone is looking at this for future reference, make sure your rule is actually

RewriteRule .* /tag/%1/? [R=301,L]

without the trailing? the query string will be re-appended and it will rewrite itself infinitely.