php - Apache:如何重写没有文件扩展名并忽略大小写的 URL?

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

我正在运行带有 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/phpinfO.php 失败。然后我删除了 .htaccess 文件并将这两行直接放入我的“/etc/apache2/sites-available/default”文件并重新启动了 Apache。由于某些无法解释的原因,我想要的行为现在起作用了!我无法可能理解如何,因为我唯一的规则不会将“.php”附加到 URL!

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

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

最佳答案

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

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

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

编辑: 好吧,我弄清楚了 .htaccess 文件无法正常工作的原因。它正在被阅读,但第二条规则没有被匹配。这就是为什么,根据 Apache documentation :

The removed prefix always ends with a slash, meaning the matching occurs against a string which never has a leading slash. Therefore, a Pattern with ^/ never matches in per-directory context.

因此,我更改了我的 .htaccess 文件以具有此...

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

...它终于开始工作了!

关于php - Apache:如何重写没有文件扩展名并忽略大小写的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14180176/

相关文章:

java - 使用 Apache POI 从 Word 文档中提取段落

python - Django、python、mod_wsgi 和 Apache worker

java - 我可以调试由 Tomcat 运行的 GWT-RPC 程序和由 Apache Web 服务器运行的静态内容吗(使用 IntelliJ)

php - .htaccess 重写规则导致 css、js 停止工作

javascript - 使用 .htaccess 重写 URL 给出 404 NOT FOUND

php mysql while循环内部while循环查询同一个表

php - PayPal Express Checkout - 扣除金额的自定义文本

php - "error": "array_merge(): Expected parameter 1 to be an array, null given"

php - 带有参数的 URL 的 htaccess 301 重定向

javascript - 在具有相同类的多个 div 上启动 ProgressBar