簡體   English   中英

htaccess:將舊域和所有頁面重定向到新域

[英]htaccess: redirect old domain and all pages to a new domain

我知道 Stackoverflow 上有很多例子,但我仍然錯過了一些東西。

我正在嘗試使用以下規則將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 時,它會正確重定向,但在調用頁面時不會。 我究竟做錯了什么?

提前致謝!

光伏

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

要將舊域重定向到新域,您需要將該規則放在.htaccesshttpd.conf文件中的第一位——所有其他規則都應出現在它之后。

如果你只想重定向某個目錄,下面的規則會這樣做,同時允許站點的rest正常到function:

<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>

如果您希望讓爬蟲知道您的內容已移動並希望盡可能無縫地進行轉換,請確保在 RewriteRule 中保留301 Redirect標志。

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


雖然我們正在討論這個主題,但作為 EE 2.2 版本的一部分,EllisLab 現在“正式”提供了從 ExpressionEngine URLs 中刪除 index.php 的有限技術支持。

只需將您的代碼添加或更新為以下內容,確保考慮您可能已經制定的任何規則:

<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>

嘗試使用以下 ruke 作為第一個:

# 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]

還要注意大寫R是小寫redirect的縮寫形式。

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

我會用ServerName old.domain.com做一個 VirtualHost,在這個 VH 中我會添加這個規則:

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

來自文檔:

那么任何以 URL-Path 開頭的請求都會在目標 URL 的位置向客戶端返回一個重定向請求。 匹配的 URL-Path 之外的其他路徑信息將附加到目標 URL。

因此,為 brand.new-domain.com (使用ServerName brand.new-domain.com )獲取一個單獨的虛擬主機,並且在此不設置重定向規則。

如果您仍想在同一個 VirtualHost 中處理 2 個域,那么您將不得不使用 mod-rewrite,因為即使 RedirectMatch 也無法檢查查詢中的請求域。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM