简体   繁体   中英

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 works

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM