Mod_rewrite 导致 404 错误
这些是来自我的两个本地主机站点的 .htaccess 文件。
虚拟主机 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 ^(.*)\.php$ http://mysite1.com/$1 [R,NC]
但是,当我尝试 http://www.mysite1.com/ testfile.php - 它重定向到 http://mysite.com/testfile ,因为它应该这样做,除了显示此消息:
未找到
在此服务器上找不到请求的 URL /testfile。
如何解决此错误消息并确保我的 .htaccess 文件正常工作?
我设法很好地理解 .htaccess ,关于阻止蜘蛛等事情,但这给我带来了一些问题,有人知道出了什么问题以及我如何防止将来再次发生此错误?
感谢所有帮助。
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 ^(.*)\.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你可能会尝试倒退。尝试这样的事情:
I think you might be trying to do it backwards. Try something like this:
打开 MultiViews
不仅适用于 .php 文件,还适用于客户端声称在请求中接受的所有文件。请参阅此处。请注意,Apache 中错误但有效的配置过去给 MultiViews 带来了坏名声,请参阅此处< /a> 以确保 Apache 已正确配置。
如果您想使用重写规则,我会这样做(未经测试):
QSA
标志是必要的,以便您不会丢失查询字符串Turn on
MultiViews
withThis 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):
The
QSA
flag is necessary in order for you not to lose the query string