2015-06-08 58 views
2

Apache Mod重寫不適用於我在URL中使用( - )時?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 
+0

「-'字符在正則表達式模式中有特殊含義。你需要像'\ -'那樣用反斜槓來轉義它 – marekful

回答

2

那是因爲你在你的正則表達式使用[^-]。使用此代碼:

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]