简体   繁体   中英

Rewriting /foo to /foo.php doesn't work

I want users to be able to go to /foo and have /foo.php displayed. 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. 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

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

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:

Options +MultiViews

You get this issue when you use .htaccess in a subfolder. Fix it by using full paths from the web root in your .htaccess file. For instance, in the folder http://www.mysite.com/test/ your .htaccess file would look like this:

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

-not tested, might need tweaking-

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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