APACHE SSL WWW 重定向到真实姓名



在.htaccess文件中:

# Enable mod_rewrite
RewriteEngine on
# Rewrite file name
RewriteRule ^dome.htm$ dome.php [L,QSA]

# Force www
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Force SSL
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

强制SSL和WWW工作良好,除了重写文件名,其中从以下URL发出的请求返回到真实的文件名,https://www.example.com/dome.php

example.com/dome.htm
www.example.com/dome.htm
https://example.com/home.htm

访问上述 URL 时如何保持https://www.example.com/dome.htm

您需要更改处理顺序:

# Enable mod_rewrite
RewriteEngine on
# Force www
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Force SSL
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# Rewrite file name
RewriteRule ^dome.htm$ dome.php [L,QSA]

另请注意该RewriteRule中略微修改的正则表达式;-(

为什么这会有所作为?想象一下,一个传入的请求被重写...按照您的规则顺序,请求首先被重写为/dome.php然后重定向到https://%{HTTP_HOST}%{REQUEST_URI},这将指示浏览器从该新 URL 加载/dome.php...重写规则从上到下进行处理。当然,您指定了[L]标志,但这只会结束该迭代_for重写!如果上次迭代更改了请求(执行了重写(,则 http 服务器将完成另一次迭代。

另一种方法是保留规则顺序,但使用[END]标志而不是[L]它做你想要的。但它仅在较新版本的 apache http 服务器上受支持,从版本 2.4 开始。

简单:

RewriteRule    ^dome.htm?$    dome.php [NC,L]    # Handle requests for "dome"

最新更新