【问题标题】:htaccss rewrite url redirect when directory exists目录存在时htaccess重写url重定向
【发布时间】:2016-08-20 10:41:43
【问题描述】:

我的 .htaccess 中有此内容:

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

在 index.php 文件中我有这个:

<?php
    var_dump($_GET);

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

我明白了。没关系:

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

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

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

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

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

如何防止这种情况发生?

【问题讨论】:

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


    【解决方案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]
    

    【讨论】:

    • 但这又增加了一个问题:如果我访问http://localhost/,那么 url 将变为http://localhost////////// 并出现无限重定向错误
    • 让我测试并还原
    猜你喜欢
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 2013-12-21
    • 2015-03-04
    相关资源
    最近更新 更多