Apache htaccess如何将两台主机重定向到子目录

Apache htaccess如何将两台主机重定向到子目录,apache,mod-rewrite,Apache,Mod Rewrite,本地主机和远程主机如何将根目录重定向到子目录 本地主机:domain.localhost 远程主机:domain.com 子目录:子目录 domain.localhost -> domain.localhost/subdir/<br> domain.localhost/ -> domain.localhost/subdir/<br> domain.com-> domain.com/subdir/<br> www.domain.com ->

本地主机和远程主机如何将根目录重定向到子目录

本地主机:domain.localhost
远程主机:domain.com
子目录:子目录

domain.localhost -> domain.localhost/subdir/<br>
domain.localhost/ -> domain.localhost/subdir/<br>
domain.com-> domain.com/subdir/<br>
www.domain.com -> www.domain.com/subdir/<br>

无需在重写规则中指定域名:

RewriteCond $0 !^subdir
RewriteRule (.*) /subdir/$1 [L]

(请记住先清除浏览器缓存)

如果未指定完整的url,Mod Rewrite将使其相对于请求的域。换句话说,
RewriteRule^(.*)$/subdir$1[R=301,L]
将重定向
http://domain.localhost/blah-blah
http://domain.localhost/subdir/blah-blah

您的配置可以压缩为一条规则:

Options +FollowSymLinks
DirectoryIndex questions.php
RewriteEngine on

RewriteCond %{REQUEST_URI} !^/subdir
RewriteRule ^(.*)$ /subdir$1 [R=301,L]
由于重写规则模式匹配将包括初始的
/
,即诸如此类,重写
/subdir$1
将防止重定向导致双斜杠。

对于这些原则:

domain.localhost -> domain.localhost/subdir/
domain.localhost/ -> domain.localhost/subdir/
domain.com-> domain.com/subdir/

www.domain.com -> www.domain.com/subdir/
以下是确切的规则:

Options +FollowSymLinks
DirectoryIndex questions.php
RewriteEngine on

# domain.localhost -> domain.localhost/subdir/
RewriteCond %{HTTP_HOST} ^domain\.localhost$
RewriteRule (.*) /subdir/$1 [QSA,L]

# domain.com -> domain.com/subdir/
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule (.*) /subdir/$1 [QSA,L]

如果不起作用,请尝试不使用“
/
”:



注意:不需要使用
[R]
指令进行重定向。没有必要对整个域进行精确计算。

我已经尝试了所有的解决方案,但不幸的是没有按预期运行。谢谢大家的回答。
Options +FollowSymLinks
DirectoryIndex questions.php
RewriteEngine on

# domain.localhost -> domain.localhost/subdir/
RewriteCond %{HTTP_HOST} ^domain\.localhost$
RewriteRule (.*) /subdir/$1 [QSA,L]

# domain.com -> domain.com/subdir/
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule (.*) /subdir/$1 [QSA,L]
Options +FollowSymLinks
DirectoryIndex questions.php
RewriteEngine on

# domain.localhost -> domain.localhost/subdir/
RewriteCond %{HTTP_HOST} ^domain\.localhost$
RewriteRule (.*) /subdir$1 [QSA,L]

# domain.com -> domain.com/subdir/
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule (.*) /subdir$1 [QSA,L]