Lance
Lance

Reputation: 27

How do I point www subdomain to top level domain with Opencart?

I have just moved an Opencart 2.3.0.2 store to a new server. It was previously on a subdomain shop.domain.com and now it is on https://example.com.

It is basically working fine. If I go to https://example.com all of the front end works great. However, if I go to www.example.com or if I just type example.com into browser bar, it goes to the site, but the browser bar does show www.example.com/pages and there are small problems like icons not showing, etc, etc.

DNS - the A record for the top level domain points to the server IP address, and I have a CNAME for www that points to @ (top level domain). I have issued SSL certificates for top level and www subdomain. When customer uses www.example.com I want them to hit the site at https://example.com. I would have thought that the CNAME www record would achieve this?

When I do the same setup on Wordpress, it will force any subdomain with a CNAME @ record to hit the site with the basic top level domain https://example.com - nice and clean. What am I missing? Why is Opencart handling this differently? The Opencart store is set to https://example.com in settings and otherwise works fine.

Upvotes: 0

Views: 35

Answers (1)

redbraz
redbraz

Reputation: 11

If you want to direct those without www to www, then you need to create a condition in htaccess, something like below. Add it at the end of your htaccess

Remove WWW

## Rules remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Add WWW

## Rules add WWW
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Upvotes: 0

Related Questions