Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 417
dreeves Avatar asked Dec 30 '22 05:12

dreeves


2 Answers

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]
like image 144
Marcel Korpel Avatar answered Jan 03 '23 11:01

Marcel Korpel


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/

like image 44
dreeves Avatar answered Jan 03 '23 11:01

dreeves