首页 > 解决方案 > 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>

也许代码有问题,我希望有人能指出我正确的位置。谢谢你们!

标签: .htaccessmod-rewrite

解决方案


推荐阅读