2017-09-05 120 views
0

我在設置重定向時遇到了問題。我在某些目錄中有索引頁,目錄名稱是簡單的六個字符長的標記,如ABCDEF。名稱是字母數字和全部大寫字母。我最初想要的是將包含令牌的所有流量,小寫和大寫指向正確的目錄。 我沒有訪問服務器模塊,如mod_speling,因此我使用php腳本來做實際的url重寫。有用。 但後來我想添加一個步驟,直接所有的流量,如:.htaccess有條件重寫/重定向

example.com/ABCDEF 
example.com/abcdef 
example.com/reference/abcDEF 
example.com/referENces/ABCdeF 

到以下目錄:

example.com/references/ABCDEF/ 

我不能讓這些工作,我得到重定向在循環中的所有時間。

我有我的htaccess什麼:

# takes care of: example.com/abcdef 
RewriteCond %{DOCUMENT_ROOT}/references/%{REQUEST_URI} !-d 
RewriteCond %{REQUEST_URI} ^(/?)([a-zA-Z0-9]{6})(/?)$ 
RewriteRule ^(/?)([a-zA-Z0-9]{6})(/?)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI} [QSA,L,NC,R=302] 

# takes care of example.com/reference/abcDEF 
RewriteCond %{REQUEST_URI} ^(/?)([REFNCS?refncs?]{9})/([a-zA-Z0-9]{6})(/?)$ 
RewriteRule ^(/?)([REFNCS?refncs?]{9})/([a-zA-Z0-9]{6})(/?)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI} [QSA,L,NC,R=302] 

# this should take care of: example.com/references/abcDEF , but if I uncomment it, we end up in a loop 
#RewriteCond %{REQUEST_URI} ([REFNCSrefncs]{10})/([a-zA-Z0-9]{6})(/?)$ 
#RewriteRule ([REFNCSrefncs]{10})/([a-zA-Z0-9]{6})(/?)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI} [QSA,L,NC,R=302] 

# Finally this handles: example.com/ABCDEF 
RewriteCond %{DOCUMENT_ROOT}/references/%{REQUEST_URI} -d 
RewriteCond %{REQUEST_URI} ^(/?)([a-zA-Z0-9]{6})(/?)$ 
RewriteRule ^(/?)([a-zA-Z0-9]{6})(/?)$ example.com/references%{REQUEST_URI} [QSA,L,NC,R=302] 

在這一點上,我相信一定有一個更好的,更普遍的正則表達式的解決方案。第二塊和第三塊可以以某種方式合併。

彼得

+0

爲什麼這些不同的'[REFNCS?refncs?] {9}'和'[REFNCSrefncs] {10}' –

+0

確實,第一個不需要s和S. –

回答

0

嘗試改變:

RewriteCond %{REQUEST_URI} ([REFNCSrefncs]{10})/([a-zA-Z0-9]{6})(/?)$ 
RewriteRule ([REFNCSrefncs]{10})/([a-zA-Z0-9]{6})(/?)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI} [QSA,L,NC,R=302] 

要:

RewriteCond %{REQUEST_URI} ^(\/?)([REFNCSrefncs]{10})\/([a-zA-Z0-9]{6})(\/?)$ 
RewriteRule ^(\/?)([REFNCSrefncs]{10})\/([a-zA-Z0-9]{6})(\/?)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI} [QSA,L,NC,R=302] 
+0

它沒有傷害,沒有循環。但仍然不匹配: example.com/references/abcdeF –

+0

我改回了'[REFNCSrefncs] {10}'。 Plus逃脫了'/' –

0

看起來是這樣工作的:

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-f 
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-d 
RewriteCond %{REQUEST_URI} ^(/?)([REFNCSrefncs]{10})/([a-zA-Z0-9]{6})(/?)$ 
RewriteRule ^(.*)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI} [QSA,L,NC,R=302] 

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-f 
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-d 
RewriteCond %{REQUEST_URI} ^(/?)([REFNCrefnc]{9})/([a-zA-Z0-9]{6})(/?)$ 
RewriteRule ^(.*)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI} [QSA,L,NC,R=302] 

我不得不改變的([REFNCSrefncs順序] {10})和([REFNCrefnc] {9} )。

感謝您的提示!