当目录存在时,HTACCSS 重写 URL 重定向


htaccss rewrite url redirect when directory exists

>我的.htaccess上有这个内容:

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

在索引.php文件上,我有这个:

<?php
    var_dump($_GET);

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

我明白了。没关系:

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

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

我得到了:...也可以:

array(1) { ["url"]=> string(5) "other" }

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

我该如何防止这种情况?

出于

安全原因,尾部斜杠被添加到现有目录的前面,这是在模块之后运行的名为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]