2011-06-29 89 views
8

我知道在Stackoverflow上有很多例子,但我仍然錯過了一些東西。htaccess:將舊域名和所有頁面重定向到一個新域

我試圖http://old.domain.com/fr/重定向到http://brand.new-domain.com/fr/以下規則,但是,這並不工作:

# Enable Rewrite Engine 
RewriteEngine On 
RewriteBase/

# Add a trailing slash to paths without an extension 
RewriteCond %{REQUEST_METHOD} !=POST 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$ 
RewriteRule ^(.*)$ $1/ [L,R=301] 

# Redirect domain 
Options +FollowSymLinks 
RewriteCond %{HTTP_HOST} ^old.domain.com [OR] 
RewriteCond %{HTTP_HOST} ^other-old.domain.com [NC] 
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [r=301,L] 

# Remove index.php 
# Uses the "exclude method" 
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Exclude_List_Method 
# This method seems to work best for us, you might also use the include method. 
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method 
# Exclude root files 
RewriteCond $1 !^(index\.php) [NC] 
# Exclude EE folders 
RewriteCond $1 !^(assets|ee-admin|images|templates|themes|fr|nl)/ [NC] 
# Exclude user created folders 
RewriteCond $1 !^(assets|css|img|js|swf|uploads)/ [NC] 
# Exlude favico, robots, ipad icon 
RewriteCond $1 !^(favicon\.ico|robots\.txt|pple-touch-icon\.png) [NC] 
# Remove index.php 
RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC] 
RewriteCond %{QUERY_STRING} !^(URL=.*)$ [NC] 
RewriteRule ^(.*)$ /index.php?/$1 [L] 

當我打電話根URL它正確地重定向,但不是當我調用的頁面。我究竟做錯了什麼?

在此先感謝!

Pv的

回答

9

在編寫mod_rewrite規則時,將按照它們出現的順序應用規則。

要重定向的舊域到一個新的領域,你會希望該規則在你的.htaccesshttpd.conf文件第一後—所有其他規則應該出現。

如果你只想重定向某個目錄,下面的規則將這樣做,同時允許網站的其餘部分正常工作:

<IfModule mod_rewrite.c> 
    RewriteEngine On 

    # Redirect Only Matching Directories 
    RewriteCond %{REQUEST_URI} ^/(fr|fr/.*)$ 
    RewriteRule ^(.*)$ http://brand.new-domain.com/fr/$1 [R=301,L] 
</IfModule> 

如果你想重定向整個網站,以下規則將這樣做:

<IfModule mod_rewrite.c> 
    RewriteEngine On 

    # Redirect Entire Site to New Domain 
    RewriteCond %{HTTP_HOST} ^old.domain.com$ [OR] 
    RewriteCond %{HTTP_HOST} ^other-old.domain.com$ [NC] 
    RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [R=301,L] 
</IfModule> 

如果你關心讓爬蟲知道你的內容感動,並希望使過渡儘可能無縫地,一定要保持301 Redirect標誌I重寫規則。

這將確保用戶和搜索引擎被引導到正確的頁面。


雖然我們關於這個問題,因爲EE 2.2版本的一部分,現在EllisLab「正式」提供了removing index.php from ExpressionEngine URLs有限的技術支持。

只需添加或更新你的代碼下面,確保考慮任何規則,你可能已經到位:

<IfModule mod_rewrite.c> 
    RewriteEngine On 

    # Removes index.php 
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC] 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ /index.php/$1 [L] 

    # If 404s, "No Input File" or every URL returns the same thing 
    # make it /index.php?/$1 above (add the question mark) 
</IfModule> 
3

嘗試使用以下汝可作爲第一個:

# Redirect domain 
Options +FollowSymLinks 
RewriteCond %{HTTP_HOST} ^old.domain.com [OR] 
RewriteCond %{HTTP_HOST} ^other-old.domain.com [NC] 
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [R=301,L] 

還與爲下殼體redirect短形式介意上殼體R

1

您是否嘗試過使用mod_alias簡單的重定向指令(你有一個核心模塊) ,在嘗試hacky-mod-rewrite之前?

我會做一個虛擬主機與ServerName old.domain.com並在此VH我想補充一點的規則:

Redirect /fr http://brand.new-domain.com/fr 

從DOC:

然後用URL路徑開始的任何請求都將返回一個重定向請求到目標URL位置的客戶端。除匹配的URL-Path之外的其他路徑信息將被追加到目標URL。

因此,爲brand.new-domain.com(與ServerName brand.new-domain.com)獲得一個單獨的VirtualHost,並在這一個不設置重定向規則。

如果你仍然想在同一個VirtualHost中處理2個域,那麼你將不得不使用mod重寫,因爲即使RedirectMatch無法檢查查詢中的請求域。

相關問題