htaccess 重写 url 目录存在时重定向
htaccss rewrite url redirect when directory exists
我的 .htaccess 中有此内容:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ index.php?url= [L,QSA]
在 index.php 文件上我有这个:
<?php
var_dump($_GET);
如果我访问http://localhost/index.php
我明白了。没关系:
array(1) { ["url"]=> string(9) "index.php" }
我得到:...也可以:
array(1) { ["url"]=> string(5) "other" }
当我尝试访问具有现有文件夹名称的 link 时出现问题。换句话说,如果我有一个名为 css
的文件夹,当我访问 http://localhost/css 时,我的 url 变成:http://localhost/css/?url=css
我怎样才能避免这种情况?
出于安全原因,在现有目录前面添加了尾部斜杠,这是在 mod_rewrite
模块之后运行的名为 mod_dir
的模块中完成的。因此,在添加尾部斜杠后,您也会得到查询字符串。
你可以这样做:
RewriteEngine on
RewriteBase /
# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302,NE]
RewriteRule ^(.*?)/?$ index.php?url= [L,QSA]
我的 .htaccess 中有此内容:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ index.php?url= [L,QSA]
在 index.php 文件上我有这个:
<?php
var_dump($_GET);
如果我访问http://localhost/index.php
我明白了。没关系:
array(1) { ["url"]=> string(9) "index.php" }
我得到:...也可以:
array(1) { ["url"]=> string(5) "other" }
当我尝试访问具有现有文件夹名称的 link 时出现问题。换句话说,如果我有一个名为 css
的文件夹,当我访问 http://localhost/css 时,我的 url 变成:http://localhost/css/?url=css
我怎样才能避免这种情况?
出于安全原因,在现有目录前面添加了尾部斜杠,这是在 mod_rewrite
模块之后运行的名为 mod_dir
的模块中完成的。因此,在添加尾部斜杠后,您也会得到查询字符串。
你可以这样做:
RewriteEngine on
RewriteBase /
# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302,NE]
RewriteRule ^(.*?)/?$ index.php?url= [L,QSA]