0

我很不幸被困在这里。我为干净的 url 做了一个 htaccess 重定向。但是每当我有一个对应于现有目录的路径时,就会添加一个反斜杠,这非常烦人并且在我的脚本中产生错误。

所以我将 DirectorySlash 选项更改为 Off,它完美地解决了我的问题。是的,我知道安全警告和停用的索引。

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

所以,“www.my-website.com/”可以工作,但“www.my-website.com”不行。

我心想“好吧,我只会检测 REQUEST 是否为空”。什么都没发生。或者可能只为子文件夹关闭目录斜线?经过数小时的搜索,我发现以下方法确实有效,但这意味着我必须努力编写 url,这确实不是最佳的

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]

任何帮助都深表感谢!

4

1 回答 1

0

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

RewriteRule ^([a-zA-Z0-9\_\-\/\.]*)$ index.php?path=$1 [QSA]
于 2012-08-28T21:34:41.473 回答