htaccess重写总是传递文件名而不是实际的url

时间:2017-10-14 08:56:44

标签: php .htaccess redirect mod-rewrite

我有以下.htaccess文件

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

所以我希望它能将mysite.com/foobar转发给mysite.com/index.php?file=foobar
我有以下index.php文件

<?php
    var_dump($_GET);
?>

无论我调用哪个网址(mysite.com,mysite.com / phpobar,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 [&#39; QUERY_STRING&#39;]也会返回&#34; file = index.php&#34;

我不认为涉及任何其他htaccess文件,当我将随机字符写入.htaccess文件时,我收到内部服务器错误(如预期的那样),如果我更改参数的名称&#34 ;文件&#34;或者更改文件的名称&#34; index.php&#34;到&#34; test.php&#34;问题保持不变(显示test.php而不是index.php)

1 个答案:

答案 0 :(得分: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

相关问题