2011-02-28 103 views
0

可能重複:
How to rewrite to mobile.myUrl.com if user is mobile? (use Apache or inside Webapp?)的.htaccess重定向

您好!我試圖寫一個重寫規則,這將在我的域中的每個地址重定向到index.php,像這樣:

RewriteCond %{REQUEST_URI} !=/index.php RewriteRule .* /index.php

這段代碼的問題是,在我的index.php檢查是否用戶從電腦或移動設備來到我的網站,並根據這些信息將他重定向到適當的版本(index.html或mobi.html) - 現在,由於此規則適用,我被困在重定向循環中。請幫助stackoverflowers :)。

+0

在服務器端語言(如PHP)中,這將更優雅地解決,因爲您將不得不維護可能隨時間增長的用戶代理列表。例如:http://stackoverflow.com/questions/4117555/simplest-way-to-detect-a-mobile-device – 2011-02-28 15:13:19

+0

但我知道如何檢測移動設備 - 而我正在使用php。我使用這種重寫條件,因爲這樣的情況:www.my-web-site.com/atricle/id < - 是需要捕獲/文章/ ID,以便我的應用程序不會遵循此網址。 – marek 2011-02-28 15:22:10

+0

那麼檢測移動設備有什麼關係呢?我不明白。 – 2011-02-28 15:29:14

回答

0

要麼你只是排除頁面index.html,然後mobi.html:

RewriteCond %{REQUEST_URI} !=/index.php 
RewriteCond %{REQUEST_URI} !=/index.html 
RewriteCond %{REQUEST_URI} !=/mobi.html 
RewriteRule .* /index.php 

,或者你可以在你的重定向URL設置一個GET變量,像header('Location: domain.com/index.html?norewrite');再不用重寫URL,如果它包含此變量:

RewriteCond %{REQUEST_URI} !=/index.php 
RewriteCond %{QUERY_STRING} !^(.*&)?norewrite(&.*)?$ 
RewriteRule .* /index.php