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

by dustin.green , in category: SEO , a year ago

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

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

3 answers

by aniyah.green , a year 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
ErrorDocument 404 /error-404.html
ErrorDocument 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.

by jaycee_rowe , 4 months ago

@dustin.green 

Please note that the asterisks in the code have been added to prevent the formatting from being recognized as HTML tags. In order to use the code, you must remove the asterisks from the code.


This code should be placed in the .htaccess file in the root directory of your website. Make sure to save a backup of the file before making any changes.


If you want to redirect links with response code 500, change the following line:


ErrorDo***ent 500 /error-500.html


To redirect links with response code 404, change the following line:


ErrorDo***ent 404 /error-404.html


After making the changes, save the .htaccess file and test the redirect by visiting a page that would generate a 404 or 500 error on your website.

by clarabelle , 4 months ago

@dustin.green 

The code is missing the RewriteRule for redirecting links with response code 500. However, here is an updated version of the code that includes both the 404 and 500 redirect rules:


1 2 3 4 5 6 7 8 9


ErrorDocument 404 /error-404.html ErrorDocument 500 /error-500.html


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


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


In this updated code, the first two lines remain the same, setting up custom error pages for 404 and 500 error codes.


Following the first set of RewriteCond lines, the RewriteRule statement redirects all requests that don't correspond to existing files or directories to the "/error-404.html" page with a 301 (permanent) redirect.


The additional RewriteCond and RewriteRule block below the first one redirects all requests that don't correspond to existing files (but still pass the first RewriteCond) to the "/error-500.html" page with a 301 redirect.


Make sure to replace "/error-404.html" and "/error-500.html" with the URLs of your own custom error pages.


Remember to save a backup of your .htaccess file before making any changes and test the redirects by visiting pages that would generate 404 and 500 errors on your website.