2017-01-17 21 views
0

我已經學習並編寫了一些.htaccess規則,有些正在執行完美。但也有一些是沒有得到執行,並顯示錯誤或404在特定的URL重寫規則中獲取404

這些規則

RewriteEngine on 

# index.php?store=xyz (executing perfectly) 
RewriteCond %{QUERY_STRING} store=([^&]+)&? 
RewriteRule /?index.php$ /%1 [END,R=301] 

RewriteRule ^/?([a-zA-Z0-9]+)$ index.php?store=$1 [END] 
RewriteRule ^/?([a-zA-Z0-9]+)/products$ index.php?store=$1&view=products [END] 
RewriteRule ^/?([a-zA-Z0-9]+)/products/([0-9]+)$ index.php?store=$1&view=products&category=$2 [END] 
RewriteRule ^/?([a-zA-Z0-9]+)/products/([0-9]+)$ index.php?store=$1&view=sales&sale=$2 [END] 
RewriteRule ^/?([a-zA-Z0-9]+)/single/([0-9]+)$ index.php?store=$1&view=single&product=$2 [END] 

# index.php?store=xyz&view=products(executing perfectly) 
RewriteCond %{QUERY_STRING} store=([^&]+)&? 
RewriteCond %{QUERY_STRING} view=products&? 
RewriteRule /?index.php$ /%1/products [END,R=301] 

# index.php?store=xyz&view=products&category=123(executing perfectly) 
RewriteCond %{QUERY_STRING} store=([^&]+)&? 
RewriteCond %{QUERY_STRING} view=products&? 
RewriteCond %{QUERY_STRING} category=([^&]+)&? 
RewriteRule /?index.php$ /%1/products/%3 [END,R=301] 

# index.php?store=xyz&view=sales (error 404) 
RewriteCond %{QUERY_STRING} store=([^&]+)&? 
RewriteCond %{QUERY_STRING} view=sales&? 
RewriteRule /?index.php$ /%1/sales [END,R=301] 

# index.php?store=xyz&view=sales&sale=123 (error 404) 
RewriteCond %{QUERY_STRING} store=([^&]+)&? 
RewriteCond %{QUERY_STRING} view=sales&? 
RewriteCond %{QUERY_STRING} sale=([^&]+)&? 
RewriteRule /?index.php$ /%1/sales/%3 [END,R=301] 

# index.php?store=xyz&view=single&product=123(executing perfectly) 
RewriteCond %{QUERY_STRING} store=([^&]+)&? 
RewriteCond %{QUERY_STRING} view=single&? 
RewriteCond %{QUERY_STRING} product=([^&]+)&? 
RewriteRule /?index.php$ /%1/single/%3 [END,R=301] 

能否請你告訴我,我可能做錯了什麼?

+0

查看代碼!我寫了'(完美執行)'和'(錯誤404)',它們的規則寫在下面 –

回答

0

您從

的index.php?商店= XYZ &視圖=單&產品客戶端重定向= 123

/%1 /單/% 3

a第二你有一個相應的RewriteRule

RewriteRule ^/?([a-zA-Z0-9]+)/single/([0-9]+)$ index.php?store=$1&view=single&product=$2 [END] 

也從

的index.php?商店= XYZ &視圖=銷售&銷售= 123

重定向客戶端

/%1 /銷售/%3

沒有相應RewriteRule,只有兩個

RewriteRule ^/?([a-zA-Z0-9]+)/products/([0-9]+)$ index.php?store=$1&view=products&category=$2 [END] 
RewriteRule ^/?([a-zA-Z0-9]+)/products/([0-9]+)$ index.php?store=$1&view=sales&sale=$2 [END] 

因此,也許改變的 「產品」 的規則之一, 「銷售」 修復您的直接的問題。


雖然,你應該知道重定向規則沒有做,你可能會想到。

RewriteCond %{QUERY_STRING} store=([^&]+)&? 
RewriteCond %{QUERY_STRING} view=single&? 
RewriteCond %{QUERY_STRING} product=([^&]+)&? 
RewriteRule /?index.php$ /%1/single/%3 [END,R=301] 

有三個RewriteCond,不符合%1%3在你的重寫規則,只有%1是有效的,看到RewriteRule一個解釋

除了純文本,替換字符串可以包括

  1. ...

  2. 反向引用(%N)來最後匹配的RewriteCond模式

兼得%1%3,你必須捕捉部分在過去RewriteCond,例如

RewriteCond %{QUERY_STRING} store=([^&]+)&view=(single)&product=([^&]+) 
RewriteRule ... 

的解決方案來捕獲多個部分見another answerRewriteCond to match query string parameters in any order