apache - 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]
它将所有流量完美地重定向到HTTPS
and 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]
解决方案
您能否根据您显示的示例尝试以下操作。请将这些规则置于所有规则之上,并确保在测试这些规则之前清除浏览器缓存。
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]
推荐阅读
- sql - 在 XML XPATH POSTGRES 中获取带有子节点的订单节点
- amazon-web-services - presto - 获取最近一个星期六的日期
- python - 在python中拆分多词标签的有效方法
- php - 使用 C 调用未定义函数 collect() 时出错
- javascript - 在没有内置函数的情况下更改字符串中字母的大小写
- javascript - 从地图循环中的父组件中删除子组件
- database - 使用 hasura 使用现有数据库进行 Flyway 迁移 - PostgresSQL 备份
- android - Android Jetpack Compose 从 Composable 内部的 Fragment 获取 Activity View Model
- c# - 如何使用 .Net 连接到 WinDSX 服务器以便查询数据库?
- opengl - 当变量未声明为统一时,编译错误显示“赋值给只读变量”