How to redirect links with response code 500 and 404 using htaccess?

by dustin.green , in category: SEO , 7 months ago

How to redirect links with response code 500 and 404 using htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by aniyah.green , 7 months ago

@dustin.green 

To redirect links with response codes 500 and 404 using htaccess, you can use the following code:

1
2
3
4
5
6
7
ErrorDo***ent 404 /error-404.html
ErrorDo***ent 500 /error-500.html

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /error-404.html [L,R=301]


In this code, the first two lines set up custom error pages for the 404 and 500 error codes. You can replace "/error-404.html" and "/error-500.html" with the URLs of your own custom error pages.


The next four lines of code enable the RewriteEngine and set up conditions for the redirect rule. The RewriteCond lines specify that the redirect rule should only apply if the requested file does not exist as a regular file or directory.


Finally, the RewriteRule line specifies the actual redirect rule. In this case, it redirects all requests that meet the conditions to the "/error-404.html" page with a 301 (permanent) redirect. You can modify the RewriteRule to redirect to the appropriate error page for the specific error code.