URL rewriting that visibly rewrites (changes the URL in the address bar)
I asked sort of the complement of this question before:
Mod_rewrite invisibly: works when target is a file, not when it's a directory
Now I actually want a rewrite to happen visibly, because I've switched URL schemes and although I want the old links to work, I want the user to see the new URL scheme.
So this 开发者_如何学Cworks
RewriteRule ^oldscheme/(.*)/?$ newscheme/$1
But the URL in the address bar remains as http://example.com/oldscheme/foo
.
What's the right way to do a visible rewrite, preferably just with mod_rewrite as opposed to something kludgy with Location redirects or somesuch?
As I cannot leave comments now, I'll post my addition to Ignacio's comment here.
You actually should post a 301 (Moved Permanently) redirect, as you're describing there's a new site directory structure. So your RewriteRule should read
RewriteRule ^oldscheme/(.*)/?$ newscheme/$1 [R=301]
It turns out adding a "redirect" code does the trick:
RewriteRule ^oldscheme/(.*)/?$ newscheme/$1 [R]
Obvious in retrospect, but hopefully this makes the answer more searchable. I found it on this excellent "cheat sheet":
http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/
精彩评论