.htcaccess file assistance needed for canonicalization - seo

My website currently has two version to google, https://www.example.com and a https://example.com This is apparently not good for SEO as its leaking all my seo 'juice' away. I have already told google to use just the www version around 5 weeks ago but come results on google still show the non-www version. I was wondering if anyone could help with my .htaccess to get the redirect right to just www.
The htcaccess file is copied below:
RewriteEngine on
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond $1 !^(blog)
RewriteCond %{HTTP_HOST} ^`.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.`.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
RewriteRule ^(.+)$ /index.php [L]

For a simple redirect you can use:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If you want to also force HTTPS:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Note: This block goes above the www. rewrite rules to make sure both rules take hold

Related

Redirect all of the traffic to https://www. Currently I have two versions http://www and https://www

I have a brand new site, initially, it was http:// then I redirected the site to http://www it worked liked a charm, but since I installed the SSL I have two versions of website http://www.example.com and https://www.example.com.
I have tried several commands in .htaccess but still I have two versions can someone please help me with this, below is the code I am using in my .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
First www and after https:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
Try this one.
RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

mod_write htaccess with subdomains

I've been trying to figure out this htaccess issue and I can't seem to get it working and getting stuck on 500 errors.
I've looked around at multiple S.O. questions and general google but haven't found any that apply to my particular situation. Here it is:
I have example.com. I want if the user puts in: example.com or www.example.com to go to index.php.
But if the user puts in a subdomain subdomain.example.com; I want it to go to
dashboard.php?val=subdomain
Further more I'd like
subdomain.example.com/contact
to go to
dashboard.php?val=subdomain&page=contact
Here is what I have so far:
RewriteEngine On
RewriteBase /
# If doesn't start with www and it has a subdomain, redirect
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule .? - [E=VAL:%1]
RewriteRule ^/?$ /dashboard.php?val=%{ENV:VAL}&page=idx
RewriteRule ^contact/?$ /dashboard.php?val=%{ENV:VAL}&page=contact [L]
#Note: I have tried this with and without the Env variables and only having 1 RewriteRule
# The "catch all other" redirect for images, files and if they do /cont by accident
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !dashboard\.php [NC]
RewriteRule . /dashboard.php?val=%1&page=idx - [L]
There may be subdomainA and subdomainB, etc so want to do a catch all on subdomains. Hopefully the question makes sense. My Apache knowledge is more limited and most of this is from googling around.
Give the following rules a try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.example\.com$ [NC]
RewriteRule ^/?$ /dashboard.php?val=%1 [NC,L]
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/dashboard\.php [NC]
RewriteRule ^.*$ /dashboard.php?val=%1&page=$0 [NC,L]

Forcing https on the whole site but not on one folder

Could anyone tell me how I can force https on my whole website but not on a single folder or url.
At the moment I have this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^thatmysite\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://thatmysite.com/$1 [R,L]
But if I add this second code in the root htaccess to remove https from the /thatsite.com/printing folder, I get a redirect loop because I am forcing on the code http to https and not https to http...
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{REQUEST_URI} ^\/(printing)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/(printing)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
Do you know a way around this please? I have been looking all over the internet and cannot find a single good answer.
Try these 2 rules at top of your .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/printing [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /printing [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I solved this issue by placing this code in an .htaccess file the root folder of the directory that hosts the http site (e.g. public_html)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/TheFolderYouCanAccessWithoutHttp/
RewriteRule (.*) https://yourdomain.xyz/$1 [R=301,L]

Redirect from https:// to https://www using .htaccess

I am trying to get my site to redirect all traffic to https://www.mylittleblackebook.com no matter how the URL is typed into the browser. There are six ways someone can type the URL into a browser. They are:
https://www.mylittleblackebook.com
https://mylittleblackebook.com
http://www.mylittleblackebook.com
http://mylittleblackebook.com
www.mylittleblackebook.com
mylittleblackebook.com
All of these URL’s will work EXCEPT FOR the second one. I cannot get the .htaccess file to redirect https:/mylittleblackebook.com to https://www.mylittleblackebook.com. Here is the code in my .htaccess file:
RewriteEngine on
# require SSL without mod_ssl
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This code works for 5 out of the 6 ways. Can anyone tell me how to get it to work for all of them?
Thank you for taking the time to look this over.
Your code looks correct, not sure why it doesn't work. I would try this instead, it's in the opposite order and a bit different:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) https://www.%{HTTP_HOST}/$1 [L,R,NE]

Redirecting to https with wildcard urls (htaccess)

I have the following problem. I have a site where some pages must be redirected to the https-secured equivalent of the link.
I tried several things but in the end it seems that the server ends up in a loop redirecting :(
Now I redirect every page to the https page but that is not what I want to.
This is the code I use right now:
RewriteCond %{http_host} ^www.domain.nl
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.nl/$1 [L,R=301]
I would like to have al urls starting with bo- have https.
examples:
http://www.domain.nl/bo-users -> redirects to https://www.domain.nl/bo-users
http://www.domain.nl/bo-groups -> redirects to https://www.domain.nl/bo-groups
but
http://www.domain.nl/about-us -> stays at http://www.domain.nl/about-us
It is clear to me I need some wildcards in the rewrite condition for all urls startin with bo-.
We are using apache on the server.
Hope someone can send me in the right direction.
Thanks,
Frank
Update after the tips from Anubhava.
I current;y have this htaccess but I can't get it working (even when clearing the cache in my browser)
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain\.nl$ [NC]
RewriteCond %{HTTPS} on
RewriteRule !^bo- http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
RewriteCond %{HTTP_HOST} ^www.domain\.nl$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^bo- https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
#rewrites een url to www.
RewriteCond %{HTTP_HOST} ^[a-z0-9-]+\.[a-z]{2,6}$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$0 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Hope for some extra help!
Thanks,
Frank
I just opened another call. The initial answer worked fine!
Use this rule:
# force HTTPS for bo- URIs
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.nl$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^bo- https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
# force HTTP for non bo- URIs
RewriteCond %{HTTP_HOST} ^www.domain\.nl$ [NC]
RewriteCond %{HTTPS} on
RewriteRule !^bo- http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]