2013-01-11 105 views
4

服務器默認重定向誰example.com/folder鍵入用戶example.com/folder/如何將用戶重定向到另一個域的.htaccess

有沒有辦法來改變(例如,當他們去例子.com /文件夾重定向到otherexample.com/folder/)。但是這必須影響所有文件夾,但不包括根目錄。此外,代碼在鍵入example.com/folder/時不應該執行任何操作。

編輯

而且代碼應該不會影響到圖像和其它文件(或更好說,如果請求的文件有文件擴展名),只是文件夾。

例如:

should affect : example.com/folder 
       : example.com/folder/folder 

but should not : example.com 
       : example.com/forum (but only forum, only this folder) 
       : example.com/folder/ 
       : example.com/folder/. 
       : example.com/folder/folder/ 
       : example.com/image.png 
       : example.com/something.something 
       : example.com/something. 
       : example.com/.something 

編輯

和另一編輯: 比方說,我們發現的.htaccess代碼(下面的第一個答案)。我如何使它適用於我所有的域名和網站,期望我的博客(example.com/blog),我不想讓整個我的網站有重定向?

回答

7

服務器默認重定向誰鍵入example.com/folder到 example.com/folder/

這是由mod_dir做了幾個很好的理由,所有的DirectorySlash文檔中解釋用戶。您可以使用DirectorySlash Off來關閉此行爲,但通常不是一個好主意,除非您有非常好的和有效的理由才能這樣做。

但是,如上所述,您所描述的問題可以單獨由mod_rewrite來處理。首先,你需要檢查請求的資源是否是一個目錄。它需要是您的DocumentRoot中的物理目錄。

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d 

這也是非常重要的也適用%{DOCUMENT_ROOT},否則目錄檢查將無法正常工作。


  • '-D'(是目錄)

    將TestString作爲路徑名並測試它是否存在,並且是一個目錄。


其次,你需要忽略到的目錄,實際上確實有拖尾斜槓的所有請求。

RewriteCond %{REQUEST_URI}     !/$ 

現在你已經滲透到剛要重定向到另一個域的資源:目錄沒有結尾的斜線。其他所有東西(帶有斜線,圖像等的目錄)在這一點上甚至都不在遊戲中。實際重定向的時間。

RewriteRule ^ http://other.com%{REQUEST_URI}/ [redirect=temp,last] 

你可能想以後改變了主意重定向,所以你應該使用臨時重定向(又名302 Found)的而不是永久(亦稱301 Moved Permanently)。 last告訴mod_rewrite停止所有進一步處理。

而這裏的最終產品:

RewriteEngine On 

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d 
RewriteCond %{REQUEST_URI}     !/$ 

RewriteRule ^ http://other.com%{REQUEST_URI}/ [redirect=temp,last] 
+0

似乎有效,感謝代碼 – Enve

+0

還有一個問題,如果你讓我問你。 是否有任何代碼不允許此代碼影響位於example.com/blog中的我的博客。 – Enve

+0

是的,請添加'RewriteCond%{REQUEST_URI}!^/blog'。那麼這個賞金怎麼樣? – Perleone

4

什麼

RewriteEngine On 
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !(.*)/$ 
RewriteRule ^(.*)$ http://otherexample.com/$1/ [L,R=301] 
+0

但我不」不能重定向圖像和其他東西,只是文件夾 – Enve

+0

所以,我說的是,它完成了這項工作,但它做得不好,如果可以,請僅使其更具體和文件夾。 – Enve

1
# This allows you to redirect your entire website to any other domain 
Redirect 301/http://mt-example.com/ 

# This allows you to redirect your entire website to any other domain 
Redirect 302/http://mt-example.com/ 

# This allows you to redirect index.html to a specific subfolder 
Redirect /index.html http://example.com/newdirectory/ 

# Redirect old file path to new file path 
Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html 

# Provide Specific Index Page (Set the default handler) 
DirectoryIndex index.html 

Read more here for RewriteEngine method

1

希望這是你所需要的

Redirect 301 /directory http://www.newdomain.com 

只是改變目錄與文件夾名

+0

這不是重定向 – Enve

+0

如果文件夾名稱相同,它會使無限重定向循環 – Enve

1

爲:www.newdomain.com

RewriteEngine On 
RewriteBase/
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC] 
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301] 

爲:olddomain.com到newdomain.com

RewriteEngine On 
RewriteBase/
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC] 
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301] 
相關問題