2012-01-03 27 views
0

在ExpressionEngine中使用類別時,可以將Category URL Indicator觸發字設置爲通過其{category_url_title}加載類別。ExpressionEngine RewriteRule RegEx引發500錯誤

我想從URL中刪除類別「觸發詞」。

這裏是我到目前爲止,與觸發字設置爲「類」:

RewriteRule /products/(.+)$ /products/category/$1 [QSA,L] 

我不是在寫正則表達式的專家,但我做了一點。我99%確定我的RegEx是好的,但是當我試圖在我的.htaccess文件中使用它作爲RewriteRule時,我得到了500錯誤。

我確定這是愚蠢的,但由於某種原因,我沒有看到我的錯誤。我究竟做錯了什麼?


更新:添加重寫規則的^到開頭的固定的500錯誤。

RewriteRule ^/products/(.+)$ /products/category/$1 [QSA,L] 
+1

我通過在此處添加「^」 – Roi 2012-01-03 07:11:00

回答

3

這是不安全的。採取:

/products/a 

正則表達式組匹配a

將重寫爲:

/products/category/a 

該正則表達式匹配再次(此時,該組匹配category/a)。猜猜會發生什麼。

從輸入開始的/products/如果它不是後跟category/,這意味着你想要一個負向前視。此外,QSA標誌是沒有用的,你沒有一個查詢字符串重寫(QSA代表查詢字符串追加):

RewriteRule ^/products/(?!category/)(.+) /products/category/$1 [L] 

另一個(我個人比較喜歡和)的方式來使用它是一個規則之前RewriteCond使用:

RewriteCond %{REQUEST_URI} ^/products/(?!category/) 
RewriteRule ^/products/(.*) /products/category/$1 [L] 
+0

+1來解決此問題。 – anubhava 2012-01-03 09:09:15

+0

我喜歡它!我沒有意識到它會重寫兩遍 - 有道理!謝謝! – Roi 2012-01-03 17:48:20

0

這阿帕奇重寫規則應該爲你做的工作*:

RewriteCond %{REQUEST_URI} ^/products/(?!category/) 
RewriteRule ^/products/(.*) /products/category/$1 [L] 

有了這個地方,你需要硬編碼您的類別裏手動NKS:

{categories backspace="2"} 
    <a href="{site_name}/products/{category_url_title}">{category_name}</a>, 
{/categories} 

將輸出的新類別的URL,你的願望,其中:

http://example.com/products/toys 

否則,如果使用推薦path variable構建分類鏈接時:

{categories backspace="2"} 
    <a href="{path=products/index}">{category_name}</a>, 
{/categories} 

將創建鏈接與網址中的Category URL Indicator

http://example.com/products/C1 
http://example.com/products/category/toys 

其中— 而完全有效 —將創建您的網站canonicalization issues因爲不同的網址,會出現重複的內容對搜索引擎。


*感謝fge的輝煌mod_write規則。