TransWikia.com

Redirecting to 404 page when wrong url is entered

Stack Overflow Asked by bharav on December 7, 2021

I have a 404 error page and tried the below code which was generated from this link. But when the wrong URL is entered it redirects to the main page, not to the 404 error page

<IfModule mod_rewrite.c>
RewriteEngine On
ErrorDocument 404 https://www.rcis.in/404.php
</IfModule>

I have added the code in htaccess file.
And also how to remove the file extension from the URL. Down below is the code I tried

 #Remove extension
 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^([^.]+)$ $1.php [NC,L]

And I tried below code which was generated from that website which is mentioned above

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?(.*).(php)$ /$1 [R=301,L]

</IfModule>

How can I solve these two

Thank You in advance

One Answer

The below code will remove php extention from the URL

RewriteEngine On
# for 404 redirection
ErrorDocument 404 https://www.yourdomain.in/404
# below code rewrites php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
# Remove php extention
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /([^ ]+).php
RewriteRule ^/?(.*).php$ /$1 [L,R=301]

Hence the url https://www.yourdomain.in/404 will load https://www.yourdomain.in/404.php

Answered by Ajith on December 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP