Search code examples
apache.htaccesshttp-redirectmod-rewriteurl-rewriting

301 Redirect: New Domain and URL Structure


I need to set up redirects from an old domain to a new one. The URL structure will also be different since we'll be using a different CMS. I need to force HTTPS and non-www.

Is the code below correct for that case scenario?

# Needed before any rewriting
RewriteEngine On

# Force HTTPS and non-WWW
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ https://www.newdomain.com%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.newdomain.com%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

## 301 Redirects
# Page to Page
RewriteCond %{HTTP_HOST}  ^olddomain\.com$ [NC]
RewriteCond %{QUERY_STRING}  ^$
RewriteCond %{HTTPS} =on
RewriteRule ^collections/product/product-1$ https://newdomain.com/product/product-1? [R=301,NE,NC,L]
RewriteRule ^collections/product/product-2$ https://newdomain.com/product/product-2? [R=301,NE,NC,L]

What's the difference between the code above and a

Redirect 301 /collections/product/product-1 https://www.newdomain.com/product/product-1

Is one preferred over the other?


Solution

  • Have it your .htaccess file in following way.

    1st way: Fixing OP's attempts here.

    # Needed before any rewriting
    RewriteEngine On
    
    # Force HTTPS and WWW to non-www/www URLs.
    RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
    RewriteRule ^ https://www.newdomain.com%{REQUEST_URI} [L,NE,R=301]
    
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
    
    ## 301 Redirects
    # Page to Page
    RewriteCond %{HTTP_HOST}  ^(www\.)?olddomain\.com$ [NC]
    RewriteCond %{QUERY_STRING}  ^$
    RewriteCond %{HTTPS} =on
    RewriteRule ^collections/product/product-1$ https://newdomain.com/product/product-1? [R=301,NE,NC,L]
    
    RewriteCond %{HTTP_HOST}  ^(www\.)?olddomain\.com$ [NC]
    RewriteCond %{QUERY_STRING}  ^$
    RewriteCond %{HTTPS} =on
    RewriteRule ^collections/product/product-2$ https://newdomain.com/product/product-2? [R=301,NE,NC,L]
    


    2nd way: Using trick here to convert them into single rule if in case you have only collections/product/product-1 AND collections/product/product-2 rules only then try following and save one more condition/rules :)

    # Needed before any rewriting
    RewriteEngine On
    
    # Force HTTPS and WWW to non-www/www URLs.
    RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
    RewriteRule ^ https://www.newdomain.com%{REQUEST_URI} [L,NE,R=301]
    
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
    
    ## 301 Redirects
    # Page to Page
    RewriteCond %{HTTP_HOST}  ^(www\.)?olddomain\.com$ [NC]
    RewriteCond %{QUERY_STRING}  ^$
    RewriteCond %{HTTPS} =on
    RewriteRule ^collections/product/product-([12])$ https://newdomain.com/product/product-$1? [R=301,NE,NC,L]
    

    Important points related to OP's question:

    • I have removed your 2nd set of htaccess rules as follows ones. Why because your 1st ruleset itself takes care of www OR non-www both redirection to www URL, so you don't need to keep following ones.

    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.newdomain.com%{REQUEST_URI} [L,R=301]
    
    • Now coming on one more point, I have added NE flag into your Rules for keeping urls in same format(not to convert them hexa form).

    • Now coming on your question of Redirect rule Redirect 301 /collections/product/product-1 https://www.newdomain.com/product/product-1 its simple, if you keep only this 1 line then your https and www will NOT be implemented in case URLs are NOT https or www, so it completely depends on your requirement, if you need to implement https and www then keep htaccess shown above(edited on from your shown attempts). But you need to still have redirect Rule because you are going to new domain, so have it in above shown way only.

    • Also have put this in regex (www\.)? to match in case www is there(which will be there since we had done redirect previous rules) in condition under your comment ## 301 Redirects.

    NOTE: Either use 1st rules OR 2nd rules one at a time, 2nd one is preferred(if it meets my mentioned suggestion where you are having urls product-1 OR product-2)