2017-10-14 21 views
0

我有以下.htaccess文件htaccess的重寫總是通過文件名,而不是實際的URL的

RewriteEngine on 
RewriteRule ^(.*)/?$ index.php?file=$1 [L] 

,所以我希望它轉發到mysite.com/foobar mysite.com/index.php?file=foobar
我有以下的index.php文件

<?php 
    var_dump($_GET); 
?> 

不管我叫哪個URL(mysite.com,mysite.com/foobar,mysite.com/test),index.php文件的輸出總是

array(1) { ["file"]=> string(9) "index.php" } 

,而我預計

array(1) { ["file"]=> string(0) "" } 
array(1) { ["file"]=> string(6) "foobar" } 
array(1) { ["file"]=> string(4) "test" } 

$ _ SERVER [ 'QUERY_STRING']也返回 「文件=的index.php」

我不認爲任何其他htaccess中都有涉及,當我寫的隨機字符到.htaccess文件,我得到一個內部服務器錯誤(如預期),如果我改變參數「文件」的名稱或更改文件「的index.php」到「test.php的」的名稱問題保持不變(顯示的test.php而不是index.php文件)

回答

0

你錯過幾位。 嘗試:
匹配所有字符:

RewriteRule ^/?([.*])$ \index.php?file=$1 [L] 

如果你只需要匹配字母字符:

RewriteRule ^/?([a-z]+)$ \index.php?file=$1 [L] 

如果你想使您的生活更輕鬆使用本:http://www.generateit.net/mod-rewrite/index.php 或者,如果你想詳細瞭解正則表達式:https://www.princeton.edu/~mlovett/reference/Regular-Expressions.pdf

+0

這個代碼顯示陣列(0){}當我打電話mysite.com或mysite.com/index.php,但返回404未找到像mysite的呼叫.COM/foobar的 – SackZement

+0

固定的拼寫錯誤,現在修正格式。 – Erik

+1

第一條規則仍然導致同樣的錯誤404,但第二個工作就好了我的目的。謝謝。 – SackZement