How to redirect all traffic to non www https with .htaccess - .htaccess

I have domain registered in ovh. My site files are stored in folder called "www" in root directory. It was original ovh setup. A few days ago I discovered that i can use let's encrypt ssl certyficate. Now I have these addresses:
example.com,
www.example.com,
https://example.com,
https://www.example.com
I want to redirect:
example.com
www.example.com
https://www.example.com
to:
https://example.com
via .htaccess 301 redirection
I tried a lot of sample htaccess redirections from this and other sites. Non of them work perfectly for me.
For example redirection from example.com to https://example.com was ok, but from www.example.comtake me tohttps://www.example.com/wwworhttps://www.example.com with browser warning "unsafe content".
It is harder than i thought.

If you're using wordpress, you could install a simple pulgin called Really Simple SSl which will automatically do this redirection.

Related

301 redirects clashing possibly with CloudFlare

My old 301 redirects in .htaccess are no longer working because something is redirecting things twice. Example of redirect:
Redirect 301 /oldpage.html //www.example.com/newpage.php
This used to work, but now it goes to:
https://www.example.com//www.example.com/newpage.php
In CloudFlare, I have a Page Rule for the www redirection:
https://example.com/*
Forwarding URL (Status Code: 301 - Permanent Redirect, Url: https://www.example.com/$1)
And my "Always Use HTTPS" in CloudFlare is on. What could be causing the double redirect?
I put one of the URLs into a redirect checker http://www.redirect-checker.org/
and noticed that for some reason the URLs go back to http, then back to https again. So I went back and edited my .htaccess file to explicitly include the https:
Redirect 301 /oldpage.html https://www.example.com/newpage.php
And now the redirect works as expected, at least in the incognito window. The regular browser isn't yet reflecting the change.

301 redirect from one domain to another but only for specific pages

We are rebuilding a website for a client from ASP to WordPress. This website will have a different domain, url structure, and file extension. I am only just getting my head around htaccess 301 redirects, and I know enough that I can't do the following:
Redirect 301 http://www.site1.com/about_us.asp https://site2.com/about/
Redirect 301 http://www.site1.com/art-specs/ https://site2.com/specs/
Redirect 301 http://www.site1.com/page/product1/ https://site2.com/product1/
There are about 12 links in total that need to be redirected, and I want to make sure that it is done right the first time as a client's SEO rankings are on the line.
Is there a variation of the above format that I could use? Or a rewrite rule that needs to be done first? Any help (and explanations) would be greatly appreciated!
After looking more into it, I realised that the htaccess file shouldn't need anything other than relative access to the original domain.
i.e. You shouldn't need to declare: http://www.site1.com/about_us.asp since the server and domain should be configured in such a way that /about_us.asp means the same thing.
So the correct answer would be to:
[1] Configure the server (in my case cPanel) by having the original domain added as an addon domain (e.g http://www.site1.com/).
[2] In the htaccess file I would add each of the 301 redirects to the htaccess file:
Redirect 301 /about_us.asp https://site2.com/about/
Redirect 301 /art-specs/ https://site2.com/specs/
Redirect 301 /page/product1/ https://site2.com/product1/
...for each redirect
[3] And finally, adding the following to the bottom of the htaccess file will catch everything else and redirect them to the home page:
RedirectMatch 301 .* https://site2.com

Redirect entire site to other url with htaccess

I have a site which you can access with or without https rule. So, I need to make a redirect to another page. Example:
My website is: www.google.com so I can access by the moment as https://www.google.com and http://www.google.com but I need when you get into my website trough http or https way you must have to redirect to www.yahoo.com.
Besides I need the url redirection too to any subdomain or url which exists in http://www.google.com or https://www.google.com
How can I do this? I have no found alredy information about it in S.O. Thanks.
Very simple. You can use the following Redirect in your htaccess to redirect all requests from your site to the other domain
RedirectMatch 301 / http://yahoo.com/

htaccess subdomain redirect to non www

For our shop CMS we have a system that works like this:
When a customer registers they will get a subdomain on our root domain.
Something like: myshop.daretoshop.nl
For SEO we want the users to be redirected to the non www version of this subdomain when they enter www.myshop.daretoshop.nl, so they are redirected (301) to myshop.daretoshop.nl
Is this possible with htaccess? We want this so happend with all subdomains, so maybe its possible to set this globally?
thanks in advance.
Use mod_rewrite.
For mod_rewrite directives to work using .htaccess, you need to set AllowOverride FileInfo. Details about how to configure mod_rewrite for your specific needs can be found in one of the similar questions, such as Generic htaccess redirect www to non-www.
You also need to configure your DNS server such that the subdomains are actually resolved to the IP address of your web server.
BTW there are absolutely no SEO benefits in redirecting http://www.example.com to http://example.com.

Apache mod_rewrite condition for hosted domain with SSL

I have five hosted domains with a multi-domain SSL certificate. I cannot write an .htaccess condition that captures both http and https conditions for CNAME www.altdomain.com (a secondary domain). When a user types "https://altdomain.com" in the browser location bar, it shows "connection untrusted" and the URL remains as is. Typing the same with http correctly redirects to https://www.altdomain.com and when the https is omitted, "altdomain.com" is correctly redirected as well.
I am using joomla with a configuration that hides a subdirectory from the URL. The doc root .htaccess file contains this in the custom redirect section:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^altdomain\.com
RewriteRule ^(.*)$ https://www.altdomain.com$1 [R=301,L]
Activating a joomla SSL plugin while omitting custom redirect rules in .htaccess also works, but "https://" with "altdomain.com" fails to redirect in all cases. Perhaps I should have omitted www from the CNAME but there would be a redirect issue there as well if anyone types www in the URL, and I can't change the CNAME without purchasing a new SSL certificate. If there is no .htaccess solution to this problem as I suspect, please explain why.
The standard redirect between www and non-www URLs works only on the primary domain or for http connections. For secure / https connections on secondary domains, there must be an exact CNAME match on the SSL certificate - redirection to a host name that is not listed as a CNAME on the certificate will not work even if the domain name is the same.
If I want https connections to redirect from altdomain.com to www.altdomain.com and vice-versa on a multi-domain SSL certificate, I have to include both CNAMES in the certificate. A five-domain SSL certificate is therefore limited to three domains.

Resources