简体   繁体   English

将 /foo 重写为 /foo.php 不起作用

[英]Rewriting /foo to /foo.php doesn't work

I want users to be able to go to /foo and have /foo.php displayed.我希望用户能够 go 到 /foo 并显示 /foo.php。 A quick search on Google came up with this:在谷歌上快速搜索得出了这个:

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

For some reason, however, this doesn't work.但是,由于某种原因,这不起作用。 I keep getting a 404 error, even though I know the.php file exists.即使我知道 .php 文件存在,我仍不断收到 404 错误。 I searched and searched for an answer, but it seems to work fine for everyone else.我搜索并搜索了一个答案,但它似乎对其他人来说都很好。

I commented out the other rules, by the way, so nothing is conflicting with this.顺便说一句,我注释掉了其他规则,所以没有什么与此冲突的。

Any ideas?有任何想法吗?

This is what you need:这就是你需要的:

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Maybe you need to add the directive AllowOverride All in your apache configuration or maybe you need to add RewriteEngine On at the very beginning of your .htaccess file也许您需要在 apache 配置中添加指令AllowOverride All或者您可能需要在 .htaccess 文件的开头添加RewriteEngine On

You need this rule:你需要这条规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /$1.php

I found the answer.我找到了答案。 Apparently this is a problem with 1and1's server configuration: This article explains the problem: http://tips.webdesign10.com/web-hosting/why-you-should-never-use-1and1-com-hosting显然这是1and1的服务器配置的问题:这篇文章解释了这个问题: http://tips.webdesign10.com/web-hosting/why-you-should-never-use-1and1-com-hosting

The first comment also happens to be the solution.第一条评论也恰好是解决方案。 Here's the rule that works:这是有效的规则:

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]

alternatively you can use apache's MultiViews option:或者,您可以使用 apache 的MultiViews选项:

Options +MultiViews

You get this issue when you use .htaccess in a subfolder.在子文件夹中使用.htaccess时会出现此问题。 Fix it by using full paths from the web root in your .htaccess file.通过使用.htaccess文件中 web 根目录的完整路径来修复它。 For instance, in the folder http://www.mysite.com/test/ your .htaccess file would look like this:例如,在http://www.mysite.com/test/文件夹中,您的.htaccess文件将如下所示:

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /test/$1.php

-not tested, might need tweaking- -未测试,可能需要调整-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM