views:

352

answers:

3

I asked sort of the complement of this question before:

http://stackoverflow.com/questions/1685030/modrewrite-invisibly

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?

+1  A: 

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/

dreeves
The [R] parameter 'forces' a rewrite of the URL if i'm correct.
Ropstah
It results in a response code of 302 along with a `Location` header being sent to the browser. Without it, only internal httpd structures are modified if possible.
Ignacio Vazquez-Abrams
+3  A: 

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]
Marcel Korpel
A: 

hi there, would this 301 redirect idea apply to this scenario?:

my htaccess file uses a single php template page to pull page content into the browser, using the location in the address bar (so mysite.com/products/featured.php would get the "featured" content file in the "products" directory). I have it set up to get an "index" content page as well when the address ends in /index.php or just /.

however, when you don't specify /index.php the address bar shows the hidden query string in my htaccess file (example: mysite.com/?c=products&p=featured).

so could I keep my existing rule for the address that ends in /index.php but then change the rule for the address ending in / to redirect to /index.php...then the query string issue above won't be seen?

hope that makes sense.

Stephan Hovnanian
You'd better ask a new question for this (after searching, that is). I don't think anyone sees this. http://stackoverflow.com/faq
Marcel Korpel