How to redirect permanently (301) via .htaccess?
Hey guys! Can you please advise me how to modify my .htaccess so that
http://example.com (non-www without trailing slash)
http://example.com/ (non-www with trailing slash)
http://www.example.com (www without trailing slash)
will be permanently redirected (301) to
http://www.example.com/ (www with trailing slash)
?
Additionally, are there general rules to apply this "behavior" to subfolders
http://example.com/subfolder
http://example.com/subfolder/
http://www.example.com/subfolder
=> http://www.example.com/subfolder/
and subdomains (reversely here)
h开发者_开发知识库ttp://www.subdomain.example.com
http://www.subdomain.example.com/
http://subdomain.example.com
=> http://subdomain.example.com/
as well? As I'm completely new to this, please be kind... =)
Thanks! Nel
The DirectorySlashes directive takes care of the trailing slash problem.
The non-www to www redirect is:
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
These three rules should do all the things you want:
RewriteEngine On
# Rewrite www.subdomain.example.com to subdomain.example.com
RewriteCond %{HTTP_HOST} ^www\.(.*)\.example\.com
RewriteRule (.*) http://%1.example.com/$1 [L,R=301]
# Rewrite example.com to www.example.com
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [L,R=301]
# Add trailing slash to all URIs without one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
I think this will do the trick:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
精彩评论