【问题标题】:APACHE SSL WWW redirect to real nameAPACHE SSL WWW 重定向到实名
【发布时间】:2019-02-04 22:35:46
【问题描述】:

在 .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

访问以上网址时如何保留https://www.example.com/dome.htm

【问题讨论】:

    标签: apache .htaccess url-rewriting url-rewrite-module


    【解决方案1】:

    简单:

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

    【讨论】:

      【解决方案2】:

      需要更改处理顺序:

      # 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... RewriteRules 从上到下处理。当然,您指定了 [L] 标志,但这只会结束重写 _for 该迭代!如果最后一次迭代更改了请求(执行了重写),则 另一个 迭代由 http 服务器完成。

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

      【讨论】:

        猜你喜欢
        • 2015-11-02
        • 1970-01-01
        • 2010-11-09
        • 2014-11-24
        • 2018-11-16
        • 1970-01-01
        • 2012-04-14
        • 2016-12-30
        相关资源
        最近更新 更多