Search code examples
.htaccesshttp-redirectmod-rewrite

Why is my .htaccess rewrite rule redirecting to the wrong place


This is the my .htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
RewriteRule ^(.*)$ https://www.myglobalsite.com/$1 [R,L]

## DIRECTORY
Options +FollowSymLinks
Options All -Indexes

DirectoryIndex index.php
ErrorDocument 404 https://www.myglobalsite.com/

redirectMatch 301 ^/products$ https://www.myglobalsite.com/en/product/drinking-cups
redirectMatch 301 ^/products/vending.php$ https://www.myglobalsite.com/en/product/drinking-cups
redirectMatch 301 ^/products/drinking.php$ https://www.myglobalsite.com/en/product/drinking-cups

The issue is when it redirects, it redirects as follows:

from: https://www.abc-something-old.com/products/vending.php

to: https://www.myglobalsite.com/products/vending.php

should redirect to: https://www.myglobalsite.com/en/product/drinking-cups

is the problem in my rewrite rule?


Solution

  • Directives in configuration files (a ".htaccess" fils is a "distributed configuration file") are processed from top to bottom.

    The first redirection rule redirects all requests to the same resource path on the new domain. That means that your RedirectMatch directives further down never come into play.

    You have to change the order of directives such that specialized, so more precise rules go to the top to get applied first . Also don't forget to correctly type the names of directives, so RedirectMatch instead of redirectMatch. More general rules, so typically catch-all rules go to the bottom to get applied as a last option if no specialized rule got applied before.

    ## DIRECTORY
    Options +FollowSymLinks
    Options All -Indexes
    
    DirectoryIndex index.php
    ErrorDocument 404 https://www.myglobalsite.com/
    
    RedirectMatch 301 ^/products$ https://www.myglobalsite.com/en/product/drinking-cups
    RedirectMatch 301 ^/products/vending.php$ https://www.myglobalsite.com/en/product/drinking-cups
    RedirectMatch 301 ^/products/drinking.php$ https://www.myglobalsite.com/en/product/drinking-cups
    
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
    RewriteRule ^ https://www.myglobalsite.com%{REQUEST_URI} [R,L]
    

    So far you are using a mixture of directives from the alias module and the rewrite module. You could unify that, though both approaches will work:

    ## DIRECTORY
    Options +FollowSymLinks
    Options All -Indexes
    
    DirectoryIndex index.php
    ErrorDocument 404 https://www.myglobalsite.com/
        
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
    RewriteRule ^/?products/?$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
    RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
    RewriteRule ^/?products/vending\.php$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
    RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
    RewriteRule ^/?products/drinking\.php$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
    
    RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
    RewriteRule ^ https://www.myglobalsite.com%{REQUEST_URI} [R,L]
    

    And by the way: you only need the RewriteCond if both domains are served by the same http server and evaluate the same configuration file to process a request. Otherwise, if those are separate servers or separate virtual hosts with separate DOCUMENT_ROOT folders (which I would strongly recommend), you can simplify that:

    ## DIRECTORY
    Options +FollowSymLinks
    Options All -Indexes
    
    DirectoryIndex index.php
    ErrorDocument 404 https://www.myglobalsite.com/
        
    RewriteEngine On
    RewriteRule ^/?products/?$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
    RewriteRule ^/?products/vending\.php$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
    RewriteRule ^/?products/drinking\.php$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
    RewriteRule ^ https://www.myglobalsite.com%{REQUEST_URI} [R,L]