Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I mentioned my htaccess rewrite rule below


If I enter a wrong url, for example:

'http://example.com/hosting/web-hosti' directs to 'http://example.com/main-page.php?pagename=web-hosti' where 'web-hosti' page doesn't exist.

or

'http://example.com/contac' directs to 'http://example.com/sub-page.php?pagename=contac' where 'contac' page doesn't exist.

Can anyone please tell me how to redirect all wrong urls to 404 page?


What I have tried:

RewriteEngine ON

##Rules for existing php files rewrite.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/?$ $1.php [NC,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^hosting/(.*)/?$ main-page.php?pagename=$1 [NC,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ sub-page.php?pagename=$1 [NC,L]

ErrorDocument 404     /404.php
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /404.php [L]

Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900