.htaccess具有现有子目录的RewriteRule行为

时间:2019-04-02 16:38:09

标签: apache .htaccess mod-rewrite friendly-url

我曾经遇到过许多类似的问题,但我找不到这种特殊情况:

具有此结构:

public_html/
   q/
      .htaccess
      index.php
      /dirnofixedname1
      /dirnofixedname2
      /dirnofixedname3
  • dirnofixednameN是包含文件供index.php使用且不能直接访问的文件夹(之所以这样称呼,是因为我可能没有在.htaccess文件中列举所有内容,否则将是不切实际的)
  • index.php应该处理传入的请求

目的是处理如下请求: http://domain/q/dirnofixedname2http://domain/q/index.php?q=dirnofixedname2同时显示http://domain/q/dirnofixedname2。确实是一个流行且已经解决的案例。

.htaccess文件为:

RewriteEngine On

RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?q=$1 [L]

问题发生了,当请求匹配那些现有目录(我想要的东西)时,它按预期工作(index.php执行并获取q),但重定向到:

http://domain/q/dirnofixedname2/?q=dirnofixedname2

(并在网址栏中显示),而不是预期的

http://domain/q/dirnofixedname2

尤其是如果目录不存在,

http://domain/q/dirthatdoesnotexist

index.php正确处理,qdirthatdoesnotexist(脚本显然将其忽略并且不返回任何内容)。

您是否有关于在子目录存在的情况下如何避免重定向的想法? (具有与该参数相同的目录名是很实际的)

1 个答案:

答案 0 :(得分:0)

这是由于stock指令引起的,该指令默认为DirectorySlash状态,因为您要在URI中请求一个实际目录。

您可以使用以下方法将其关闭:

On

也可以屏蔽目录列表的使用:

DirectorySlash Off

.htaccess顶部