.htaccess文件,用于重定向子域的目录

时间:2015-05-11 19:32:49

标签: .htaccess redirect

我已经查看了其他一些.htaccess问题,但没有一个具有我的特定用例。

状况

我有一个网站,比如example.com。它有一个子域sub.example.com。 Web主机还为每个子域提供了一个文件夹:example.com/sub

我想将sub.example.com和example.com/sub链接到othersite.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

在/sub/.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 / ..)

我需要更改什么才能使其工作,以便子文件夹不会添加到路径?

2 个答案:

答案 0 :(得分:0)

请勿在{{1​​}}中混用mod_alias条规则。保持mod_rewrite

/sub/.htaccess

清除浏览器缓存后进行测试。

答案 1 :(得分: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]
相关问题