apache apache从非www重定向到www
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1100343/
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
apache redirect from non www to www
提问by user121196
I have a website that doesn't seem to redirect from non-www to www.
我有一个网站似乎没有从非 www 重定向到 www。
My Apache configuration is as follows:
我的Apache配置如下:
RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/ [r=301,nc]
What am I missing?
我错过了什么?
回答by Greg Hewgill
Using the rewrite engine is a pretty heavyweight way to solve this problem. Here is a simpler solution:
使用重写引擎是解决这个问题的重量级方法。这是一个更简单的解决方案:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
# real server configuration
</VirtualHost>
And then you'll have another <VirtualHost>section with ServerName www.example.comfor your real server configuration. Apache automatically preserves anything after the /when using the Redirectdirective, which is a common misconception about why this method won't work (when in fact it does).
然后您将有另一个<VirtualHost>部分ServerName www.example.com用于您的真实服务器配置。Apache 会/在使用Redirect指令后自动保留任何内容,这是关于为什么此方法不起作用(实际上它起作用)的常见误解。
回答by burzumko
http://example.com/subdir/?lold=13666=> http://www.example.com/subdir/?lold=13666
http://example.com/subdir/?lold=13666=> http://www.example.com/subdir/?lold=13666
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
回答by cherouvim
<VirtualHost *:80>
ServerAlias example.com
RedirectMatch permanent ^/(.*) http://www.example.com/
</VirtualHost>
回答by cherouvim
To remove wwwfrom your URLwebsite use this code in your .htaccessfile:
要从www您的URL网站中删除,请在您的.htaccess文件中使用以下代码:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1 [R=301,L]
To force wwwin your website URLuse this code on .htaccess
要强制www在您的网站上URL使用此代码.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^YourSite.com$
RewriteRule ^(.*)$ http://www.yourSite.com/ [R=301]
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)$ /.html [R=301,L]
Where YourSite.commust be replaced with your URL.
哪里YourSite.com必须用你的URL.
回答by Dishan Philips
<VirtualHost *:80>
DocumentRoot "what/ever/root/to/source"
ServerName www.example.com
<Directory "what/ever/root/to/source">
Options FollowSymLinks MultiViews Includes ExecCGI
AllowOverride All
Order allow,deny
allow from all
<What Ever Rules You Need.>
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
This is what happens with the code above. The first virtual host block checks if the request is www.example.com and runs your website in that directory.
这就是上面代码发生的情况。第一个虚拟主机块检查请求是否为 www.example.com 并在该目录中运行您的网站。
Failing which, it comes to the second virtual host section. Here anything other than www.example.com is redirected to www.example.com.
如果失败,则进入第二个虚拟主机部分。这里除了 www.example.com 之外的任何内容都被重定向到 www.example.com。
The order here matters. If you add the second virtualhost directive first, it will cause a redirect loop.
这里的顺序很重要。如果先添加第二个 virtualhost 指令,则会导致重定向循环。
This solution will redirect any request to your domain, to www.yourdomain.com.
此解决方案会将任何请求重定向到您的域,即 www.yourdomain.com。
Cheers!
干杯!
回答by weotch
This is similar to many of the other suggestions with a couple enhancements:
这类似于许多其他建议,但有一些改进:
- No need to hardcode the domain (works with vhosts that accept multiple domains or between environments)
- Preserves the scheme (http/https) and ignores the effects of previous
%{REQUEST_URI}rules. The path portion not affected by previous
RewriteRules like%{REQUEST_URI}is.RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ %{REQUEST_SCHEME}://www.%{HTTP_HOST}/ [R=301,L]
- 无需对域进行硬编码(适用于接受多个域或环境之间的虚拟主机)
- 保留方案 (http/https) 并忽略先前
%{REQUEST_URI}规则的影响。 不受之前
RewriteRules影响的路径部分%{REQUEST_URI}是。RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ %{REQUEST_SCHEME}://www.%{HTTP_HOST}/ [R=301,L]
回答by Curtis Tasker
RewriteCond %{HTTP_HOST} ^!example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
This starts with the HTTP_HOSTvariable, which contains just the domain name portion of the incoming URL (example.com). Assuming the domain name does not contain a www.and matches your domain name exactly, then the RewriteRule comes into play. The pattern ^(.*)$will match everything in the REQUEST_URI, which is the resource requested in the HTTP request (foo/blah/index.html). It stores this in a back reference, which is then used to rewrite the URL with the new domain name (one that starts with www).
这从HTTP_HOST变量开始,该变量仅包含传入 URL ( example.com)的域名部分。假设域名不包含 awww.并且与您的域名完全匹配,那么 RewriteRule 就会发挥作用。该模式^(.*)$将匹配 中的所有内容REQUEST_URI,即 HTTP 请求 ( foo/blah/index.html) 中请求的资源。它将其存储在反向引用中,然后使用该引用用新域名(以 开头的域名)重写 URL www。
[NC]indicates case-insensitive pattern matching, [R=301]indicates an external redirect using code 301 (resource moved permanently), and [L]stops all further rewriting, and redirects immediately.
[NC]指示不区分大小写的模式匹配,[R=301]指示使用代码 301(永久移动资源)的外部重定向,并[L]停止所有进一步的重写,并立即重定向。
回答by mikep
Redirection code for both non-www => www and opposite www => non-www. No hardcoding domains and schemes in .htaccess file. So origin domain and http/https version will be preserved.
非 www => www 和对面 www => 非 www 的重定向代码。.htaccess 文件中没有硬编码域和方案。所以原始域和 http/https 版本将被保留。
APACHE 2.4 AND NEWER
APACHE 2.4 及更新版本
NON-WWW => WWW:
非万维网 => 万维网:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
WWW => NON-WWW:
万维网 => 非万维网:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%1%{REQUEST_URI} [R=301,L]
Note: not working on Apache 2.2 where %{REQUEST_SCHEME} is not available. For compatibility with Apache 2.2 use code below or replace %{REQUEST_SCHEME} with fixed http/https.
注意:不适用于 %{REQUEST_SCHEME} 不可用的 Apache 2.2。为了与 Apache 2.2 兼容,请使用下面的代码或用固定的 http/https 替换 %{REQUEST_SCHEME}。
APACHE 2.2 AND NEWER
APACHE 2.2 及更新版本
NON-WWW => WWW:
非万维网 => 万维网:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
... or shorter version ...
...或更短的版本...
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|offs
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
WWW => NON-WWW:
万维网 => 非万维网:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
... shorter version not possible because %N is available only from last RewriteCond ...
...更短的版本是不可能的,因为 %N 仅从上次 RewriteCond 可用 ...
回答by Andrew Deal
I ran this...
我跑了这个...
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.*$ [NC]
RewriteRule ^/.+www\/(.*)$ http://www.%{HTTP_HOST}/ [R=301,L]
I need this to be universal for 25+ domains on our new server, so this directive is in my virtual.conf file in a <Directory> tag. (dir is parent to all docroots)
我需要这对我们新服务器上的 25 个以上域通用,因此该指令位于我的 virtual.conf 文件中的 <Directory> 标记中。(dir 是所有 docroot 的父级)
I had to do a bit of a hack on the rewrite rule though, as the full docroot was being carried through on the pattern match, despite what http://httpd.apache.org/docs/2.2/mod/mod_rewrite.htmlsays about it only being stuff after the host and port.
尽管http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html说了什么,但我不得不对重写规则进行一些修改,因为在模式匹配中正在执行完整的 docroot关于它只是主机和端口之后的东西。
回答by sys0dm1n
If you are using Apache 2.4 ,without the need to enable the rewrite apache module you can use something like this:
如果您使用的是 Apache 2.4,无需启用重写 apache 模块,您可以使用以下内容:
# non-www to www
<If "%{HTTP_HOST} = 'domain.com'">
Redirect 301 "/" "http://www.domain.com/"
</If>

