skip to Main Content

I am trying to Redirect pages to new location on the same website using .htaccess
the physical file name is displayitems.php but there is a rule in .htaccess

RewriteRule ^buy-online-(.*) ./displayitems.php?url=$1

which is to handle the user friends URLs and works well.

Now i want to redirects these user friendly urls to new location which is on the same website for eg.

redirect https://example.com/buy-online-alhabib-rings4-sku-1658906163 https://example.com/products/jewelry/buy-online-alhabib-rings4-sku-1658906163 [R=301]

redirect https://example.com/buy-online-alhabib-rings3-sku-1658906162 https://example.com/products/jewelry/buy-online-alhabib-rings3-sku-1658906162 [R=301]

redirect https://example.com/buy-online-alhabib-rings2-sku-1658906161 https://example.com/products/jewelry/buy-online-alhabib-rings2-sku-1658906161 [R=301]

redirect https://example.com/buy-online-alhabib-rings1-sku-1658906160 https://example.com/products/jewelry/buy-online-alhabib-rings1-sku-1658906160 [R=301]

these user friendly url doesn’t have any extensions like ".php" ".htm" etc

but nothing happening.

2

Answers


  1. Chosen as BEST ANSWER

    I have added this code in php file to check if url doesn't contain products than redirect it to new location with the same name, for testing i just redirect it with 302 once all tested i will change it to 301

    if (strpos($_SERVER['REQUEST_URI'], "/products/") === false) { $NewAddress = strtolower("Location:". $ini['website_address_https'] . "products/".$Product['categoriesname']."/".$Product['BrandName'].$_SERVER['REQUEST_URI']); header("$NewAddress",TRUE,302); }


  2. redirect https://example.com/buy-online-alhabib-rings4-sku-1658906163 https://example.com/products/jewelry/buy-online-alhabib-rings4-sku-1658906163 [R=301]

    There are 3 main issues here:

    1. The mod_alias Redirect directive takes a root-relative URL-path (starting with a slash) as the source URL, not an absolute URL. So the above will never match.

    2. You have mixed syntax with mod_rewrite. [R=301] is a RewriteRule (mod_rewrite) flags argument and has nothing to do with the mod_alias Redirect directive. Redirect takes the HTTP status code as an optional second argument. eg. Redirect 301 /buy-online-alhabib-rings4-sku-1658906163 ...

    3. Since you are using mod_rewrite (ie. RewriteRule) for your internal rewrite, you should use mod_rewrite for external redirects as well to avoid potential conflicts. These redirects then need to go before your internal rewrite.

    Additionally,

    1. In the 4 redirects you have posted it looks like you are simply injecting /products/jewelry at the start of the URL-path. This does not need 4 separate rules, providing you are wanting to redirect all URLs that following this particular format.

    Try the following instead:

    RewriteEngine On
    
    # Inject (redirect) "/product/jewelry" at the start of the URL-path
    RewriteRule ^buy-online-alhabib-ringsd-sku-d+$ /products/jewelry/$0 [R=301,L]
    
    # Internal rewrite
    RewriteRule ^buy-online-(.*) displayitems.php?url=$1 [L]
    

    The $0 backreference in the first rule contains the entire URL-path that is matched by the RewriteRule pattern.

    Note that I also removed ./ from the start of the substitution string in the last rule. This is unnecessary here.

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