我使用Apache 2.2.22和PHP 5.3.10运行Ubuntu 12.04。我希望用户能够在我的网站上请求页面,而不必键入'.php'扩展名,而不必担心区分大小写。例如,网址像这样......Apache:如何重写没有文件扩展名的URL并忽略大小写?
http://www.site.com/test/phpinfo
http://www.site.com/test/PhPiNfO
...应该都会导致:
http://www.site.com/test/phpinfo.php
我开始不区分大小写按照在this question接受的答案。下面是我的 '的/ etc/apache2的/网站可用/默认的' 文件看起来像......
<VirtualHost *:80>
ServerAdmin [email protected]
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}
...并试图访问不成功测试/ phpinfo.php的。然后我删除了.htaccess文件,并将这两行直接放到我的'/ etc/apache2/sites-available/default'文件中,然后重新启动Apache。出于一些莫名其妙的原因,我想要的行为现在正在工作!我不能可能如何,因为我唯一的规则不会追加'.php'的URL!
这可能听起来很奇怪,但它几乎感觉我的更改正在以某种延迟方式生效。我尝试了一些其他测试不能马上工作,但是当它们不应该有时它似乎起作用。我继续测试并期望我的当前设置在不改变任何内容的情况下打破。无论如何,我感到非常困惑。
希望有人能够在我将大脑翻出来并用大锤砸碎之前将这些东西洒下来。
在你最后的规则中,'$'应该是'/ $'中的文字字符还是元字符? –
@FelipeAlamedaA:'/ $'中的'$'不是字面的。它允许我调用重新映射的内部函数。请参阅Apache文档:[link](http://httpd.apache.org/docs/current/rewrite/rewritemap.html#int) –
我知道。我只是确定。 –