I have a WordPress site which works fine if I access it through the full name of the domain, example www.example.com. However if I try to access it without typing the www e.g example.com, I get the following error:
Origin http://example.com is not allowed by Access-Control-Allow-Origin.
The only solution I can think of is to append / rewrite any request to example.com to www.example.com. How can I achieve this with .htaccess?
Add this above any wordpress rules in your htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www\.%{HTTP_HOST}/$1 [L,R=301]
For those who encountered this with WordPress. I got it fixed by installing this plugin https://github.com/jacopotarantino/WordPress-Cross-Domain-Plugin and add the site into "Allowed Domains".
Related
I have a website hosted on GCP bucket and I use cloudflare for HTTPS.
How can I use .htaccess file to remove www. from all website pages and redirect them to HTTPS?
Basically, I want:
http://www.example.com/ redirect to https://example.com/
https://www.example.com/ redirect to https://example.com/
and any other pages like
https://www.example.com/about.html or http://www.example.com/about.html redirect to https://example.com/about.html
I found the configuration for the .htaccess file that I need to add to the root directory of my website:
Catch all and redirect www to no-www (HTTPS):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com$1 [R=301,L]
I replaced example.com with my domain name, put the file into the root directory of my GCP bucket and nothing happened. If I try to access my website by http://example.com I get DNS_PROBE_FINISHED_NXDOMAIN error. I tried to save .htaccess with ANSI and with UTF-8 with the same result.
How do I:
Remove www. from my website using .htaccess
Redirect users to https:// without www.
What encoding should I use for .htaccess? Does it matter?
Is this even possible with my GCP bucket hosted static website and cloudflare provided https://?
I tried setting up redirects through my domain registrar's control panel, but they don't support redirects with dns nameservers other than their own default ones.
The simplest solution would be to use CloudFlare to take care of HTTPS and the www site redirection.
To force HTTPS, go to CloudFlare settings: SSL/TLS => Edge Certificates => Always Use HTTPS => On.
Always Use HTTPS:
Redirect all requests with scheme "http" to "https". This applies to
all http requests to the zone.
To redirect some locations to some other locations, you can create a page rule. See example https://support.cloudflare.com/hc/en-us/articles/200172286-Configuring-URL-forwarding-or-redirects-with-Cloudflare-Page-Rules
There could be further issues at play here (It has been a while since I've messed with Apache), but the unescaped periods in your regex for the condition will definitely cause issues. Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
I am looking to 301 an entire domain on https://www.olddomain.co.uk to a new domain on https://newdomain.com via .htaccess. Currently, the version I have will only work if you manually remove the www from the old link showing in Google SERP for the old domain listing - so it shows 'This site can’t be reached' when clicked. Removing the 'www' part of the URL and hitting return performs the redirect.
Current .htacess is:-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ https://newdomain.com/$1 [R=301,L]
</IfModule>
And I've also tried:-
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.co.uk [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.co.uk [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC]
Important to keep the SEO value form the old domain. Any assistance appreciated here.
Thanks in advance
Glennboy
Fixed in 2 parts:-
The DNS settings on the domain did not cover 'www' which was a simple error that would have stopped 'www' showing anyway.
Added a valid SSL Certificate to the old domain to ensure safe traversing of SSL - SSL
In short, the issue was not the .htaccess implementation which was perfectly valid and now working fine.
So, we printed some cards but the card accidentally has www.sub.domain.com/example but when to try to go there it says the site cannot be reached but If we try to go to sub.domain.com/example it works.
The main website is made in WordPress but the landing pages (subdomains) are made using Unbounce can this be fixed using .htaccess? if so then which .htaccess file and how can we fix this.
Thank you
You need to redirect your www to non-www. That can be done by adding this to your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]
Don't forget to change yourdomain with your domain.
i have migrated a domain from olddomain.com to newdomain.com...i want all URLs to redirect from olddomain.com to newdomain.com. I got this part working. My issue now is that I have a CNAME on newdomain for help.newdomain.com -> externaldomain.com (uses referrerURL to land on proper page)
I need to redirect help.olddomain.com -> help.newdomain.com -CNAME-> externaldomain.com
Is this possible with .htaccess rewrite? if not, is there another way to skin it?
Yes, a 301 .htaccess rewrite should do it. Put this in the server that currently serves help.olddomain.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^help.olddomain.com [NC]
RewriteRule ^(.*)$ http://externaldomain.com/$1 [L,R=301,NC]
If you really want it to hit help.newdomain.com first, use that in the RewriteRule instead.
I've a domain hosted on bluehost with iLister CMS installed in, the problem which I'm facing is http://www.advett.com/admin isn't responding because I registered advett.com/admin in the site_url field of iLister license.
How to redirect all the request for www.advett.com and www.advett.com/admin to advett.com and advett.com/admin respectively?
I know it can be done with .htaccess but I've not been able to find a solution till now.
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.advett\.com [NC]
RewriteRule ^(.*)$ http://advett.com/$1 [L,R=301]