【问题标题】:Mod Rewrite, ignore directory unless URL has a trailing slashMod Rewrite,忽略目录,除非 URL 有斜杠
【发布时间】:2015-11-04 15:48:26
【问题描述】:

我正在为我的网址制作一个使用 base 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 的 url 时,我的问题就出现了,在理想情况下,重写规则将捕获“css”并让我的 index.php 处理其余部分,问题是 apache 添加了一个斜杠,它最终进入 css 目录。

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

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

【问题讨论】:

  • 使用Options -MultiViews
  • 运气不好,知道如何实现吗?
  • 您是否尝试在规则中设置 / 可选? RewriteRule ^([A-Za-z0-9_\-]{3,8})/?$ index.php?a=$1
  • @PanamaJack css/ 目录存在。

标签: php apache .htaccess mod-rewrite


【解决方案1】:

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

DirectorySlash off
Options -Indexes
DirectoryIndex index.php

RewriteEngine On

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多