2012-03-07 89 views
0

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

本地主机: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> 

Options +FollowSymLinks 
DirectoryIndex questions.php 
RewriteEngine on 

RewriteCond %{HTTP_HOST} ^domain\.localhost$ 
RewriteRule ^(.*)$ http://domain\.localhost/subdir/$1 [R=301,L] 

RewriteCond %{REQUEST_URI} ^/$ 
RewriteRule (.*) http://www.domain.com/subdir/ [R=301,L] 
+0

我已经尝试了所有的解决方案,但不幸的是没有按预期工作。谢谢你们所有的答案。 – Codex73 2012-03-22 17:49:06

回答

0

不需要指定域名在你的rewrite-rules里面:

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

(记住要清除浏览器缓存第一)

0

如果不指定一个完整的URL,现代重写将使其相对于请求的域名。换句话说,RewriteRule ^(.*)$ /subdir$1 [R=301,L]将重定向http://domain.localhost/blah-blahhttp://domain.localhost/subdir/blah-blah

你的配置可被冷凝成一个单一的重写规则:

Options +FollowSymLinks 
DirectoryIndex questions.php 
RewriteEngine on 

RewriteCond %{REQUEST_URI} !^/subdir 
RewriteRule ^(.*)$ /subdir$1 [R=301,L] 

由于重写规则图案匹配将包括初始/,即/夸夸其谈,重写/subdir$1将防止重定向导致双斜线。

1

对于这些原则:

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

www.domain.com -> www.domain.com/subdir/ 

这里有精确的RewriteRules:

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] 

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

相关问题