Apache 重定向到根目录中的index.php
我在htaccess文件中编写了以下代码:Apache 重定向到根目录中的index.php,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我在htaccess文件中编写了以下代码: RewriteRule ^(.*)/$ $1 RewriteCond %{REQUEST_URI} !^index.php.*$ RewriteRule ^(.*)$ /index.php?route=$1 [END] 它适用于所有路径,除了存在的目录。例如,如果我输入http://localhost/profilepic而这样的目录实际上存在,它将重定向到http://localhost/profilepic/?route=profilepic,但
RewriteRule ^(.*)/$ $1
RewriteCond %{REQUEST_URI} !^index.php.*$
RewriteRule ^(.*)$ /index.php?route=$1 [END]
它适用于所有路径,除了存在的目录。例如,如果我输入http://localhost/profilepic
而这样的目录实际上存在,它将重定向到http://localhost/profilepic/?route=profilepic
,但我希望它隐式转换为http://localhost/index.php?route=profilepic
提前感谢。发生这种情况的原因是。本质上,如果它看到一个URI没有尾随斜杠,并且它映射到一个现有目录,那么它将重定向请求,使其具有尾随斜杠。由于mod_dir和mod_rewrite都位于URL文件处理管道中的不同位置,因此mod_dir和mod_rewrite都应用于同一URL。这就是为什么最终会出现重定向和带有查询字符串的奇怪URL 如果您绝对必须拥有没有尾部斜杠的目录,那么您需要打开
DirectorySlash
。关闭它的问题是,存在一个信息披露安全问题,这将使人们能够查看目录的内容,即使您有一个索引文件。这意味着您必须使用mod_rewrite来弥补mod_dir
因此,摆脱规则:
RewriteRule ^(.*)/$ $1
并将其替换为以下规则:
DirectorySlash Off
# redirect direct requests that end with a slash to remove the slash.
RewriteCond %{THE_REQUEST} \ /+[^\?\ ]+/($|\ |\?)
RewriteRule ^(.*)/$ /$1 [L,R]
# internally add the trailing slash for directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ /$1/ [L]
以下是另一种不用关闭
目录斜杠
(被视为安全漏洞)就可以拥有规则的方法:
+这是一个很好的解决方案。Tnx。顺便说一句,L是什么意思?L是将规则标记为
Last
,它将重写的URL重新注入mod_重写引擎。
RewriteEngine On
# remove trailing slash for non-directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
# routing for directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+?)/$ /index.php?route=$1 [L]
# routing for non directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /index.php?route=$1 [L]