【发布时间】: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]
完美地将所有流量重定向到HTTPS 和non-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