apache 通用 htaccess 将 www 重定向到非 www
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/234723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generic htaccess redirect www to non-www
提问by deepwell
I would like to redirect www.example.comto example.com. The following htaccess code makes this happen:
我想重定向www.example.com到example.com. 以下 htaccess 代码实现了这一点:
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/ [L,R=301]
But, is there a way to do this in a generic fashion without hardcoding the domain name?
但是,有没有一种方法可以在不对域名进行硬编码的情况下以通用方式执行此操作?
回答by
回答by Andron
But if we need to do this for separate http and https:
但是,如果我们需要为单独的 http 和 https 执行此操作:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/ [R=301,L]
回答by Dmitriy
Redirect non-wwwto www(both: http + https)
将非 www重定向到www(均:http + https)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/ [R=301,L]
回答by William Denniss
If you want to do this in the httpd.conf file, you can do it without mod_rewrite (and apparently it's better for performance).
如果您想在 httpd.conf 文件中执行此操作,您可以在没有 mod_rewrite 的情况下执行此操作(显然它对性能更好)。
<VirtualHost *>
ServerName www.example.com
Redirect 301 / http://example.com/
</VirtualHost>
I got that answer here: https://serverfault.com/questions/120488/redirect-url-within-apache-virtualhost/120507#120507
我在这里得到了答案:https: //serverfault.com/questions/120488/redirect-url-within-apache-virtualhost/120507#120507
回答by Salman A
Here are the rules to redirect a www URL to no-www:
以下是将 www URL 重定向到 no-www 的规则:
#########################
# redirect www to no-www
#########################
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/ [R=301,NE,L]
Here are the rules to redirect a no-www URL to www:
以下是将无 www 的 URL 重定向到 www 的规则:
#########################
# redirect no-www to www
#########################
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/ [R=301,NE,L]
Note that I used NEflag to prevent apache from escaping the query string. Without this flag, apache will change the requested URL http://www.example.com/?foo%20barto http://www.example.com/?foo%2250bar
请注意,我使用NE标志来防止 apache 转义查询字符串。如果没有这个标志,apache 会将请求的 URL 更改http://www.example.com/?foo%20bar为http://www.example.com/?foo%2250bar
回答by Michael Cramer
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/(.*)$ https://%1/ [R]
The RewriteCondcaptures everything in the HTTP_HOSTvariable afterthe www.and saves it in %1.
在RewriteCond捕捉一切都在HTTP_HOST变化后的www.并保存在%1。
The RewriteRulecaptures the URL without the leading /and saves it in $1.
在RewriteRule捕获没有领先的URL/并保存在$1。
回答by Greg
Try this:
尝试这个:
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ %{HTTP_HOST} [C]
RewriteRule ^www\.(.*)$ http:// [L,R=301]
If the host starts with www, we stick the whole host onto the start of the URL, then take off the "www."
如果主机以 www 开头,我们将整个主机粘贴到 URL 的开头,然后去掉“www”。
回答by Gregor Macgregor
Complete Generic WWW handler, http/https
完整的通用 WWW 处理程序,http/https
I didn't see a complete answer. I use this to handle WWW inclusion.
我没有看到完整的答案。我用它来处理 WWW 包含。
- Generic. Doesn't require domain info.
- Forces WWW on primary domain: www.domain.com
- Removes WWW on subdomains: sub.domain.com
- Preserves HTTP/HTTPS status.
- Allows individual cookies for domain / sub-domains
- 通用的。不需要域信息。
- 在主域上强制 WWW:www.domain.com
- 删除子域上的 WWW:sub.domain.com
- 保留 HTTP/HTTPS 状态。
- 允许域/子域的单独 cookie
Please let me know how this works or if I left a loophole.
请让我知道这是如何工作的,或者我是否留下了漏洞。
RewriteEngine On
RewriteBase /
# Force WWW. when no subdomain in host
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ [NC]
RewriteCond %{HTTPS}s ^on(s)|off [NC]
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Remove WWW. when subdomain(s) in host
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|off [NC]
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)(.+\.)(.+\.)(.+)$ [NC]
RewriteRule ^ %1%3%4%5%{REQUEST_URI} [R=301,L]
回答by Htaccess Redirect
There can be a lot of misinformation out there about htaccess redirects, I find. First off, make sure your site is running on Unix using Apache and not on a Windows host if you expect this code to work.
我发现可能有很多关于 htaccess 重定向的错误信息。首先,如果您希望此代码正常工作,请确保您的站点在使用 Apache 的 Unix 上运行,而不是在 Windows 主机上运行。
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
(Make sure there are no line spaces between each line of text, though; I have added an extra space between lines so it renders okay in this window.)
(不过,请确保每行文本之间没有行间距;我在行之间添加了一个额外的空格,以便在此窗口中显示正常。)
This is one snippet of code that can be used to direct the www version of your site to the http:// version. There are other similar codes that can be used, too.
这是一段代码,可用于将您网站的 www 版本定向到 http:// 版本。还有其他类似的代码也可以使用。
回答by Rick
For those that need to able to access the entire site WITHOUTthe 'www' prefix.
对于那些需要能够在没有“www”前缀的情况下访问整个站点的人。
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]
Mare sure you add this to the following file
确保您将此添加到以下文件中
/site/location/.htaccess

