2012-12-06 23 views
2

我有一個Apache的配置,具有多個重寫規則,以獲得最可愛的URL重定向,避免重複的搜索引擎優化,等這裏有一個片斷爲例(它的功能多了很多):如何結合Apache重定向?

# Redirecting non-www to www 
RewriteCond %{HTTP_HOST} ^example.com 
RewriteRule (.*) http://www.example.com/$1 [R=301,L] 

# Removing index.php 
RewriteCond %{REQUEST_URI} index\.php$ [NC] 
RewriteRule .*/[R=301,L] 

# A big bunch of these 
Redirect permanent /old-article.html http://www.example.com/new-article.html 
Redirect permanent /another-old-article.html http://www.example.com/new-article2.html 

這很好,但它恰好產生了很多重定向。一個常見情況如下:

http://example.com/index.php, 301 redirect to http://www.example.com/index.php 
http://www.example.com/index.php, 301 redirect to http://www.example.com 

它有時會達到4-5重定向。

現在,我想所有這些規則被鏈接並只產生一個301重定向,像這樣:

http://example.com/index.php, 301 redirect to http://www.example.com 

我知道我可以花一個下午的時間思考和排序規則,以更好的匹配,也我可以創建組合規則。但是這會使已經很長的文件變得複雜。我想要一個標誌,操作數或任何可以執行所有規則的內容,就好像它們在內部所在的位置一樣,只有在每條規則都被抓取後才發出重定向。這甚至有可能嗎?

回答

0

看來,如果簡單地重新排序,這將得到你想要的東西:

# A big bunch of these 
Redirect permanent /old-article.html http://www.example.com/new-article.html 
Redirect permanent /another-old-article.html http://www.example.com/new-article2.html 

# Removing index.php 
RewriteRule ^/index.php http://www.example.com/ [R=301,L] 

# Redirecting non-www to www 
RewriteCond %{HTTP_HOST} ^example.com 
RewriteRule (.*) http://www.example.com/$1 [R=301,L] 

example.com域老款的直接請求:

http://example.com/old-article.html 

將導致單個重定向到:

http://www.example.com/new-article.html 

一種用於任一http://example.com/index.php或請求http://www.example.com/index.php將導致一個重定向到:

http://www.example.com/ 

不符合任何東西都不會導致從一個單一的重定向請求:

http://example.com/foo 

要:

http://www.example.com/foo 

這似乎涵蓋了所有的基礎。我錯過了什麼?

0

從您的RewriteRule中刪除[L]標誌,它們將自動合併。