简体   繁体   中英

404 page not working for links with trailing slash

I have a site with a 404 error page, set up in htaccess like this:

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:

www.example.com/index.php/news

And this isnt caught by my 404 page. Instead it goes to my index page and breaks the page!

How can I make sure that my 404 page catches ALL wrong URLs?

Thanks


EDIT:

I have already tried the duplicate question answer, (to add AcceptPathInfo Off) but it doesnt seem to work for me.

This is everything I have in my htaccess file:

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

You can add this line at top of your .htaccess to send 404 for anything after a slash for .php files:

AcceptPathInfo Off

Alternatively you can use this rewrite rule:

RewriteEngine On

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

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