Mod_rewrite 导致 404 错误

发布于 2024-09-04 18:42:03 字数 870 浏览 1 评论 0原文

这些是来自我的两个本地主机站点的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫负雪 2024-09-11 18:42:03

我想你可能会尝试倒退。尝试这样的事情:

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

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]
许你一世情深 2024-09-11 18:42:03

打开 MultiViews

Options +MultiViews

不仅适用于 .php 文件,还适用于客户端声称在请求中接受的所有文件。请参阅此处。请注意,Apache 中错误但有效的配置过去给 MultiViews 带来了坏名声,请参阅此处< /a> 以确保 Apache 已正确配置。

如果您想使用重写规则,我会这样做(未经测试):

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

QSA 标志是必要的,以便您不会丢失查询字符串

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文