【发布时间】:2016-04-25 00:12:05
【问题描述】:
我有一个使用 2 个单独的虚拟主机文件的域:一个用于 :80,一个用于 :443
:80 设置非常简单,唯一的工作就是重定向到 :443:
<VirtualHost *:80>
# This is the first host so it's the default.
# So although I've specified a ServerName and ServerAlias anything else not specified elsewhere will also end up here.
ServerName www.domain.com
ServerAlias domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# Redirect everything to https:
RewriteEngine on
RewriteRule ^(.*)$ https://www.domain.com$1 [R=301,L]
</VirtualHost>
如果 :443 不存在,只需将 www 添加到 url 的开头即可:
<VirtualHost *:443>
# This is the first host so it's the default.
# So although I've specified a ServerName and ServerAlias anything else not specified elsewhere will also end up here.
ServerName www.domain.com
ServerAlias domain.com
ErrorLog ${APACHE_LOG_DIR}/ssl.error.log
CustomLog ${APACHE_LOG_DIR}/ssl.access.log combined
# Redirect everything which is not already on the real www domain name to that:
RewriteEngine on
RewriteCond %{HTTP_HOST} !www.domain.com
RewriteRule ^(.*)$ https://www.domain.com$1 [R=301]
ErrorDocument 404 /404.html
</VirtualHost>
我有 1 个案例,其中这些重写似乎失败了:
https://domain.com -> 应该指向 https://www.domain.com 但它指向 https://www.domain.com%24/# 。显然后面的字符会阻止 DNS 服务器找到域。
是什么导致了这个问题?我已经帮助创建了这些虚拟主机文件,但它们似乎仍未按预期完全工作。
但我也想将我的 URL 重写为更好的 URL。我认为我的规则是正确的,并且 :443 中的 Rewrite 块如下所示
RewriteEngine on
RewriteCond %{HTTP_HOST} !www.domain.com
RewriteRule ^(.*)$ https://www.domain.com$1 [R=301]
RewriteRule ^subpage/(.+)/?$ subpage.html?$1 [NC]
应该改写的
https://www.domain.com/subpage/2 -> https://www.domain.com/subpage.html?2 但它现在只是指向我的 404 文件。
这可能是显而易见的,但我没有看到我的错误。
【问题讨论】:
标签: mod-rewrite apache2