简体   繁体   English

404页不适用于带有斜杠的链接

[英]404 page not working for links with trailing slash

I have a site with a 404 error page, set up in htaccess like this: 我有一个带有404错误页面的网站,在htaccess中设置如下:

ErrorDocument 404 http://www.example.com/error404.php

This works fine if you try to access a page that doesnt exist: 如果您尝试访问不存在的页面,则此方法很好用:

www.example.com/nonsensepage.php
www.example.com/nonsensepage.htm

But Google search has some old links from the previous site (a Joomla site), that have the following format: 但是Google搜索具有以前网站(Joomla网站)的一些旧链接,其格式如下:

www.example.com/index.php/news

And this isnt caught by my 404 page. 而这并没有被我的404页捕获。 Instead it goes to my index page and breaks the page! 相反,它转到我的索引页面并中断页面!

How can I make sure that my 404 page catches ALL wrong URLs? 如何确保我的404页面捕获所有错误的URL?

Thanks 谢谢


EDIT: 编辑:

I have already tried the duplicate question answer, (to add AcceptPathInfo Off) but it doesnt seem to work for me. 我已经尝试过重复的问题答案((将AcceptPathInfo关闭),但它似乎对我不起作用。

This is everything I have in my htaccess file: 这就是我的htaccess文件中的所有内容:

DirectoryIndex index.php

AcceptPathInfo Off
CheckSpelling Off

## EXPIRES CACHING ##
 <IfModule mod_expires.c>
 ExpiresActive On
 ExpiresByType image/jpg "access 1 month"
 ExpiresByType image/jpeg "access 1 month"
 ExpiresByType image/gif "access 1 month"
 ExpiresByType image/png "access 1 month"
 ExpiresByType text/css "access 1 month"
 ExpiresByType text/html "access 1 month"
 ExpiresByType application/pdf "access 1 month"
 ExpiresByType text/x-javascript "access 1 month"
 ExpiresByType application/x-shockwave-flash "access 1 month"
 ExpiresByType image/x-icon "access 1 month"
 ExpiresDefault "access 1 month"
 </IfModule>
## EXPIRES CACHING ##

FileETag none
ServerSignature Off

Options +FollowSymlinks

RewriteEngine On
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

ErrorDocument 404 http://www.example.com/error404.php

I would also be happy with just a solution that strips any trailing content and slashes, always directing: 我也将对仅剥离任何尾随内容和斜杠并始终指导的解决方案感到满意:

index.php/anything

to either index.php or error404.php 到index.php或error404.php

You can add this line at top of your .htaccess to send 404 for anything after a slash for .php files: 您可以在.htaccess的顶部添加以下行,以发送404以斜线显示.php文件后的所有内容:

AcceptPathInfo Off

Alternatively you can use this rewrite rule: 或者,您可以使用以下重写规则:

RewriteEngine On

RewriteRule \.php/.+$ [L,NC,R=404]

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

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