我只想重定向
http://thehost/pagename
至http://thehost/pagename.php
和http://thehost/pagename.html
至http://thehost/pagename.php
。
它还必须支持:http://thehost
或http://thehost/
(至index.php
)
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/(.*)\.html $1.php [L]
RewriteRule ^/([^/]*)$ /$1.php [L]
迷失。帮助。
答案 0 :(得分:1)
我想这应该是您要寻找的:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^/?([^/]*)$ /$1.php [END]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/?(.*)\.html$ /$1.php [END]
如果您在这些行中收到“内部服务器错误”(http状态为500),则很可能是您运行的Apache http服务器版本非常旧,应升级[END]
标志或将其替换为旧的[L]
标志。在这种情况下,您将在http服务器错误日志文件中看到有关此内容的明确提示。
还有一个一般性说明:您应该始终喜欢将此类规则放置在http服务器主机配置中,而不要使用动态配置文件(“ .htaccess”)。这些动态配置文件增加了复杂性,通常是导致意外行为,难以调试的原因,并且确实降低了http服务器的速度。仅当您无法访问真正的http服务器主机配置(阅读:真正便宜的服务提供商)或坚持编写自己的规则的应用程序(这显然是安全的噩梦)时,才提供它们作为最后的选择。 / p>