重写与特定模式匹配的 URL

Rewriting a url that matches a specific pattern

提问人: 提问时间:1/10/2023 最后编辑:RavinderSingh13 更新时间:1/10/2023 访问量:43

问:

我有一个包含以下行的文件.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]

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

apache .htaccess mod-rewrite 重写friendly-url

评论


答:

1赞 RavinderSingh13 1/10/2023 #1

对于您显示的示例,请尝试以下 .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]