Home > OS >  .htaccess does not activate 404 rule when entering wrong URLs
.htaccess does not activate 404 rule when entering wrong URLs

Time:10-23

I have some rules working fine for accessing categories/subcategories/products

But if I enter a non existing URL, instead of activate the 404 rule, access to principal.php

also for images and other extensions How can I cancel the rules for all calls except the following ones:

  • mydomain.com/MyCategory
  • mydomain.com/MyCategory/MySubcategoy
  • mydomain.com/MyCategory/MySubcategoy/MyProduct

The rule should not apply to URL's with extensions (e.g *.html, *.css, *.php, *.pdf, etc) And these extensions should not appear

example

mydomain.com/contact.php

should be

mydomain.com/contact

this is my .htaccess

ErrorDocument 404 file-not-found.html
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^.]*\.html)/?$  $1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^.]*\.php)/?$  $1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/] )$ principal.php?cat=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/] )/([^/] )$ principal.php?cat=$1&sub=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/] )/([^/] )/([^/] )$ principal.php?cat=$1&sub=$2&prod=$3  [L]

CodePudding user response:

With your shown samples and attempts please try following .htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.

Since you are not expecting few formats(eg: html, php etc) then from your current Rules set we can remove 1 set of rules and keep it one so total would be 4 rules set.

ErrorDocument 404 file-not-found.html
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(?:html|css|pdf|php)/?$
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$  $1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(?:html|css|pdf|php)/?$
RewriteRule ^([^/] )$ principal.php?cat=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(?:html|css|pdf|php)/?$
RewriteRule ^([^/] )/([^/] )$ principal.php?cat=$1&sub=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(?:html|css|pdf|php)/?$
RewriteRule ^([^/] )/([^/] )/([^/] )/?$ principal.php?cat=$1&sub=$2&prod=$3  [L]
  • Related