【发布时间】:2016-10-04 12:32:34
【问题描述】:
我有一个这样设置的文件夹:
/blog/blog-post-name/post-content.txt
我的网站根目录中有另一个页面,其中将包含 post-content.txt,方法是在查询字符串中包含目录名称,如下所示:
/blog.php?path=blog-post-name
但是,为了使网址看起来更好,我希望用户能够通过以下链接访问帖子:
/blog/blog-post-name
我创建了一个如下所示的 htaccess 文件,用于在内部使用查询字符串重定向到 url。
RewriteEngine on
# if not a file
RewriteCond %{REQUEST_FILENAME} !-f
# and if the directory exists
RewriteCond %{REQUEST_FILENAME} -d
# take old folder and append to query string
RewriteRule ^blog/(.*)$ blog/post.php?path=$1 [NC,L]
问题是,因为这实际上是一个目录,它想添加一个尾部斜杠。我可以使用DirectorySlash off,但我已经阅读了security concerns。有没有更安全的方法将目录重定向到文件,或者这只是一个坏主意?
【问题讨论】:
-
如果 directorySlash 指令有问题,请重命名文件夹。
标签: apache .htaccess mod-rewrite query-string