Markus Hansen
Markus Hansen

Reputation: 11

.htaccess 301 redirect all pages but the homepage

I want to 301 redirect oldsite.com to newsite.com I want to redirect all pages to the same URLs on the new site, except the homepage. Only the homepage will go to a separate page.

RewriteRule (.*) http://www.siteb.com/$1 [R=301,L]

So what I am trying to do is this:

oldsite.com/article1 should go to newsite.com/article1

oldsite.com/article2 should go to newsite.com/article2

and so on

However I want the

homepage oldsite.com to go to newsite.com/old-site

How can I do this?

Upvotes: 1

Views: 217

Answers (2)

Mohammed Elhag
Mohammed Elhag

Reputation: 4302

What you need here is to redirect all requests except home page so try this at the main root .htaccess :

RewriteRule ^(.+)$  http://www.newsite.com/$1 [L,R=302]

Then to redirect homepage to spacific page so add this line :

RewriteRule ^(.*)$  http://www.newsite.com/old-site [L,R=302]

Both like this :

RewriteRule ^(.+)$  http://www.newsite.com/$1 [L,R=302]
RewriteRule ^(.*)$  http://www.newsite.com/old-site [L,R=302]

This regex ^(.+)$ will catch every request except / then the other regex ^(.*)$ will catch the request to homepage / but according to home page you assigned you could except it before the first request like this , considering index:

RewriteCond %{REQUEST_URI} !^/index  
RewriteRule ^(.+)$  http://www.newsite.com/$1 [L,R=302]
RewriteRule ^(.*)$  http://www.newsite.com/old-site [L,R=302]

If there are query strings you could just put these rules :

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$  http://www.newsite.com/old-site [L,R=302]
RewriteRule ^(.+)$  http://www.newsite.com/$1 [L,R=302]

So , if there is no query string only with homepage otherwise redirect as is.

Note: if every thing goes Ok , change 302 to 301 to get permanent redirection & make sure you empty browser cache before testing

Upvotes: 0

Abhishek Gurjar
Abhishek Gurjar

Reputation: 7476

Try with below we are using separate condition and rule for root url and other directories url.

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

RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]

Upvotes: 1

Related Questions