.htaccess 将域重定向到子目录而不更改 URL [英] .htaccess redirect domain to subdirectory without changing URL

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

问题描述

我有以下设置:-> domain1.com 是我的主域,也是文档根目录-> domain2.com 是另一个注册为别名域的域(意味着它与 domain1.com 完全相同),我有多个这样的域.

I have the following setup: -> domain1.com is my main domain and also document root -> domain2.com is another domain which is registered as an alias domain (means it does exactly the same as domain1.com), I have multiple of those domains.

当我现在想将该域重定向到某些内容时,我通常使用它来将域重定向到其他内容(例如外部网站):

when I now want to redirect that domain to something I usually use this to redirect the domain to something else (like an external website):

RewriteCond %{HTTP_HOST} ^(www\.)?domain2.com$ [NC]
RewriteRule ^(.*) http://somewiredisp.com/user/bla/$1 [L,R]

如果我为需要为他们的论坛或其他任何东西提供一个不错的 URL 的人注册域,通常会使用此方法.像魅力一样工作 - 唯一的问题"是浏览器中的地址栏发生了变化,但我已经读到外部 URL 无法做到这一点,而且因为没有人抱怨过它没问题.

this is usually used if I register a domain for someone who needs a nice url for their forums or whatever. Works like a charm - the only "problem" would be that the address bar in the browser changes, but I've read that there would be no way to do that with external URLs, and since no one ever complained about it I am fine with it.

但是,现在我想将一些 url 链接到一个子目录,而不会在地址栏中更改 url.

However, now I would like to link some url to a subdirectory WITHOUT the url changing in the address bar.

domain2.com -> domain1.com/subdir(或 domain2.com/subdir - 没关系,因为它是别名域).

domain2.com -> domain1.com/subdir (or domain2.com/subdir - that doesn't matter, since it's an alias domain).

我目前的方法是

RewriteCond %{HTTP_HOST} ^(www\.)?domain2.com$ [NC]
RewriteRule ^(.*) http://domain1.com/subdir/ [P]

这不起作用(错误 404) - 如果我直接调用 domain1.com/subdir/如果明显有效.

which doesn't work (Error 404) - if I call domain1.com/subdir/ directly if works obviously.

我也尝试了

RewriteRule ^(.*) http://domain2.com/subdir/ [P]
RewriteRule ^(.*) /subdir/ [P]
RewriteRule ^(.*) http://domain1.com/subdir/index.html [P]

结果都一样.

也许这只是一个简单的错误,但我目前一无所知:/

maybe it's just a simple mistake but I am currently clueless :/

推荐答案

你可以使用这个规则:

RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule !^subdir/ /subdir%{REQUEST_URI} [L,NC]

如果 /subdir 不存在,这会将 domain2.com 的每个请求作为前缀.

This will prefix every request for domain2.com with /subdir if it is not already there.

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

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