Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite redirection avoiding query string

         

simonvlc

12:20 am on Aug 21, 2005 (gmt 0)

10+ Year Member



Hello,

I have a little problem with a mod rewrite rule. I'm using a drupal system that uses mod rewrite to make the urls search engine friendly in this manner:

http://example.ord/index.php?q=node/99 >>> http://example.org/example-url

The problem is that now I need to redirect (301) some of the pages to another urls.

From http://example.org/example-url To http://example.org/new-example-url

I used this rewrite rule to do the trick:

RewriteCond %{REQUEST_URI} ^example-url(.*)
RewriteRule ^(.*)$ /new-example-url [R=301,L]

But when I type the first url it redirects to the new page but adds a query string to the end in this manner:

http://example.org/new-example-url?q=example-url

How can I avoid to add that query string to the end? Thank you a lot, Simon.

jd01

12:36 am on Aug 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Simon,

Welcome to WebmasterWorld.

You can elimate the current query string by appending a blank query string to the 'new' URL

RewriteCond %{REQUEST_URI} ^example-url(.*)
RewriteRule ^(.*)$ /new-example-url? [R=301,L]

Hope this helps.

Justin

jdMorgan

2:02 am on Aug 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note that code which invokes an external redirect should specify a canonical URL, and that the regex can be shortened:

RewriteCond %{REQUEST_URI} ^example-url
RewriteRule .* http://www.example.com/new-example-url? [R=301,L]

or even:

RewriteRule ^example-url http://www.example.com/new-example-url? [R=301,L]

If the old URLs are tested explicitly --that is, if you test for an exact old URL-- then you should end-anchor the pattern as well:

RewriteRule ^example-url$ http://www.example.com/new-example-url? [R=301,L]

Jim

simonvlc

9:12 am on Aug 21, 2005 (gmt 0)

10+ Year Member



It works perfectly escaping the '?' character:

RewriteRule ^(.*)$ http://example.org/new-example-url\? [R=301,L]

Thanks a lot, Simon.