.htaccess 重写与特定模式匹配的url

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

我有一个包含以下行的.htaccess文件

# ErrorDocument 404 /error/404.php

Options All -Indexes -MultiViews

RewriteEngine On

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

# Silent Redirect from any url ending with mcc-* to show.php?id=mcc-*
# This is the portion that isn't working
RewriteCond %{REQUEST_URI} (mcc-[\d]+)\.php$
RewriteCond %{REQUEST_URI}/$1.php -f [NC]
RewriteRule show.php?id=$1 [L]

我试图找到任何以模式(mcc-[\d]+)结尾的网址,并将其重定向到show.php?id=%pattern%,但试图访问与此模式匹配的页面只会返回404错误,因为没有文件mcc-*

oxcyiej7

oxcyiej71#

请尝试使用下面的.htacess规则文件。此代码将在内部重写为show.php文件。请确保:

  • 将.htaccess文件与show.php文件放在沿着。
  • 请确保在测试URL之前清理浏览器缓存。
RewriteEngine ON

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

## Rules for internal rewrite here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(mcc-.*)/?$ show.php?id=$1 [QSA,NC,L]

相关问题