【问题标题】:Problem with URL path and .htacces redirectURL 路径和 .htaccess 重定向问题
【发布时间】:2021-02-27 21:41:12
【问题描述】:

我的.htaccess 中有以下代码:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

完美地将所有流量重定向到HTTPSnon-www。但是,如果用户碰巧访问了网站上的页面并包含www,它不会保留完整路径,而是重定向到主页。例如:

www.example.com 转到example.com

但是

www.example.com/page 转到 example.com 而不是 example.com/page

无论使用 HTTPS 还是 HTTP,都会发生这种情况。这与导致问题的www 重定向有关(?)...我只是想不通,我在谷歌上搜索了很多答案都无济于事。

更新

我完整的.htacces 是:

RewriteEngine on

RewriteBase /

# block text files in the content folder from being accessed directly
RewriteRule ^content/(.*)\.(txt|md|mdown)$ index.php [L]

# block all files in the site folder from being accessed directly
# except for requests to plugin assets files
RewriteRule ^assets/plugins/([a-zA-Z0-9\.\-_%=]+)/(.*)$ site/plugins/$1/assets/$2 [L,N]
RewriteCond $1 !^plugins/[a-zA-Z0-9\.\-_%=]+/assets/.*
RewriteRule ^site/(.*) index.php [L]

# block direct access to kirby and the panel sources
RewriteRule ^(kirby|panel\/app|panel\/tests)/(.*) index.php [L]

# make panel links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^panel/(.*) panel/index.php [L]

# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]

# HTTP to HTTPS and WWW to non WWW
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

【问题讨论】:

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


    【解决方案1】:

    请根据您显示的示例尝试以下操作。请将这些规则置于所有规则之上,并确保在测试之前清除浏览器缓存。

    RewriteEngine ON
    ##For www requests changing to https.
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
    
    ##For non-www requests changing to https.
    RewriteCond %{HTTPS} !on
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 2020-10-01
      • 2020-12-03
      • 2016-07-02
      • 2013-07-09
      • 1970-01-01
      • 2011-05-10
      相关资源
      最近更新 更多