Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite: Simple URL string replacement?

Redirect a URL string variable to new variable

         

jstarkweather

5:33 pm on Mar 30, 2004 (gmt 0)

10+ Year Member



I have searched hi-and-lo for this example code. No luck so I though I would see if you guys can help.

I need a redirect to forward requests for a soon to be out-of-date URL to the new module (this was a forum upgrade where the forum module name changed).

The old string would come in like this:

[site.com...]

I added the foo bar variables because this may be the tricky part. There will be several different variations on the full URL string (based on different functions in the forum). So a perfect solution would redirect all requests after $OldName and keep the rest in tact.

The rewrites would simply need to go to:

[site.com...]

In case anyone is wondering, I want to do this because there are numerous existing links to the old forum both in my forum and out on the net. I want to keep these links working.

Thanks for any help.

Jim

Birdman

5:52 pm on Mar 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll take a stab at it.

RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*)module=OldName(.*)
RewriteRule ^(.*)$ /$1%1module=NewName%2 [R=301,L]

Hopefully, that'll do it but keep a backup ready ;)

jstarkweather

6:31 pm on Mar 30, 2004 (gmt 0)

10+ Year Member



Is that for httpd.conf or an .htaccess file?

It didn't work via httpd.conf

Thanks,
Jim

Birdman

8:14 pm on Mar 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I mostly use .htaccess since it's all I have on most sites I work on. I meant it for .htaccess. What exactly happened when you tried it. Did you get a 500(internal) error? Did it redirect to the wrong place?

By the way, welcome to Webmaster World!

jstarkweather

7:08 am on Mar 31, 2004 (gmt 0)

10+ Year Member



I tried putting it into the .htaccess file at root. It appeared to loop and keep repeating the replacement set. So 404.

Thanks,
Jim

Birdman

1:00 pm on Mar 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I tested it and there was one character missing, the (?) in the rewritten URL. I thought that it would be captured in the rewrite cond, but apparently not.

Here is the updated code that worked for me:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*)module=OldName(.*)
RewriteRule ^(.*)$ /$1?%1module=NewName%2 [R=301,L]