2015-05-11 20 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]