如何通过.htaccess永久重定向(301)?

如何通过.htaccess永久重定向(301)?,.htaccess,redirect,http-status-code-301,.htaccess,Redirect,Http Status Code 301,嘿,伙计们!你能告诉我如何修改我的.htaccess以便 http://example.com (non-www without trailing slash) http://example.com/ (non-www with trailing slash) http://www.example.com (www without trailing slash) 将永久重定向(301)到 http://www.example.com/ (带尾随斜杠的www) 此外,是否有将此“行为”应用于子文

嘿,伙计们!你能告诉我如何修改我的.htaccess以便

http://example.com (non-www without trailing slash)
http://example.com/ (non-www with trailing slash)
http://www.example.com (www without trailing slash)
将永久重定向(301)到

http://www.example.com/ (带尾随斜杠的www)

此外,是否有将此“行为”应用于子文件夹的一般规则

http://example.com/subfolder
http://example.com/subfolder/
http://www.example.com/subfolder
=> http://www.example.com/subfolder/
子域(此处相反)

还有吗?由于我对这一点完全陌生,请善待我……=)


谢谢!Nel

我认为这会起作用:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301] 

我认为这会起作用:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301] 

这三条规则可以满足您的所有需求:

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] 

这三条规则可以满足您的所有需求:

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] 

DirectorySlashes指令处理尾部斜杠问题

非www到www重定向为:

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

DirectorySlashes指令处理尾部斜杠问题

非www到www重定向为:

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

谢谢伊格纳西奥!我已经给了医生一次尝试,但必须承认,它的大部分只是越过我的头…=)我正在寻找一个“一刀切”的解决方案,这使它更难。。。真的需要你,专业人士!没关系,我误解了你的问题。你不需要重定向,你需要重写其他答案。删除了我错误的答案。谢谢伊格纳西奥!我已经给了医生一次尝试,但必须承认,它的大部分只是越过我的头…=)我正在寻找一个“一刀切”的解决方案,这使它更难。。。真的需要你,专业人士!没关系,我误解了你的问题。你不需要重定向,你需要重写其他答案。删除了我错误的答案。嘿,你!=)我想这就是我想要的——非常感谢!我是否必须像Eric引用的那样在这里添加“RewriteEngine On”和“RewriteBase/”?我相信只有RewriteEngine On。(我也会编辑我的答案)再次感谢,我会尽快尝试。嘿,你!=)我想这就是我想要的——非常感谢!我是否必须像Eric引用的那样在这里添加“RewriteEngine On”和“RewriteBase/”?我相信只有RewriteEngine On。(我也会编辑我的答案)再次感谢,我会尽快尝试。