.htaccess Redirects
Im using Apache and an .htaccess file to set up some redirects. Is there a way to 301 redirect everything in a domain to a subdomain except for the domain index?
So redirect http://doma开发者_开发技巧in.com/*
to http://sub.domain.com/*
But leave http://domain.com/
where it is?
Thanks in advance if anybody is able to help!
Single rewrite rule:
# Rewrite if on main domain AND NOT requesting index
RewriteCond %{HTTP_HOST} ^domain[.]com$ [NC]
RewriteCond %{REQUEST_URI} !^(/(index[.](html|php))?)?$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://sub.%1$1 [R=301,QSA,L]
# Check for non-empty request URI
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .* http://sub.domain.com%1 [L,R=301]
You probably want to allow for default html or php on your main domain. In that case, the following pair of rules should work. Test and confirm.
# Skip the rewrite for the root of domain.com
RewriteCond %{HTTP_HOST} ^domain[.]com$ [NC]
RewriteCond %{REQUEST_URI} ^(/(index[.](html|php))?)?$
RewriteRule ^(.*)$ - [L]
# Rewrite everything else
RewriteCond %{HTTP_HOST} ^(domain[.]com)$ [NC]
RewriteRule ^(.*)$ http://sub.%1$1 [R=301,QSA,L]
精彩评论