【问题标题】:Multiple hyphen handling in .htaccess (URL rewriting).htaccess 中的多个连字符处理(URL 重写)
【发布时间】:2019-04-16 17:48:51
【问题描述】:

我遇到了 URL 重写的问题。我在 .htaccess 文件中编写了以下规则

RewriteRule ^c-([^/]*)-([^/]*)/$ cat.php?id=$1&slug=$2

它给了我这样的 URL:http://localhost/actuco/c-628Y8x-france/(带有类别的 ID 和它的 slug)。此网址完美运行。

但是,当我输入这个 URL 时:http://localhost/actuco/c-xpS3cc-amerique-du-nord/ 它不再起作用了。问题的原因一定是后一个 slug 包含多个连字符(amerique-du-nord),而第一个(法国)根本不包含连字符(我尝试了所有包含多个连字符的 slug 类别)。

请问有人知道怎么解决吗?

这是我完整的 .htaccess 代码

Options +FollowSymlinks

RewriteEngine On

RewriteBase /actuco/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_URI} /+[^\.]+$
#RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ article.php?lng=$1&yr=$2&mo=$3&dy=$4&slug=$5&total_slug=$6
#RewriteRule ^([^/]*)-([^/]*)-([^/]*)$ waluty.php?cur=$1&amt=$2&lang=$3
RewriteRule ^c-([^/]*)-([^/]*)/$ cat.php?id=$1&slug=$2
RewriteRule ^([^/]*)/([^/]*)/$ url.php?mode=$1&u=$2
RewriteRule ^bio$ o.php [L]

提前致谢!

【问题讨论】:

    标签: .htaccess url-rewriting slug


    【解决方案1】:

    你可以试试:

    RewriteRule ^c\-([\w]+)\-([\w-]+)\/$ cat.php?id=$1&slug=$2
    

    【讨论】:

    • 完美运行,谢谢!你能解释一下反斜杠和“w”代表什么吗?
    • 不客气!当然,\w 是正则表达式中的元字符,它代表每个标准字母和数字以及下划线字符。其他反斜杠用于“转义”功能字符,例如 - 或 /。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多