2016-08-23 137 views
0

我想重定向所有傳入的URL請求通過.htaccess.htaccess規則 - 如何將傳入的「/login.php」url請求重定向到「index.php?do = login」?

例的index.php:

/johngroup -> /index.php?do=johngroup 
/addmem -> /index.php?do=addmem 
/autoshare -> /index.php?do=autoshare 
/spamwall -> /index.php?do=spamwall 

index.php將處理所有的請求。

下面的代碼是不工作:

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/
    RewriteCond %{REQUEST_URI} ^/([A-Za-z0-9_-.]*).php? do=(.*) 
    RewriteRule .* /%1.php?do=%2 [R=301,L] 
</IfModule> 
+1

你舉的例子是不明確的。您想要從哪個網址重定向哪些網址? 「我想重寫/重定向」 - 請注意「重寫」和「重定向」嚴格意義上是兩回事。您想在瀏覽器的地址欄中看到哪個網​​址? – MrWhite

+1

如果用戶請求「http:// example.com/test」,它將重定向到http://example.com/index.php?do = test' –

+0

yes abhushek sir,'http:// example。 com/test.php'到http://example.com/index.php?do=test –

回答

0

這個工作對我來說:

RewriteEngine On         
RewriteRule ^index.php - [L]     # 1 
RewriteRule ^(.*).php$ index.php?do=$1 [L]  # 2 
RewriteRule ^(\w+)$ index.php?do=$1    # 3 

說明:

  1. 如果網址以/index.php開頭,請停止處理。否則,請繼續下一條規則。
  2. 如果url格式爲/x.php,則重寫爲/index.php?do=x並停止進一步處理。否則,請繼續下一條規則。
  3. 轉換任何網址例如/test,至/index.php?do=test

也可以使用這些網站測試和調整的htaccess規則:

0

example.com/test.phpexample.com/index.php?do=test

嘗試在你的根以下.htaccess文件:

RewriteEngine On 
RewriteCond %{REQUEST_URI} !^index\.php$ 
RewriteRule ^(\w+)\.php$ index.php?do=$1 [L] 

上面會內部重寫/test.php/index.php?do=test(即, /test.php保留在瀏覽器地址欄中),無論test.php是否作爲文件系統上的物理文件存在。基本上,任何具有.php文件擴展名的文件。 RewriteCond指令可防止重寫循環。

如果您只需要重寫映射到物理文件的URL,例如。 test.php然後文件系統中的物理文件重寫前加上一個附加條件,以檢查該文件是否存在:

RewriteEngine On 
RewriteCond %{REQUEST_URI} !^index\.php$ 
RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule ^(\w+)\.php$ index.php?do=$1 [L] 
+0

先生,我試了一下,但沒有工作,我有'http:// example.com/test.php'文件。當我把你的代碼放在'.htaccess'上,然後瀏覽'http://example.com/index.php?do = test'時,它不起作用。瀏覽其展示我的主頁後。 –

+0

請回復,w3dk先生。 –

+0

你大概「瀏覽」/ test.php而不是'/index.php?do = test' - 不是嗎?如果'/index.php?do = test'不起作用,那麼這似乎是你的服務器端腳本 - 是不是? – MrWhite

相關問題