【问题标题】:Apache: How to rewrite URL with no file extension and ignore case?Apache:如何重写没有文件扩展名的 URL 并忽略大小写?
【发布时间】:2012-12-20 06:01:45
【问题描述】:

我正在运行带有 Apache 2.2.22 和 PHP 5.3.10 的 Ubuntu 12.04。我希望用户能够在我的网站上请求页面,而无需键入“.php”扩展名,也不必担心区分大小写。例如,像这样的 URL...

http://www.site.com/test/phpinfo
http://www.site.com/test/PhPiNfO

...都应该导致:

http://www.site.com/test/phpinfo.php

我按照this question 中接受的答案开始不区分大小写。这是我的“/etc/apache2/sites-available/default”文件的样子...

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www

        RewriteMap lc int:tolower

        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        <Directory /var/www/test>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>
</VirtualHost>

...这是我的 .htaccess 文件:

RewriteEngine On
RewriteRule ^([a-z]+)/([a-z\-]+)$ /${lc:$1}/${lc:$2}.php [L,NC]

文件扩展名部分正在工作,但由于某些原因,仍在强制区分大小写。我的 Apache 错误日志充满了这样的行:

[Sat Jan 05 23:10:41 2013] [error] [client 192.168.1.9] File does not exist: /var/www/test/phpinfO

有什么想法我在这里做错了吗?

编辑:经过几个小时的实验,我看到了最奇怪的行为!我想也许我的 .htaccess 文件只是被忽略了,所以我用胡言乱语填充了它。这如预期的那样产生了内部服务器错误。然后我在我的 .htaccess 文件中尝试了这两行...

RewriteEngine On
RewriteRule ^/(.*)$ /${lc:$1}

...并尝试访问 test/phpinfoO.php 失败。然后我删除了 .htaccess 文件并将这两行直接放入我的“/etc/apache2/sites-available/default”文件中并重新启动了 Apache。由于某种莫名其妙的原因,我想要的行为现在正在起作用!我无法可能理解如何,因为我唯一的规则不会将“.php”附加到 URL!

这可能听起来很奇怪,但几乎感觉我的更改正在以某种延迟的方式生效。我尝试了一些其他的测试,这些测试没有立即起作用,但后来似乎在他们不应该起作用的时候起作用。我一直在测试并期望我当前的设置会在不更改任何内容的情况下中断。无论如何,我绝对感到困惑。

希望在我挖出我的大脑并用大锤砸碎它之前,有人能对此有所了解。

【问题讨论】:

  • 在您的最后一条规则中,$ 应该是 /$ 中的文字字符还是元字符?
  • @FelipeAlamedaA:/$ 中的 $ 不是字面意思。它允许我调用我重新映射的内部函数。请参阅 Apache 文档:link
  • 我知道。我只是在确定。

标签: php apache mod-rewrite file-extension ignore-case


【解决方案1】:

我终于想通了。好吧,至少是其中的一部分。事实证明,“MultiViews”选项负责解析没有“.php”扩展名的文件。结合我的“/etc/apache2/sites-available/default”文件中的这两行,我得到了我想要的行为:

RewriteEngine On
RewriteRule ^/(.*)$ /${lc:$1}

不幸的是,我无法确定为什么我的 .htaccess 文件在具有上述规则时会被忽略,但在出现乱码时却不会被忽略。

编辑: 好的,我知道为什么 .htaccess 文件不起作用了。它正在被读取,但第二条规则没有被匹配。这就是为什么,根据Apache documentation

去掉的前缀总是以斜杠结尾,表示匹配 发生在一个没有前导斜杠的字符串上。因此,一个 带有 ^/ 的模式在每个目录的上下文中永远不会匹配。

因此,我将 .htaccess 文件更改为具有此...

RewriteEngine On
RewriteRule ^(.*)$ ${lc:$1}

...终于成功了!

【讨论】:

    【解决方案2】:
    CheckSpelling on
    

    匹配文件和目录。详情请见documentation

    复制自 = Case Insensitive URLs with mod_rewrite

    【讨论】:

    • 我在发帖之前确实尝试过,但它不起作用。
    • 我在发布之前也尝试过,它导致 Apache 重定向到一个奇怪的页面,上面写着:Multiple Choices The document name you requested (/test/phpinfo) could not be found on this server .但是,我们找到了与您要求的名称相似的文件。可用文件:/test/phpinfo.php(通用基本名称)
    猜你喜欢
    • 2023-04-05
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多