本文介紹了.htaccess 將子域重寫(xiě)到目錄并將子域保留在 url 中的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我需要使用 .htaccess 將子域重寫(xiě)為子目錄,但將子域保留在 url 中,如下所示:
I need to rewrite a subdomain to a subdirectory using .htaccess but keep the subdomain in the url like this:
瀏覽器中訪問(wèn)的 url:sub1.domain.com
Visited url in browser: sub1.domain.com
提供的網(wǎng)址:sub.domain.com/sub1/
我嘗試了以下
RewriteCond %{HTTP_HOST} ^subdomain.domain.com
RewriteRule ^(.*)$ http://subdomain.domain.com/subdomain/$1 [L,NC,QSA]
但這會(huì)導(dǎo)致無(wú)限循環(huán).我知道通過(guò) php 執(zhí)行此操作會(huì)更容易,但是使用 .htaccess 是否有任何解決方案?
but this leads to an endless loop. I know that it would be easier to do this by php but is there any solution for this with using .htaccess?
推薦答案
您可以在文檔根目錄中使用此規(guī)則:
You can use this rule in document root:
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$ [NC]
RewriteRule ^((?!sub1/).*)$ /sub1/$1 [L,NC]
說(shuō)明:
NC
:忽略大小寫(xiě)L
:最后一條規(guī)則RewriteCond %{HTTP_HOST}
行確保規(guī)則只對(duì)subdomain
執(zhí)行(?!sub1/)
是否定前瞻表達(dá)式,表示請(qǐng)求的意思不是以/sub1/
開(kāi)頭RewriteRule ^((?!sub1/).*)$/sub1/$1 [L,NC]
將當(dāng)前路徑重寫(xiě)為/sub1/
NC
: Ignore caseL
: Last ruleRewriteCond %{HTTP_HOST}
line makes sure that rule only executes forsubdomain
(?!sub1/)
is negative lookahead expression that means of request is not starting with/sub1/
RewriteRule ^((?!sub1/).*)$ /sub1/$1 [L,NC]
rewrites current path to/sub1/<uri>
- Apache mod_rewrite 介紹
- Apache mod_rewrite 技術(shù)細(xì)節(jié)
這篇關(guān)于.htaccess 將子域重寫(xiě)到目錄并將子域保留在 url 中的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!