Using Mod-Rewrite to change Paths into files
I've searched both Google, and stackoverflow for more than 48 hrs, and I haven't found a similar question/solution; hence, my asking for help.
If I want to rewrite URLs such as http://localhost/filename
to obtain filename.php (such that localhost/开发者_开发技巧aboutus
fetches aboutus.php
, localhost/contact
fetches contact.php
, etc).
How do I do that? I don't want to write a rule for each of the pages; I want the 'filename' to be a dynamic so that a single rule will work for all the pages in the specific folder.
I tried:
RewriteRule ^(.*)/?$ $1.php
In a .htaccess file, and I keep receiving a 500 error. Thanks.
The whole file contains this: RewriteEngine On RewriteBase / RewriteRule ^(.*)/?$ $1.php That's all the file containsThe 500 error can have lots of reasons - is there a way to obtain more information about it?
Anyway, possibly your problem is the greedy approach of (.*) and your handling of the trailing slash that may be there or not.
Try
RewriteRule ^([_a-zA-Z0-9]*)/?$ $1.php
instead which will only match the characters inside the [] and not include a '/'.
You can check your RewriteRules here: http://martinmelin.se/rewrite-rule-tester/
精彩评论