Crazy Horse
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
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>
What spmt said above. 'www' is almost always a subdomain. In the DNS for my sites, it is in a 'CNAME' record.
ASKER
Not sure if this helps but the errors in console show this:
.....has been blocked by CORS policy: No 'Access-Control-Allow-Orig in' 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.
.....has been blocked by CORS policy: No 'Access-Control-Allow-Orig
It is effecting fonts which is why all my icons are just blocks and it is effecting an ajax request it looks like.
ASKER
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>
# 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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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]
ASKER
Thanks David, posted at the same time as you. Yours is a lot longer and more complicated for my tiny brain :-)
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,