在.htaccess中链接多个所需重写

brvekthn  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(97)

我目前有2重写应该发生在任何给定的网址,
第一个将匹配模式%DOMAIN%/mcc-*的任何url重定向到show.php?id=mcc-*,第二个将不带.php扩展名的任何url重定向到页面,就像它有扩展名一样。
第一次重写被调用并工作,但第二次没有。
我试过交换重写的位置,但结果是一样的,'AddExtension'重写从来没有被调用过,即使在没有重定向到show.php的页面上也是如此

ErrorDocument 404 /error/404.php

Options All -Indexes -MultiViews

RewriteEngine On

# Silent Redirect from any url ending with mcc-* to show.php?id=mcc-*
# This works as intended.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(mcc-[\d]+)/?$ show.php?id=$1 [QSA,NC]

# Allow urls to not include the .php extension
# This is not not being called.
RewriteBase /
RewriteCond %{REQUEST_URI}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

有没有办法让这两个条件都运行,或者如果第一个条件不匹配,至少运行第二个条件?

f0brbegy

f0brbegy1#

请使用您显示的示例尝试以下.htaccess规则文件。请确保在测试URL之前清除浏览器缓存。

ErrorDocument 404 /error/404.php
Options All -Indexes -MultiViews

RewriteEngine ON

# Allow urls to not include the .php extension
# This is not not being called.
RewriteBase /

RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

# Silent Redirect from any url ending with mcc-* to show.php?id=mcc-*
# This works as intended.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(mcc-[\d]+)/?$ show.php?id=$1 [QSA,NC]

相关问题