Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite or Redirect Help Needed Please

         

Planet13

5:58 am on Apr 4, 2012 (gmt 0)

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



Hi there, Everyone:

Need a little help doing a mod rewrite / redirect from static pages on one site to wordpress pages on another site.

Of course, I am clueless...

All the files are in a directory called /reference/

I want to do a 301 rewrite from this:

www.old.example.com/reference/abc.html

to this file

www.new.example.com/news/newfilename/

Any help on how to do that with a mod rewrite would be much appreciated. I tried to do something like:

redirect 301 /reference/abc.html http://www.example.com/news/newfilename/

But that unfortunately appended a query string to the URL.

Thanks in advance.

[edited by: incrediBILL at 6:11 am (utc) on Apr 4, 2012]
[edit reason] exemplified URLs [/edit]

lucy24

6:19 am on Apr 4, 2012 (gmt 0)

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



Appended a query string?! Ouch. Where'd it come from? Query strings aren't just invented out of thin air. Please say you mean that there used to be a query string, and you wanted the Redirect to take it away but it didn't.

If that's the case, you've got a simple fix. Throw out everything using mod_alias (Redirect 301 by that name) and change it to mod_rewrite.* That way, you can get rid of the query string at the same time.

Can we stipulate that when you say "301 rewrite" you mean 301 redirect? Ain't no such animal as a 301 rewrite. In fact, rewrites don't get numbers at all, because they're done in secret. You want an upfront, visible redirect:

RewriteRule reference/abc.html http://www.example.com/news/newfilename/? [R=301,L]

See that ? at the end of the target? That means "dump the whole query string". If you're redirecting via mod_rewrite, the wording is exactly the same whether you're going to a different URL in the originating domain, or somewhere in a different domain.

... and now you know the other reason the Forums insist on "example.com" ;)


* By amazing coincidence, it is less than 24 hours since I posted a couple lines of Regular Expression that I used when globally changing a bunch of redirects from mod_alias to mod_rewrite. May or may not be useful.

g1smd

6:58 am on Apr 4, 2012 (gmt 0)

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



You're using Wordpress.

This uses URL rewriteing to convert friendly URLs to ugly internal filepaths "in secret".

If you really are redirecting
www.old.example.com/reference/abc.html
and not
www.old.example.com/reference/abc.html?someparameter=somevalue
then there's a simple explanation for the sudden appearance of a query string "from nowhere".

When you request
www.old.example.com/reference/abc.html
mod_rewrite internally rewrites this to
/index.php?page=abc
or similar. Next, mod_alias sees it needs to redirect the request. It uses the already rewritten pointer and redirects to
www.example.com/news/index.php?page=abc
or similar.

The fix is to use RewriteRule for all of your rules, both those that redirect and those that rewrite and to list all of the redirects before all of the rewrites. Within each group list from most specific to most general.

Redirect externally, before rewriting internally.