2015-05-11 11 views
-1

我已經看過一些其他.htaccess的問題,但沒有我的具體用例。.htaccess文件重定向子域的目錄

形勢

我有一個網站,說example.com。它有一個子域sub.example.com。網絡主機還爲每個子域提供一個文件夾:example.com/sub

我想將sub.example.com和example.com/sub鏈接到othersitem.com/some/folder/path/

我至今(代碼還增加了WWW的事情,不要問爲什麼,除非能解決的事情):

在/.htaccess:

RewriteBase/

##Rewrite to www 
Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^example.com[nc] 
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc] 

##301 Redirect Entire Directory 
RedirectMatch 301 /sub/(.*) https://othersite.com/some/folder/path/$1 
在/子

/.htaccess:

RewriteBase /sub/ 

##Rewrite to www 
Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^sub.example.com[nc] 
RewriteRule ^(.*)$ http://www.sub.example.com/$1 [r=301,nc] 

##301 Redirect Entire Directory 
RedirectMatch 301 /(.*) https://othersite.com/some/folder/path/$1 

 

的問題

當我使用sub.example.com/foo/bar重定向作品(它重定向到https://othersite.com/some/folder/path/foo/bar

當我使用example.com/sub它打破(它重定向到https://othersite.com/some/folder/path/sub/foo/bar,注意../sub/..)

我需要做些什麼才能使其工作這樣子文件夾不會添加到路徑中?

回答

0

請勿在mod_rewrite中混用mod_alias規則。請將/sub/.htaccess保存爲

##Rewrite to www 
Options +FollowSymLinks 
RewriteEngine on 
RewriteBase /sub/ 

RewriteCond %{HTTP_HOST} ^sub.example.com[nc] 
RewriteRule ^(.*)$ http://www.sub.example.com/$1 [r=301,nc] 

##301 Redirect Entire Directory 
RewriteRule (.*) https://othersite.com/some/folder/path/$1 [L,R=301] 

清除瀏覽器緩存後進行測試。

0

這聽起來像是mod_dir所做的,因爲您正在訪問目錄,而沒有結尾的斜槓。默認情況下,訪問沒有結尾斜槓的目錄會將請求標記爲重定向到的同一目錄,其尾部的斜槓。這經常會導致重寫或其他重定向。嘗試關閉它並添加一個額外的重定向來處理尾部的斜線:

RewriteBase/

DirectorySlash Off 

##Rewrite to www 
Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^example.com[nc] 
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc] 

RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^(.*[^/])$ /$1/ [L,R] 

##301 Redirect deviantart Directory 
RewriteRule ^sub/(.*) https://othersite.com/some/folder/path/$1 [L,R=301]