如何重定向所有服务器别名,然后www www in .htaccess in .htaccess in .htaccess
寻找一种将使用Apache VirtualHosts和.htaccess
的方法重定向多个服务器别名(www and nonwww)将多个服务器别名(www and nonwww)重定向到一个非www域。我现在已经搜索了一段时间,已经找到了几种解决方案,但是它们都在某种程度上起作用。
情况如下,在我的.conf
文件中,我有一个虚拟主机指定为:
<VirtualHost *:443>
ServerName example.domain
ServerAlias *.example.domain *.exampledomain.com exampledomain.com
...
</VirtualHost>
<VirtualHost *:80>
ServerName example.domain
ServerAlias *. example.domain *.exampledomain.com exampledomain.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.domain [OR]
RewriteCond %{SERVER_NAME} =example.domain
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
接下来,我在.htaccess
:结果中有以下内容:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www.exampledomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^exampledomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.domain$
RewriteRule ^(.*)$ https://example.domain/$1 [L,R=301]
结果如下:
http://example.domain/ -> https://example.domain/ - (correct)
http://www.example.domain/ -> https://example.domain/ - (correct)
http://exampledomain.com/ -> http://exampledomain.com/ - Forbidden, you dont have access...
http://www.exampledomain.com/ -> http://www.exampledomain.com/ - Forbidden, you dont have access...
https://exampledomain.com/ -> https://example.domain/ - (correct)
https://www.exampledomain.com/ -> https://www.exampledomain.com/ - Connection not secure
我真的无法弄清楚这出错了哪里,为什么有些重定向工作而其他重定向不行。任何提示都将不胜感激。
Looking for a way to redirect several server aliases (both www and non-www) to one non-www domain with forced SSL using Apache VirtualHosts and .htaccess
. I've been searching for a while now and have found several solutions but they all work partially.
The situation is as follows, in my .conf
file I have a virtual host specified as follows:
<VirtualHost *:443>
ServerName example.domain
ServerAlias *.example.domain *.exampledomain.com exampledomain.com
...
</VirtualHost>
<VirtualHost *:80>
ServerName example.domain
ServerAlias *. example.domain *.exampledomain.com exampledomain.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.domain [OR]
RewriteCond %{SERVER_NAME} =example.domain
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Next, I have the following in my .htaccess
:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www.exampledomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^exampledomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.domain$
RewriteRule ^(.*)$ https://example.domain/$1 [L,R=301]
Result is as follows:
http://example.domain/ -> https://example.domain/ - (correct)
http://www.example.domain/ -> https://example.domain/ - (correct)
http://exampledomain.com/ -> http://exampledomain.com/ - Forbidden, you dont have access...
http://www.exampledomain.com/ -> http://www.exampledomain.com/ - Forbidden, you dont have access...
https://exampledomain.com/ -> https://example.domain/ - (correct)
https://www.exampledomain.com/ -> https://www.exampledomain.com/ - Connection not secure
I really can't figure out where this goes wrong and why some redirects work and others don't. Any hints would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您可以访问
&lt; virtualHost&gt;
,那么您就不需要(不应该)使用.htaccess
。如果目标是将单个重定向的规范域(+https)重定向,则仅使用定义的两个VHOST,而您所需要的只是:
mod_alias
redirect
指令是前缀匹配和比赛后的所有内容附加到目标URL的末尾。因此,重定向
上述指令将每个URL重定向到目标的同一URL。您应首先使用302(临时)重定向测试,并且仅一旦确认其按预期工作就可以更改为301(永久)。您可能需要清除浏览器缓存,因为浏览器将301持续缓存。
查看您的“结果”:
当前的HTTP到https重定向:80容器仅重定向
www.example.example.domain
example.domain.domain < /code>,并且您可能不接受VHOST中的请求:80个容器,因此任何HTTP请求(未重定向到HTTPS)可能都会阻止。您的SSL证书需要涵盖所有域和所有别名,否则,您(充其量)将获得浏览器SSL证书警告,并且浏览器将拒绝连接到服务器(因此看不到重定向)。
If you have access to the
<VirtualHost>
then you don't need to (should not) use.htaccess
at all for this.If the goal is to redirect to the canonical domain (+HTTPS) in a single redirect using just the two vHosts as defined then all you need is:
The mod_alias
Redirect
directive is prefix matching and everything after the match is appended to the end of the target URL. So, theRedirect
directive above redirects every URL to the same URL at the target.You should test first with 302 (temporary) redirects and only change to a 301 (permanent) once you have confirmed it works as intended. You will likely need to clear your browser cache since 301s are cached persistently by the browser.
A look at your "results":
The current HTTP to HTTPS redirect in the vHost:80 container is only redirecting
www.example.domain
andexample.domain
and you probably aren't accepting requests in the vHost:80 container, so any HTTP request (that is not redirected to HTTPS) is probably blocked.Your SSL cert needs to cover all domains and all aliases, otherwise, you will (at best) get a browser SSL cert warning and the browser will refuse to connect to your server (so does not see the redirect).