sparecycle
sparecycle

Reputation: 2058

htaccess is rewriting entire URL and leaving off subdirectories

I thought I had this figured out but no dice... I'm attempting to rewrite a domain but preserve the subdirectory paths:

www.olddomain.com/sub/directory/page.php

TO

www.newdomain.com/sub/directory/page.php

The following two directives are both needed. The first is the attempt to accomplish the above. The second is inserting a hidden "index.php" before every URL.

RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Curently, no matter what URL I visit on olddomain.com, it takes me to newdomain.com/index.php. Maybe it is inserting the "index.php" from the previous site... I inserted [L] to attempt it to stop parsing the .htaccess file but it doesn't like that either. Any direction would be much appreciated.

Upvotes: 2

Views: 57

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

You need to make sure to remove any rules you have on your old site (if the site resides in a different place than your new domain), otherwise, you need to make sure that the redirect is the very first rule, right under the RewriteEngine On line.

Your rule:

RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

is correct. And as long as there's nothing before it, it should work.

Make sure you've cleared your browser's cache.

Upvotes: 2

Related Questions