Home > Mobile >  Codeigniter not showing 404
Codeigniter not showing 404

Time:09-17

I want to show 404 page when someone writes a slash at the end of the URL

  • http://www.example.com/abc (Correct URL)

  • http://www.example.com/abc/ (i want to show 404 error)

CodePudding user response:

If none of your URLs end in a trailing slash and you don't need to access any filesystem directories directly then you can add the following at the top of your .htaccess file, before the existing Codeigniter directives, to serve a 404 instead:

# Trigger 404 for any URL that ends in a slash
RewriteRule /$ - [R=404]

Alternatively, you could remove the trailing slash instead. ie. redirect any URL that ends in a slash (that is not a filesystem directory) to the non-slash version of the URL. For example:

# Remove trailing slash (except for directories)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (. )/$ /$1 [R=301,L]

The above would redirect /abc/ to /abc, thus avoiding the duplicate content issue.

NB: Test first with a 302 (temporary) redirect to avoid potential caching issues.

You need to exclude directories otherwise mod_dir will try to reappend the trailing slash and you would get a redirect loop.


Aside: You need to make sure that you are consistently linking to the correct canonical URL, ie. without the trailing slash. If Google is indexing URLs with a trailing slash then it indicates it has found these URLs somewhere.

  • Related