我目前正在尝试编写重写规则。我想简化输入的 URL,以便它始终使用索引。输入 url 将是 www.example.com/home.php
并将重写为 www.example.com/index.php?r=page/home
。
我写了这个 .htaccess
文件:
RewriteEngine On
RewriteRule ^([^/]*)\.php$ /index.php?r=page/$1 [L]
不幸的是,这个配置给我一个错误 500。我可以理解这是因为如果我输入 index.php
例如,apache 将不知道它是否必须使用 index.php
文件或使用重写的 url index.php?r=page/index
。
有没有可能用apache来完成这种重写规则?如果是这样,我该如何修复我的错误 500?
编辑:请注意,如果我将扩展名 .php
更改为任何其他内容(例如 .html
),RewriteRule 可以正常工作,就像这样: RewriteRule ^([^/]*)\.html$/index.php?r=page/$1 [L]
最佳答案
你有两种可能。
第一个
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule ^([^/]+)\.php$ /index.php?r=page/$1 [L]
第二个
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /([^/]+)\.php
RewriteRule ^.*$ /index.php?r=page/$1 [L]
关于php - .htaccess 将 php 重写为 php 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22951819/