【问题标题】:.htaccess problems with https redirecthttps 重定向的.htaccess 问题
【发布时间】:2018-05-07 06:39:37
【问题描述】:

我刚刚将我的网站从 HTTP 升级到 HTTPS,而我的 .htaccess 之前运行良好,现在却表现得很奇怪。我有以下内容:

  • 删除尾部斜杠
  • 删除php扩展

...加上其他,但我想这些是有问题的。

在示例页面上,我得到的行为是:

我是否遗漏了一些明显的东西?代码是:

Order Deny,Allow

DirectoryIndex index.php

# Secure text files
<Files ~ "\.txt$">
  Order allow,deny
  Deny from all
</Files>

# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

ErrorDocument 404 /404.php

RewriteEngine On

# All calls go to SSL
RewriteEngine On
RewriteCond %{ENV:HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ https://www.navanter.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ https://www.navanter.com/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    对于这个特定问题,您用于删除尾随 /.php 扩展名的重写规则不需要替换中的完整 URL。您也可以考虑重新排序其中一些规则。

    更新示例:

    Require all granted
    
    DirectoryIndex index.php
    
    # Secure text files
    <Files ~ "\.txt$">
      Require all denied
    </Files>
    
    ErrorDocument 404 /404.php
    
    RewriteEngine On
    
    # Redirect HTTP to HTTPS and non-www to www
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
    
    # Unless directory, remove trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ /$1 [R=301,L]
    
    # Redirect external .php requests to extensionless url
    RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
    RewriteRule ^ /%1 [NC,R=301,L]
    
    # Resolve .php file for extensionless php urls
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+)$ /$1.php [NC,L]
    

    这里有几点需要注意:

    • 上面的指令被修改为使用更新的 apache 2.4 语法
    • RewriteEngine on 指令应位于其他重写规则之前
    • HTTP -&gt; HTTPSnon-www -&gt; www 重定向逻辑可以在一个操作中完成
    • PHP 扩展剥离和随后的重新映射应在没有完整 URL 的情况下完成,以避免您观察到的重复行为。最后一点应该可以解决问题。

    如果您需要有关此处使用的某些语法的更多信息,可以查看mod_rewrite 文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      • 2016-09-14
      相关资源
      最近更新 更多