Mod Rewrite,忽略目录,除非URL有一个尾部斜杠

时间:2015-11-04 15:48:26

标签: php apache .htaccess mod-rewrite

我正在使用基本64编码为我的网址制作网址缩短器。问题是我的主目录中还有一堆文件夹。我的目录看起来像这样

/
  /css
    style.css
  /handlers
    handle_database.php
  index.php
  .htaccess

这是我用来捕获编码网址的重写规则

RewriteRule ^([A-Za-z0-9_\-]{3,8})$ index.php?a=$1

这很有效。我的问题来自于我的系统生成一个类似于http://exampleurlshortener.com/css的网址,在理想情况下,重写规则将捕获“css”并让我的index.php处理其余的,问题是apache添加了一个尾随斜杠它最终进入了css目录。

所以我需要的是 http://exampleurlshortener/css - >让index.php处理请求 http://exampleurlshortener/css/ - >访问实际目录

到目前为止,我没有运气,因为apache不断添加尾部斜杠

1 个答案:

答案 0 :(得分:0)

您需要关闭DirectorySlash以避免Apache添加尾部斜杠。关闭Indexes选项以避免Apache向用户显示目录列表也很重要。

DirectorySlash off
Options -Indexes
DirectoryIndex index.php

RewriteEngine On

RewriteRule ^([\w-]{3,8})$ index.php?a=$1 [L,QSA]