.htaccess 重定向到子域 [英] .htaccess redirect to subdomain

查看:37
本文介绍了.htaccess 重定向到子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在准备设置场景,如果有人访问 website.com/blog/,他们将被重定向到 blog.website.com.

I'm tying to set up the scenario, if someone visits website.com/blog/ they are redirected to blog.website.com.

我之前在 .htaccess 文件中使用此代码来实现其他一些重定向(从 .net 到 .com):

I used this code in my .htaccess file to achieve some other redirection (from .net to .com) previously:

Options +FollowSymLinks
RewriteEngine on  
RewriteCond %{HTTP_HOST} ^(www.|blog.)?website.net(.*) [NC]  
RewriteRule (.*) http://www.website.com/$1 [R=301,L]

我的 apache 配置文件中有一个 ErrorDocument 404 也可能与此相关.

It may also be relevant that I have an ErrorDocument 404 in my apache configuration file.

为了解决我的问题,我尝试了这个:

To solve my my problem I tried this:

Options +FollowSymLinks
RewriteEngine on  
RewriteCond %{HTTP_HOST} ^(www.|blog.)?website.net(.*) [NC]  
RewriteRule (.*) http://www.website.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(www.)?website(.com|.net)/blog/?$ [NC]
RewriteRule (.*) http://blog.website.com/ [L]

但是此代码不起作用.我的错误文档仍在处理请求(/blog/目录不存在).我尝试将这个新代码移到 apache conf 文件中的 ErrorDocument 上方,但发生了同样的事情.

This code however is not working. My error document is still handling the request (the /blog/ directory doesn't exist). I tried moving this new code above the ErrorDocument in the apache conf file, but the same thing occurred.

这无疑是一个语法错误,任何帮助将不胜感激:)

It's no doubt a syntactical error, any help would be greatly appreciated :)

推荐答案

尝试将以下内容添加到 webiste.com 站点根目录中的 .htaccess 文件中.

Try adding the following to the .htaccess file in the root directory of your webiste.com site.

RewriteEngine on
RewriteBase / 

#if not already blog.website.com
RewriteCond %{HTTP_HOST} !^blog\.website\.com$ [NC] 
#if request is for blog/, go to blog.website.com
RewriteRule ^blog/$ http://blog.website.com [L,NC,R=301] 

这篇关于.htaccess 重定向到子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆