【问题标题】:Apache Mod rewrite not work for me When use (-) in url?Apache Mod 重写对我不起作用在 url 中使用 (-) 时?
【发布时间】:2015-08-22 18:41:54
【问题描述】:

Apache Mod 重写不适合我在 url 中使用 (-) 时?

我试图访问页面

www.example.com/products/ipod/white-ipod-8gb/12345678

它不起作用,我如何编辑.htaccess 文件?

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^products/([^-]*)/([^-]*)/([^-]*)$ /products.php?produucts_name=$1&products_des=$2&products_id=$3 [L]
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]    
ErrorDocument 404 /404.php
ErrorDocument 403 /403.php
Options -indexes

【问题讨论】:

  • - 字符在正则表达式模式中具有特殊含义。您需要使用反斜杠将其转义,例如 \-

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

那是因为您在正则表达式中使用了[^-]。使用此代码:

ErrorDocument 404 /404.php
ErrorDocument 403 /403.php
Options -indexes
RewriteEngine on

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]    

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ $1.php [L]

RewriteRule ^products/([^/]+)/([^/]+)/([^/]*)$ /products.php?produucts_name=$1&products_des=$2&products_id=$3 [L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 2015-07-26
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多