2013-02-02 141 views
1

我已經使用Apache很長一段時間了(很長時間以來),即使如此,我沒有做太多的URL重寫或類似的東西,只是簡單的託管。但是現在我正在嘗試爲一個重新命名爲新域的小企業拼湊一個簡單的重定向。重定向域上的所有請求到特定的URL

它的設置方式是舊域的主機有一個基於Web控制面板的重定向到一個特定的URL,這是一個「尋找舊我們?」頁面上的新域名。所有的請求都會被重定向,但是它們會攜帶整個請求路徑,從而在新網站上產生404。

我一直在瀏覽一些Apache文檔和一些我可以在網上找到的例子,但我還沒有完成。在那裏我已經離開了,到目前爲止是這樣的:

RewriteCond %{REQUEST_URI} .*looking-for-blah.* [NC] 
RewriteRule^http://newsite.com/looking-for-blah [L,R=301] 

的想法是,任何請求進來爲包含looking-for-blah,任何路徑,無論之前或之後有什麼,應該去明確http://newsite.com/looking-for-blah 。所以,當舊主機重定向某人:

http://newsite.com/looking-for-blah/foo/baz 

他們得到新的網站重定向到:

http://newsite.com/looking-for-blah 

然而,它似乎並沒有被捕獲傳入的請求並重定向他們。我錯過了RewriteCond中的一些基本概念嗎?也許有更好的方法來做到這一點,我甚至沒有考慮過?

編輯:這裏是的.htaccess的當前狀態作爲一個整體:

# BEGIN WordPress 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php [L] 
</IfModule> 

# END WordPress 
# BEGIN WordPress 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php [L] 
</IfModule> 

# END WordPress 

# BEGIN custom redirect 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteRule looking-for-icamp http://empow.me/looking-for-icamp [L,R=301] 
</IfModule> 
# END icamp redirect 

但做一個簡單的wgethttp://empow.me/looking-for-icamp/foo出現了404,而不是期望301

+0

'RewriteCond'甚至沒有必要。你可以這樣做:'RewriteRule look-for-blah http://newsite.com/looking-for-blan [L,R = 301]'(Apache不需要'。*',因爲它會匹配一個子串如果不是'^ $'錨定),但無論如何你應該工作。你是否錯過了'RewriteEngine On'來初始化它? –

+0

@MichaelBerkowski:這絕對看起來更簡單,但它在這種情況下也沒有伎倆。我不禁想知道其他事情是否也在阻礙之中。我已經更新了關於'.htaccess'的更多信息以及驗證的實際URL。 – David

+0

哦,Wordpress是參與其中。移動你的規則_before_ wordpress規則。否則,它可能匹配'RewriteRule。 /index.php [L]'並被髮送到WP路由。 (你只需要一個''圍繞着所有東西,也只有一個'RewriteEngine On'和'RewriteBase') –

回答

2

WordPress的默認的全部路由匹配您的規則,因此您的規則將需要被放置以上任何WordPress的重寫。我還添加了一個RewriteCond,以便比您的.+技巧更清楚地說明避免循環重寫的問題,這對我來說看起來有點難以理解,並且在以後的閱讀中很難理解。

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/

# BEGIN custom redirect 

# This must take place before the Wordpress redirect to index.php 
# Added condition to avoid circular rewrite 
RewriteCond %{REQUEST_URI} !^/looking-for-icamp$ 
RewriteRule looking-for-icamp http://empow.me/looking-for-icamp [L,R=301] 
# END icamp redirect 

# Note - you had two identical WP blocks. I've removed one. 

# BEGIN WordPress 
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# This rule was the one blocking your custom rule earlier.... 
RewriteRule . /index.php [L] 

# END WordPress 
</IfModule> 
相關問題