我有一个网站似乎没有从非www重定向到www。
我的Apache配置如下:
RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
我漏掉了什么?
我有一个网站似乎没有从非www重定向到www。
我的Apache配置如下:
RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
我漏掉了什么?
使用重写引擎解决此问题是一种相对复杂的方法。这里有一个更简单的解决方案:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
# real server configuration
</VirtualHost>
然后您将拥有另一个<VirtualHost>
部分,其中包含ServerName www.example.com
以供您的真实服务器配置。当使用Redirect
指令时,Apache会自动保留斜杠/
之后的任何内容,这是关于为什么此方法不起作用的常见误解(实际上它确实有效)。
www.example
和 example
即可。 - Greg HewgillRedirect 301 / http://example2.com/extra/
但是当它进行重定向时,会丢失尾随的斜线,这意味着example.com/blah
会被重定向到example2.com/extrablah
。有什么解决方法吗?(Apache 2.2.22) - Peter Howehttp://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]
example.com/subdi1/ws/*
通过,但其他情况不通过呢? - black senseis
即可。 - rodrigoap<VirtualHost *:80>
ServerAlias example.com
RedirectMatch permanent ^/(.*) http://www.example.com/$1
</VirtualHost>
ServerAlias
中包含了www.
? - cherouvim要从您的URL网址中移除www
,请在您的.htaccess
文件中使用以下代码:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [R=301,L]
如果想在网站的URL中强制添加www
,请在.htaccess
文件中使用以下代码:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^YourSite.com$
RewriteRule ^(.*)$ http://www.yourSite.com/$1 [R=301]
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [R=301,L]
在编程中,YourSite.com
必须替换为您的 URL
。
<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>
以上代码的作用如下。第一个虚拟主机块检查请求是否为www.example.com,并在该目录中运行您的网站。
如果不成功,则转到第二个虚拟主机部分。在这里,任何不是www.example.com的请求都将重定向到www.example.com。
这里的顺序很重要。如果先添加第二个virtualhost指令,它会导致重定向循环。
此解决方案将重定向任何请求到您的域,至www.yourdomain.com。
干杯!
既可以将非www => www,也可以将相反的www =>非www进行重定向。不要在.htaccess文件中硬编码域名和协议。因此,原始域名和http / https版本将被保留。
NON-WWW => WWW:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
WWW => 非WWW:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%1%{REQUEST_URI} [R=301,L]
注意:在 Apache 2.2 上不起作用,因为 %{REQUEST_SCHEME} 不可用。若要与 Apache 2.2 兼容,请使用以下代码或将 %{REQUEST_SCHEME} 替换为固定的 http/https。
非 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]
... 或简短版本...
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转化为非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]
...无法提供更短的版本,因为%N仅从最后一个RewriteCond可用...
这与许多其他建议类似,但有一些改进:
%{REQUEST_URI}
规则的影响。路径部分不受先前RewriteRule
(如%{REQUEST_URI}
)的影响。
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ %{REQUEST_SCHEME}://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^!example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
这段代码以HTTP_HOST
变量开头,其中包含传入URL的域名部分(example.com
)。假设域名不包含www.
并且与您的域名完全匹配,则RewriteRule开始发挥作用。模式^(.*)$
将匹配REQUEST_URI
中的所有内容,这是HTTP请求中请求的资源(foo/blah/index.html
)。它将此存储在后向引用中,然后使用新域名(以www
开头的域名)重写URL。
[NC]
表示不区分大小写的模式匹配,[R=301]
表示使用代码301进行外部重定向(资源永久移动),[L]
停止所有进一步的重写,并立即重定向。
如果您正在使用Apache 2.4,无需启用rewrite Apache模块,您可以使用类似以下方式的方法:
# non-www to www
<If "%{HTTP_HOST} = 'domain.com'">
Redirect 301 "/" "http://www.domain.com/"
</If>
我运行了这个...
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.*$ [NC]
RewriteRule ^/.+www\/(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
.htaccess
的解决方案,我建议参考在相反问题上提出的答案:https://dev59.com/YHVC5IYBdhLWcg3woCnN#5262044 - hakre