2017-07-27 58 views
14

所以我一直在這個小時,但不能解決它。在stackoverflow沒有解決我的問題。問題是,我有一個重定向規則在我的.htaccess如下.htaccess內部重定向不能使用字符串參數

RewriteRule ^store-category/(.+)/?$ /wordpress/store-category/?cid=$1 [QSA,L] 

當CID是一個數字,但是當使用像一個字符串「我的鑰匙」它不工作這重定向工作。關鍵是我必須改變這個規則。

RewriteRule ^store-category/(.+)/?$ /wordpress/store-category/?cid=$1 [R,L] 

但是我不想使用它,因爲這會重定向我的用戶和url更改,我不想要。任何人都可以請解釋爲什麼這個東西適用於數字而不是字符串參數。任何幫助,將不勝感激。

編輯:完整的.htaccess

# BEGIN WordPress 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase /wordpress/ 
RewriteRule ^index\.php$ - [L] 
RewriteRule ^store-category/(.+)/?$ /wordpress/store-category/?cid=$1 [QSA,L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /wordpress/index.php [L] 
</IfModule> 

# END WordPress 
+0

這是該只有在您的.htaccess文件中的規則? –

+0

沒有。這是一個標準的wordpress .htaccess與這個額外的規則 – shazyriver

+0

是否有可能發生重定向,但接收請求的腳本期望的數字不是字符串,因此失敗? –

回答

-1

用這個,這是成功地爲我工作,我希望它會爲你的作品。

RewriteEngine on 
IndexIgnore * 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond $1 !^(index\.php|img|robots\.txt) 
RewriteRule ^(.*)$ index.php/$1 [L] 
RewriteRule ^wordpress/index.php/.*$ - [F,L,NC] 
+0

你能解釋它在做什麼?我必須在wordpress環境中使用它。如果不知道它的作用,我不能放任何東西。謝謝。 – shazyriver

+0

@shazyriver **的RewriteCond%{} REQUEST_FILENAME -f ** **的RewriteCond%{} REQUEST_FILENAME -d ** **的RewriteCond $ 1→(索引\ .PHP!|!IMG |機器人\ .txt)** ...表示如果瀏覽器中指定名稱的文件不存在,或瀏覽器中的目錄不存在,並且頁面不是index.php,則img文件夾或robots.txt。然後進入下面的重寫規則。 最後一個RewriteRULE是實際重定向 –

+0

但我沒有看到我放在那裏的規則 – shazyriver

0

正則表達式是貪婪的(從左到右)。你可能會拉其他東西。我發現最簡單的方法是不使用URL類(很少會匹配所有內容)。

RewriteRule ^store-category/([^\/]+)/?$ store-category/?cid=$1 [QSA,L] 

您可能還可以簡化這一點,取決於您希望如何處理可能商店類別ID後畸形的URL。

RewriteRule ^store-category/([^\/]+) store-category/?cid=$1 [QSA,L] 

編輯:你說得對。我沒有測試整個htaccess。你在rewritebase和規則中再次指定wordpress路徑兩次。我已經更新了上述規則,以及在測試人員提供了一個鏈接到更新的解決方案:

http://htaccess.mwl.be?share=116de701-cf8a-577c-bab5-5c357d844452

+0

這仍然不影響結果 – shazyriver

+0

非常感謝。但我沒有寫這個規則。我用wordpress函數自動生成它。並使用一個數字它的作品。所以我不認爲這與wordpress路徑兩次的原因有什麼關係。 – shazyriver

+0

非常感謝。但我沒有寫這個規則。我用wordpress函數自動生成它。並使用一個數字它的作品。所以我不認爲這與wordpress路徑兩次的原因有什麼關係。 – shazyriver

1

我做了一些嘗試當地,發現我的作品,如果我把這兩個規則:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

即使是行之前

RewriteRule ^store-category/(.+)/?$ /wordpress/store-category/?cid=$1 [QSA,L] 

完全.htaccess文件將是:

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase /wordpress/ 

RewriteRule ^index\.php$ - [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^store-category/(.+)/?$ /wordpress/store-category/?cid=$1 [QSA,L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /wordpress/index.php [L] 
</IfModule> 

。不管怎麼樣,(.+)/?$,最終尾隨斜線將從組被捕獲,並會在cid參數。如果類別內沒有斜槓,最好使用([^/]+)/?$

更新:瞭解在您的重定向上發生了什麼,啓用日誌(請參閱http://httpd.apache.org/docs/current/mod/mod_rewrite.html)。在我主我啓用mod_rewrite.c:trace3,我得到以下日誌中error_log(與rewrite只是跟蹤線)

[Fri Aug 04 12:48:56.904099 2017] [rewrite:trace3] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] strip per-dir prefix: /var/www/localhost/htdocs/wordpress/store-category/my-id -> store-category/my-id 
[Fri Aug 04 12:48:56.904123 2017] [rewrite:trace3] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] applying pattern '^index\\.php$' to uri 'store-category/my-id' 
[Fri Aug 04 12:48:56.904129 2017] [rewrite:trace3] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] strip per-dir prefix: /var/www/localhost/htdocs/wordpress/store-category/my-id -> store-category/my-id 
[Fri Aug 04 12:48:56.904132 2017] [rewrite:trace3] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] applying pattern '^store-category/(.+)/?$' to uri 'store-category/my-id' 
[Fri Aug 04 12:48:56.904143 2017] [rewrite:trace2] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] rewrite 'store-category/my-id' -> '/wordpress/store-category/?cid=my-id' 
[Fri Aug 04 12:48:56.904147 2017] [rewrite:trace3] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] split uri=/wordpress/store-category/?cid=my-id -> uri=/wordpress/store-category/, args=cid=my-id 
[Fri Aug 04 12:48:56.904151 2017] [rewrite:trace2] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] trying to replace prefix /var/www/localhost/htdocs/wordpress/ with /wordpress/ 
[Fri Aug 04 12:48:56.904155 2017] [rewrite:trace2] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] trying to replace context docroot /var/www/localhost/htdocs with context prefix 
[Fri Aug 04 12:48:56.904158 2017] [rewrite:trace1] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] internal redirect with /wordpress/store-category/ [INTERNAL REDIRECT] 

(其次是一些其他記錄,同時它試圖找到默認文檔index.php

+0

沒有爲我工作:( – shazyriver

+0

@shazyriver你可以訪問Apache日誌並激活日誌記錄mod_rewrite?(請參閱http://httpd.apache.org/docs/current/mod/mod_rewrite.html) – Urban

+0

但是我編輯哪個文件來啓用日誌? – shazyriver