2015-04-23 42 views
0

文檔根我有一個網上商店,而且由於谷歌決定促進網頁的移動版本,我發展我的m.example.com移動版本(的.htaccess?)

這裏有個竅門。我不想要,m.域名。我想要的是,當用戶來到時,我檢查用戶代理,如果它來自移動設備,我將顯示移動版本,如果來自桌面,則顯示桌面版本。

但是,我想確切地使用相同的URL給用戶。因此,http://example.com/contact/將成爲桌面用戶和移動用戶的網址。

頁面的移動版本是一個子目錄下./mobile/

是否有可能以某種方式強迫apache的,要改變文檔根目錄,如果有人是來自移動設備,但保持原有的網址嗎?

例如:

http://example.com/contact/應該運行/var/apache/example.com/mobile/contact.php,但對於桌面版,應該運行/var/apache/example.com/contact.php?正如我所提到的,這些URL是相同的。

如果問題不清晰,請發表評論。

+1

**注:**根據應用程序的設計,這是可以做到當你加載視圖(在MVC設計中)而不是有2個項目(1個用於桌面,1個用於移動) –

+0

是的,這是一個MVC項目。但是移動版本,在MVC中是一個新的開發,而桌面版本是一個遺留代碼。 – vaso123

+0

另一個注意事項:用PHP應用程序本身實現這一點可能更容易,或者將當前CSS調整爲響應 –

回答

0

像這樣的東西? (從this answer整理後只是做了RewriteRule

RewriteEngine On 

# Check if this is the noredirect query string 
RewriteCond %{QUERY_STRING} (^|&)noredirect=true(&|$) 
# Set a cookie, and skip the next rule 
RewriteRule^- [CO=mredir:0:%{HTTP_HOST},S] 

# Check if this looks like a mobile device 
# (You could add another [OR] to the second one and add in what you 
# had to check, but I believe most mobile devices should send at 
# least one of these headers) 
RewriteCond %{HTTP:x-wap-profile} !^$ [OR] 
RewriteCond %{HTTP:Profile}  !^$ 
# Check if we're not already on the mobile site 
RewriteCond %{HTTP_HOST}   !^m\. 
# Check to make sure we haven't set the cookie before 
RewriteCond %{HTTP:Cookie}  !\smredir=0(;|$) 
# Now redirect to the mobile site 
RewriteRule ^(.*)$ mobile/$1 [R,L] 

信貸必須到原來的答案 - Mobile Redirect using htaccess

+0

我不想將用戶重定向到'm.'我不想要一個'm.'版本的網站。我想要的是,Apache從'/ etc/apache/example.com/mobile'目錄提供'www'。 – vaso123

+0

這就是底部的'RewriteRule'用於(將請求重定向到「mobile /'目錄)。 ''檢查我們是否已經在移動網站上'「只是從相關的答案中獲得的。 –

+0

也許,我沒有解釋清楚。讓我澄清一下。我不想要'重寫規則'。我不想重定向。我想要告訴Apache,從/ mobile /目錄下獲取文件,但不想重定向。沒有'm.'網站。檢查這一個:http://bookline.hu – vaso123