Apache mod重写,DirectorySlash Off root fix(查看索引)

时间:2012-08-28 12:41:06

标签: apache .htaccess mod-rewrite

我很遗憾地被困在这里。 我做了一个htaccess重定向清理网址。但每当我有一个与现有目录相对应的路径时,就会添加一个反斜杠,这在我的脚本中非常烦人并产生错误。

所以我将DirectorySlash选项更改为Off,它解决了我的问题。是的我知道安全警告和去激活索引。

现在我的主要问题如下:如果输入网站的路径没有斜杠,我无法重定向到我的index.php,因为REQUEST_URI为空。

因此," www.my-website.com /"将工作,但" www.my-website.com"不会。

我心里想着,我只是检测到REQUEST是否为空"。什么都没发生。或者mayble仅关闭子文件夹的directoryslash? 经过几个小时的搜索,我发现以下内容确实有效,但意味着我必须努力编写网址,这真的不是最佳的

Options +FollowSymlinks
RewriteEngine on
  #prevent from weird bug if path is a directory
DirectorySlash Off

  # add the trailing slash to root directory
  # THIS IS WHERE I NEED A VARIABLE PATH THAT I DON'T NEED TO CHANGE MANUALY
RedirectMatch ^/?([^\.\/]+)$ http://my-fixed-url.com/$1/

  # Don't show directory indexes (the listing of files)
Options -Indexes -MultiViews

  # as long is it isn't an existing file
RewriteCond %{REQUEST_FILENAME} !-f

  # Then redirect to index.php with the param "path" set to the url
RewriteRule ^([a-zA-Z0-9\_\-\/\.]+)$ index.php?path=$1 [QSA]

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

只需将+更改为*,也可以匹配此“空”网址。所以:

RewriteRule ^([a-zA-Z0-9\_\-\/\.]*)$ index.php?path=$1 [QSA]
相关问题