使用 Mod-Rewrite 将路径更改为文件
我在 Google 和 stackoverflow 上搜索了超过 48 小时,但没有找到类似的问题/解决方案;因此,我寻求帮助。
如果我想重写诸如 http://localhost/filename
之类的 URL 来获取 filename.php (例如 localhost/aboutus
获取 aboutus.php
、localhost/contact
获取 contact.php
等)。
我该怎么做?我不想为每个页面都编写规则;我希望“文件名”是动态的,以便单个规则适用于特定文件夹中的所有页面。
我尝试过:
RewriteRule ^(.*)/?$ $1.php
在 .htaccess 文件中,我不断收到 500 错误。 谢谢。
整个文件包含以下内容:
RewriteEngine 开启
RewriteBase /
RewriteRule ^(.*)/?$ $1.php
这就是文件包含的全部内容
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 contains
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
500 错误可能有很多原因 - 有没有办法获取有关它的更多信息?
不管怎样,你的问题可能是 (.*) 的贪婪方法以及你对可能存在或不存在的尾部斜杠的处理。
尝试
只匹配 [] 内的字符而不包含“/”。
您可以在此处检查您的 RewriteRules:http://martinmelin.se/rewrite-rule-tester/
The 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
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/