问题
I'm running Ubuntu 12.04 with Apache 2.2.22 and PHP 5.3.10. I'd like users to be able to request pages on my site without having to type the '.php' extension, and without having to worry about case sensitivity. For example, URLs like these...
http://www.site.com/test/phpinfo
http://www.site.com/test/PhPiNfO
...should both lead to:
http://www.site.com/test/phpinfo.php
I started with case insensitivity by following the accepted answer in this question. Here's what my '/etc/apache2/sites-available/default' file looks like...
<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>
...and here's my .htaccess file:
RewriteEngine On
RewriteRule ^([a-z]+)/([a-z\-]+)$ /${lc:$1}/${lc:$2}.php [L,NC]
The file extension part is working, but for some reason case sensitivity is still being enforced. My Apache error log is filled with lines like this:
[Sat Jan 05 23:10:41 2013] [error] [client 192.168.1.9] File does not exist: /var/www/test/phpinfO
Any ideas what I'm doing wrong here?
EDIT: After experimenting for a couple hours, I'm seeing the most bizarre behavior! I thought maybe my .htaccess file was simply being ignored, so I filled it with gibberish. This produced an internal server error as expected. I then tried just these two lines in my .htaccess file...
RewriteEngine On
RewriteRule ^/(.*)$ /${lc:$1}
...and tried to access test/phpinfO.php unsuccessfully. I then deleted the .htaccess file and put those two lines directly into my '/etc/apache2/sites-available/default' file and restarted Apache. For some inexplicable reason, the behavior I wanted is now working! I can't possibly fathom how, since my only rule doesn't append '.php' to the URL!
This may sound odd, but it almost feels like my changes are taking effect in some delayed way. I tried some other tests that didn't work right away, but that seemed to work later when they shouldn't have. I keep testing and expecting my current setup to break without changing anything. Whatever the case, I am absolutely baffled.
Hope somebody can shed some f'ing light on this before I rip my brain out and smash it with a sledge hammer.
回答1:
I finally figured this one out. Well, at least part of it. It turns out that the 'MultiViews' option was responsible for resolving files without the '.php' extension. That, in combination with these two lines in my '/etc/apache2/sites-available/default' file, gives me the behavior I desire:
RewriteEngine On
RewriteRule ^/(.*)$ /${lc:$1}
Unfortunately, I haven't been able to determine why my .htaccess file is ignored when it has the above rules, but not ignored when it has gibberish.
EDIT: OK, I figured out why the .htaccess file wasn't working. It was being read, but the second rule wasn't being matched. Here's why, according to the 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.
As a result, I changed my .htaccess file to have this...
RewriteEngine On
RewriteRule ^(.*)$ ${lc:$1}
...and it's finally working!
回答2:
CheckSpelling on
Matches files and directories. See the documentation for details.
Copied from = Case Insensitive URLs with mod_rewrite
来源:https://stackoverflow.com/questions/14180176/apache-how-to-rewrite-url-with-no-file-extension-and-ignore-case