How to avoid a looping redirection from .htaccess?

by jacey.lubowitz , in category: SEO , 4 months ago

How to avoid a looping redirection from .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer


by terrence , 4 months ago


To avoid a looping redirection in the .htaccess file, you can use the following techniques:

  1. Conditionally redirect: When setting up a redirection, add a condition to the rule that checks whether the requested URL has already been rewritten or redirected. This can be done using the %{ENV:REDIRECT_STATUS} variable. For example: RewriteEngine On RewriteCond %{REQUEST_URI} !^/redirected-page/ RewriteRule ^old-page/$ /redirected-page/ [R=301,L] By checking if the requested URL does not start with "/redirected-page/", you ensure that the redirection only occurs once.
  2. Limit redirection to specific domains: Specify the domain(s) to which the redirection should apply. This helps to prevent redirecting URLs that are already redirected to another domain in a loop. For example: RewriteEngine On RewriteCond %{HTTP_HOST} ^(www.)?$ RewriteRule ^old-page/$ /redirected-page/ [R=301,L] This rule will only redirect requests from the "" domain, preventing loops with other domains.
  3. Use the END flag: The [END] flag instructs Apache to stop processing rewrite rules if the current rule matches. By using this flag, you can prevent further redirects from occurring. For example: RewriteEngine On RewriteRule ^old-page/$ /redirected-page/ [R=301,END] With the END flag, Apache stops rewriting URLs after the first successful redirect.
  4. Check the Request URI: Add a condition to check the original requested URL against a specific pattern before applying the redirection. This ensures that the rule is only applied if the requested URL has not already been rewritten. For example: RewriteEngine On RewriteCond %{REQUEST_URI} !/redirected-page/ RewriteRule ^old-page/$ /redirected-page/ [R=301,L] By excluding "/redirected-page/" from the condition, the rule will only redirect if the original requested URL does not already contain the redirect page.

By implementing these techniques, you can avoid looping redirections in your .htaccess file.