首页 > 解决方案 > URL 路径和 .htacces 重定向问题

问题描述

我的代码中有以下代码.htaccess

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

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

www.example.comexample.com

www.example.com/pageexample.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.htaccessurlmod-rewriteurl-rewriting

解决方案


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

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]

推荐阅读