2012-03-07 46 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]指令進行重定向。沒有必要精確整個域。

相關問題