2010-05-04 30 views
12

注意:我已經嘗試了this question下的建議,但它們要麼不起作用,要麼給我一個無限的重定向循環錯誤。使用Mod_Rewrite將ROOT重寫爲另一個路徑

我在我的.htaccess文件如下:

<IfModule mod_rewrite.c> 

    Options +FollowSymLinks 
    RewriteEngine On 
    RewriteBase/

    # Rewrite old links to new 
    Redirect 301 /howitworks.php  /how-it-works 
    Redirect 301 /testimonials.php  /testimonials 
    Redirect 301 /contact_us.php  /customer-service 
    Redirect 301 /affiliates.php  /affiliates 
    Redirect 301 /account.php   /customer/account 
    Redirect 301 /shopping_cart.php  /checkout/cart 
    Redirect 301 /products.php   /starter-kits 
    Redirect 301 /login.php    /customer/account/login 

    # Force to Admin if trying to access admin 
    RewriteCond %{HTTP_HOST} www\.example\.com [NC] 
    RewriteCond %{REQUEST_URI} admin [NC] 
    RewriteRule ^(.*)$ https://admin.example.com/index.php/admin [R=301,L,QSA] 

    # Compress, Combine and Cache Javascript/CSS 
    RewriteRule ^(index.php/)?minify/([^/]+)(/.*.(js|css))$ lib/minify/m.php?f=$3&d=$2 

    # Always send 404 on missing files in these folders 
    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/ 

    # Never rewrite for existing files, directories and links 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-l 
    RewriteCond %{REQUEST_URI} !server-status 

    # Rewrite everything else to index.php 
    RewriteRule .* index.php [L] 

    # Force www in url and non-example domains to example 
    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC] 
    RewriteCond %{HTTP_HOST} !^ww2\.example\.com [NC] 
    RewriteCond %{HTTP_HOST} !^qa\.example\.com [NC] 
    RewriteCond %{HTTP_HOST} !^uat\.example\.com [NC] 
    RewriteCond %{HTTP_HOST} !^local\.example\.com [NC] 
    RewriteCond %{HTTP_HOST} !^admin\.example\.com [NC] 
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L,QSA] 

</IfModule> 

我需要重寫,將做到以下幾點:

重寫:http://subdomain.example.com/http://subdomain.example.com/page

我也想這隱藏/頁面(/會像/頁面一樣),但這不是必需的要求。另外,我希望它能夠使用HTTP或HTTPS。

任何幫助是非常感謝,因爲我一直在掙扎幾個小時。

+1

這個問題屬於上ServerFault。 – leek 2010-10-11 06:42:41

回答

20

根路徑的正確重定向很簡單:

RewriteEngine On 
RewriteRule ^/$ /page [R] 

迫使這只是在一個子域(這應該勉強發生,如果你重寫塊是在虛擬主機的定義):

RewriteEngine on 
RewriteCond %{HTTP_HOST} =subdomain.example.com 
RewriteRule ^/$ /page [R] 
+5

這個答案在htaccess上下文中並不起作用,並且完全重定向到客戶端(IOW不會像問到的那樣「隱藏」) – covener 2012-12-30 21:08:29

+4

只有在使用^/$而不是^ $時,這隻對apache2有效。答案不正確或我犯了錯誤? – 2014-08-01 13:44:32

+0

錯誤:RewriteRule^$/page [R] Right:RewriteRule ^/$/page [R] – Hartmut 2016-04-25 18:51:23

5

複製我刪除的回覆:

在您的文檔的htaccess只是簡單的:

RewriteEngine on 
RewriteCond %{HTTP_HOST} =subdomain.example.com 
RewriteRule ^$ /page 
+1

不知道爲什麼會被刪除......'RewriteRule^$/page'似乎是唯一有效的答案。 'RewriteRule ^/$/page'確實_NOT_將請求匹配到站點的文檔根目錄。我正在修改當前接受的答案,以便它是正確的。 – joshperry 2013-11-06 00:05:44