简体   繁体   中英

.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?

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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM