Link to home
Start Free TrialLog in
Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on

How to always redirect to one url whether user inputs http or www in address bar.

If i type in the web address as https://somewebsite.com it works perfectly. If I type in https://www.somewebsite.com it breaks. How can I force it to always go to one url only, no matter how the user types it in the address bar?

I have this in my .htaccess file

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{HTTPS} !=on
 RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NC,L,R=301] 
 RewriteRule ^$ public/ [L]
 RewriteRule (.*) public/$1 [L]
</IfModule>

Open in new window

Avatar of spmt
spmt

hello,

 i'm think you don't need rewrite Rule , but configure VHost.

Add somewebsite.com  A record in DNS

Add www.somewebsite.com  as cname in DNS

In vhost configuration change the entry like this:

ServerName somewebsite.com

ServerAlias www.somewebsite.com 

it should work.

Thanks and Regards,
Avatar of Dave Baldwin
What spmt said above.  'www' is almost always a subdomain.  In the DNS for my sites, it is in a 'CNAME' record.
Avatar of Crazy Horse

ASKER

Not sure if this helps but the errors in console show this:

.....has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present....

It is effecting fonts which is why all my icons are just blocks and it is effecting an ajax request it looks like.
Apparently this will solve the issue for fonts and ajax. Do you guys think it would be okay to use this in .htaccess? I have asked my web host about what spmt said as well.

# Allow font assets to be used across domains and subdomains
<FilesMatch "\.(ttf|otf|eot|woff)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>

Open in new window


# Allow cross-domain access to our Ajax endpoint
<IfModule mod_headers.c>
  SetEnvIf Request_URI "/ajax/endpoint" CORS=True
  Header set Access-Control-Allow-Origin "*" env=CORS
</IfModule>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I found a solution that works:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

Open in new window

Thanks David, posted at the same time as you. Yours is a lot longer and more complicated for my tiny brain :-)