301 .htaccess redirect on entire site EXCEPT main index page?

Currently using the following code to redirect an old domain to a new one:

RewriteEngine ON
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

The thing is, if people type in the old domain, I still wait the index.php page to show on the old domain… I just want it to redirect any OTHER pages to the new domain. I tried doing this but it still redirects everyone to the new domain regardless:

RewriteEngine ON
RewriteRule ^/$ http://www.olddomain.com/originalindex.php [R=301,L]
RewriteRule ^/index.php$ http://www.olddomain.com/originalindex.php [R=301,L]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

JM,

That’s because you’re using (.*) inappropriately (i.e., you need to learn regex). Okay, that’s only part of the problem so I’d recommend that you have a look at the tutorial article linked in my signature.

Saying that, your “specification” is that you want to redirect EVERY request from old-domain to new-domain EXCEPT index.php. What about support files? Are all the images, css, js, yadda-yadda supposed to be redirected, too (as (.*) will do)? I believe that you ONLY want to redirect .php requests.

Okay, that new “specification” translates to

  1. Check for old domain
  2. Check for NOT index.php
  3. Redirect all .php requests to same .php file on new domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} olddomain\\.com [NC]
RewriteCond %{REQUESST_URI} !^index\\.php$
RewriteRule ^(.*)\\.php$ http://newdomain.com/$1.php [R=301,L]

Note that the No Case flag is used because domains are case INsensitive and the permanent (R=301) redirect will cause SE’s to update their databases. The Last flag merely terminates this mod_rewrite block (like a } in PHP).

Regards,

DK

Hey DK!

I actually do want EVERYTHING to forward – including image folders, all file types, etc.

The only thing I don’t want to forward is when people go to olddomain.com or www.olddomain.com … i want www.olddomain.com/index.php to load in those cases. I actually do want everything else to be hosted at the new domain.

Tried changing the code to this:

RewriteEngine on
RewriteCond %{HTTP_HOST} olddomain\\.com [NC]
RewriteCond %{REQUEST_URI} !^index\\.php$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

But that causes everything to forward, including the main index page.

Is that the exact code you’re using? there’s a typo, ‘REQUESST_URI’ should have one ‘S’

SS,

Thanks for spotting that typo (corrected).

Regards,

DK

So how can I set it up in a way that the only file that loads on the old domain is when people access the main site olddomain.com or olddomain.com/index.php and the REST of the files forward to the new domain?

JM,

That’s the following lines of the above code:

RewriteCond %{HTTP_HOST} olddomain\\.com [NC]
RewriteCond %{REQUEST_URI} !^index\\.php$

Regards,

DK