开发者

Mod_rewrite causing 404 error

These are the .htaccess files from two of my localhost sites.

Virtual host 1 (mysite1.com):

    # this is the initialization
Options         +FollowSymLinks
RewriteEngine On # Turn on the rewriting engine 
RewriteRule ^news-07/?$ news_01_06_2007.php [NC,L] 
# Handle requests for "news"
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)\.php$ $1 [nc]
#RewriteRule ^(.*)\.开发者_StackOverflow中文版php$ http://mysite1.com/$1 [R,NC]

However, when I try http://www.mysite1.com/testfile.php - it redirects to http://mysite.com/testfile as it should do, except this message is displayed:

Not Found

The requested URL /testfile was not found on this server.

How can I resolve this error message and ensure my .htaccess file works?

I'm managing to understand .htaccess well, with regard to things like blocking spiders etc. but this one is causing me some problems, anyone know what's wrong and how I can prevent this error happening again in the future?

All help appreciated.


I think you might be trying to do it backwards. Try something like this:

RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$  $1.php  [nc]


Turn on MultiViews with

Options +MultiViews

This will work not only with .php files but with all files the client claims to accept in the request. See here. Note that wrong-but-working configurations in Apache have given a bad name to MultiViews in the past, see here to make sure you have Apache correctly configured.

If you want to use rewrite rules, I'd do this instead (untested):

RewriteCond $0 !\.[a-zA-Z0-9]+$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$  $0.php [QSA]

The QSA flag is necessary in order for you not to lose the query string

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜