skip to Main Content

First I rewrite URLs structure instead of changing the file name.

RewriteRule ^solar-solutions-in-india.php?$ solution.php [L]

So when I try to open my link it is open from both URLs.

But for SEO purposes I want to redirect my old URL.

I.E. solution.php to solar-solutions-in-india.php with 301.

I try to redirect 301 using the below code. But it is showing too many redirections.

Redirect 301 /solution.php https://www.abc.or/solar-solutions-in-india.php

2

Answers


  1. Both redirections are generating an infinite loop of redirects, and thats why you are getting “too many redirects” error.

    First line redirects solar-solutions-in-india.php to solution.php, then, once solution.php is accessed, you are telling the server to redirect it to solar-solutions-in-india.php, and since on the first line you are redirecting it to solution.php, you have an infinite loop.

    You cant redirect page A to page B and at the same time page B to page A, im afraid you will have to remove one of them.

    Login or Signup to reply.
  2. In order to avoid the redirect loop, you need to only redirect direct requests made by the user (or search engine bot) and not the rewritten request from your later rewrite.

    Try the following instead using mod_rewrite at the top of your .htaccess file:

    RewriteEngine On
    
    # Redirect direct requests to old URL
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^solution.php$ /solar-solutions-in-india.php [R=302,L]
    
    # Internally rewrite new URL to the existing old file
    RewriteRule ^solar-solutions-in-india.php$ solution.php [L]
    

    The condition that checks against the REDIRECT_STATUS environment variable ensures we only target direct requests and not the rewritten request from the later rewrite. The REDIRECT_STATUS env var is empty on the initial request and set to 200 (as in 200 OK HTTP status) after the first successful rewrite.

    Don’t forget to escape literal dots in the RewriteRule pattern (regex).

    I removed the trailing ? on the RewriteRule pattern (ie. ^solar-solutions-in-india.php?$), as it looks like an error. Unless you really are trying to match both <url>.php and <url>.ph?!

    Note that the above is a 302 (temporary) redirect. Only change it to a 301 (permanent) once you have tested that it works OK. 301s are cached persistently by the browser so can cause problems whilst testing. You will need to clear your browser cache before testing.

    NB: Redirect is a mod_alias directive. You should avoid mixing redirects from both modules in this instance.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search