当目录存在时,htaccss重写url重定向

时间:2016-04-26 14:19:57

标签: php .htaccess url-rewriting apache2.4

我在.htaccess上有这个内容:

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

并在index.php文件中我有:

<?php
    var_dump($_GET);

如果我访问http://localhost/index.php

我得到了这个。没关系:

  

array(1){[“url”] =&gt; string(9)“index.php”}

如果我访问http://localhost/other

我得到了......也没关系:

  

array(1){[“url”] =&gt; string(5)“other”}

当我尝试访问具有现有文件夹名称的链接时出现问题。换句话说,如果我有一个名为css的文件夹,当我访问http://localhost/css时,我的网址会变为:http://localhost/css/?url=css

我该如何防止这种情况?

1 个答案:

答案 0 :(得分:1)

由于安全原因,在现有目录前添加了尾部斜杠,这是在mod_dir模块之后运行的名为mod_rewrite的模块中完成的。因此,在跟踪斜杠添加后,您也会获得查询字符串。

你可以这样做:

RewriteEngine on
RewriteBase /

# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302,NE]

RewriteRule ^(.*?)/?$ index.php?url=$1 [L,QSA]