【问题标题】:Rewriting a url that matches a specific pattern重写匹配特定模式的 url
【发布时间】:2023-01-10 14:33:12
【问题描述】:

我有一个包含以下行的 .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]+) 结尾的任何 url 并将其重定向到 show.php?id=%pattern%,但是尝试访问与此模式匹配的页面只会返回 404 错误,因为没有文件 mcc-*

【问题讨论】:

  • 所以你在浏览器上点击http://localhost:80/mcc-blablabla-etc这样的URL,你想把它重定向到http://localhost:80/show.php?id=mcc-blablabla-etc?请确认一次。

标签: apache .htaccess mod-rewrite


【解决方案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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    相关资源
    最近更新 更多