.htaccess - htaccess 文件中的多个重写规则导致冲突
问题描述
我一直在为我正在创建的模块制定一套新的 htaccess 重写规则。htaccess 文件已经有一些基于 PHP 的应用程序的规则,这些规则现在被打破了。我想知道是否有办法解决这个问题。这是代码:
# My Module Rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*?)/?$ ./page.php?url=$1 [L]
RewriteCond %{THE_REQUEST} /page\.php\?url=([^\&\ ]+)
RewriteRule ^/?page\.php$ ./%1/? [R=301, L]
</IfModule>
# Application Rules
<IfModule mod_rewrite.c>
RewriteEngine on
# RewriteBase is set to "/" so rules do not need updating if the
# installation directory is relocated. It is imperative that
# there is also a RewriteCond rule later that can effectively get
# the actual value by comparison against the request URI.
#
# If there are _any_ other RewriteBase directives in this file,
# the last entry will take precedence!
RewriteBase /
# Redirect directories to an address with slash
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]
# Send all remaining (routable paths) through index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Determine and use the actual base
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^.*$ %2index.php [QSA,L]
</IfModule>
我上面的规则适用于我的模块 URL:
https://www.example.com/page.php?url=test-page => https://www.example.com/test-page/
但是,它破坏了主应用程序的重写规则,这是一个示例:
https://www.example.com/admin/setup => 404 错误
我试图重新安排规则,但是我的重写不起作用。我也试图不编辑应用程序的规则,因为这些是使用它的人的标准。
我尝试添加另一个条件来检查请求的 URL 是否包含“page.php”,但它没有解决问题。这是我使用的代码:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/?page\.php$
RewriteRule ^/?(.*?)/?$ ./page.php?url=$1 [L]
RewriteCond %{THE_REQUEST} /page\.php\?url=([^\&\ ]+)
RewriteRule ^/?page\.php$ ./%1/? [R=301, L]
</IfModule>
也许代码有问题,我希望有人能指出我正确的位置。谢谢你们!
解决方案
推荐阅读
- apache-spark - Spark SQL 中 DISTINCT 与 GROUP BY 的重复删除
- amazon-web-services - 使用 Terraform (AWS) 进行标记时缺少逻辑
- php - 显示来自数据库 PHP 的图像
- javascript - 无法让 cytoscape undo-redo 正常工作
- perl - 为什么 CPU 端版本 SDL_FillRect 比 GPU 端版本 SDL_RenderFillRect 快 7 倍?
- java - 像在三星时钟应用程序中一样更改振动声音
- c# - 如何将 Bing 地图组件添加到我的 C# Winforms 应用程序?
- python - Pb 从下拉列表中选择文本
- c++ - 模板和重载之间的交互
- javascript - JQuery:将数组转换为函数参数