【发布时间】:2013-11-05 06:10:35
【问题描述】:
我发现了很多关于删除 URL 中的 .php 扩展名的教程和问题。我已经尝试了很多示例,直到现在它仍然无法正常工作。这让我发疯。我还想在 URL 的末尾加上斜杠,但首先我想先实现这一点。目前我已经尝试过这是我的一些解决方案:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
还有这个:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
没有什么是好的。我正在使用 Windows 7 和 XAMPP 在我的笔记本电脑上工作。我只是创建了我的 .htaccess 文件并将其放在 localhost/myFolder 中。当我在浏览器中运行时,根据我的解决方案,(i)有时我找不到页面,(ii)有时服务器内部错误。此外,当我运行例如 localhost/myFolder/index 时,URL 将被重定向到没有 myFolder 的 localhost/index。我想知道为什么会发生这种情况,并希望有人可以给我链接或解决方案,因为我尝试了很多。提前谢谢你。
【问题讨论】:
-
RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA] 请试试这个
-
@KristerAndersson 谢谢,对不起,我不明白 RewriteCond Backreference 部分,你能举个例子吗?我是新手。
-
@Asif 我的示例出现服务器错误。
-
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] 它看起来像 yoursite.com/test.php 到 yoursite .com/test
-
@Asif 谢谢但还是同样的问题,它将我重定向到 localhost/index 而不是 localhost/myFolder/index。即使我手动放置“myFolder”,页面显示页面未找到。
标签: php html apache .htaccess mod-rewrite