mod_rewrite not works with http:// but not http://www - .htaccess

I have the following code:
#### Redirect to www location
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sspconf.com [NC]
RewriteRule ^(.*)$ https://www.sspconf.com/$1 [R=301,NC]
When I type in http://sspconf.com/ it re-directs to https://www.sspconf.com/ which is fine.
However if I type http://www.sspconf.com/ (with a www) it doesn't re-direct. Can someone help?

Change your rule to:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sspconf.com [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.sspconf.com/$1 [R=301,NC]
So that it redirects if it is missing the "www" OR if it isn't HTTPS.

Add this line:
RewriteCond %{HTTPS} off
and change this line:
RewriteCond %{HTTP_HOST} ^sspconf.com [NC]
to this:
RewriteCond %{HTTP_HOST} ^(www\.)?sspconf.com [NC]
The ^ only matches the start of the string, so www.sspconf.com is a non-match.
So, your overall rule will look like this:
#### Redirect to www location
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?sspconf.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.sspconf.com/$1 [R=301,NC]
Edited to address comment

The following has solved my problem, thanks for the help.
# Force SSL on entire site
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.sspconf.com/$1 [R,L]

Related

.htaccess rewrites results in www.www

What I want to achieve should be quite simple:
Redirect all traffic to HTTPS and the www. subdomain. And to achieve this I used the following rule:
# Canonical https/www
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ([^.]+)\.domain.com [NC]
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^(.*) https://www.%1/$1 [R=301,L]
</IfModule>
However, if I follow a link like this:
http://www.example.com
I end up here:
https://www.www.example.com
So then I found this question: .htaccess: http://www redirects to www.www
And I completely replaced the rule I used above with the rule suggested in the accepted answer:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^(.*)$ https://www.%1%{REQUEST_URI} [R=301,L]
However now I get the ERR_TOO_MANY_REDIRECTS error, and the site completely refuses to load.
Can someone help me out here?
Check this rewrite in your .htaccess file, maybe it help
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

Redirecting from IP address to domain name in HTTPS via .htaccess

I am trying to Canonicate an IP to redirect to the domain name, but for some reasons it won't work and constantly having https://www.111.111.111.11.
Here is what I am using in my .htaccess
RewriteEngine On
RewriteBase /
#Uncomment lines below when uploading to the live server
RewriteCond %{HTTP_HOST} ^111\.111\.111\.11$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.org$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} !^(www\.)example\.org$ [NC]
RewriteRule ^.*$ https://www.example.org[L,R=301]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https://(www\.)?example\.org/.*$ [NC]
RewriteRule \.(gif|jpg|png|tif|js|css|xls|xlsx|zip|rar|pdf|ods|ots)$ - [F]
Additionally, plesk takes care of a safe SEO redirect from example.org to www.example.org and also from http to https.
I am not very familiar with .htaccess rules, but even to me it looks like I have some unnecessary lines in there.
Can anyone please help?
Thanks in advance
In public directory, you must be sure that 000-default.conf file has AllowOverride All set, so the settings in your .htaccess file will be used.
Use these following in your .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^111\.22\.33\.44$
RewriteRule ^(.*)$ http://www.domainname.com/$1 [L,R=301]

301 redirect http to https with www

I have to redirect my domain from http to https. In my htaccess I have already.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
This snippet redirect everything without "www" to "www".
When I change this to
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
The result is:
http://www.example.com/folder/page.php
becomes
Location => https://www.example.com/folder/page.php
Fine!
https://example.com/folder/page.php
becomes
https://www.example.com/folder/page.php
Fine!
but:
http://example.com/folder/page.php
becomes
Location => https://example.com/folder/page.php
but it has to be
Location => https://www.example.com/folder/page.php
How is it possible to fix this?
I know all of this redirects:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
but I need only one redirection instead of two 301.
You can use the following rule
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{RERUEST_URI} [NE,L,R=301]
Clear your browser cache before testing this.
I have found a solution here:
https://webmasters.stackexchange.com/questions/84757/htaccess-redirect-non-www-to-www-with-ssl-https
The second answer by #w3dk is working.
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Your site needs to be accessible by both www and non-www over SSL for the .htaccess redirect to trigger.
With letsencrypt its no problem to have this 2 certs.

Redirect www and index.html to root non-www .htaccess

I want to make a rule in my .htaccess file, to redirect my www.example.com and
www.example.com/index.html to example.com
Thanks
You can combine both requirement in a single rule like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{THE_REQUEST} /index\.html[?\s] [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*?)(?:index\.html)?$ http://%1/$1 [L,R=301,NE,NC]
Create a .htaccess file. And add this:
#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.YOURDOMAIN\.com [NC]
RewriteRule ^(.*)$ http://YOURDOMAIN.com/$1 [L,R=301]
thiis should work seamlessly
RewriteCond %{HTTP_HOST} ^www\.sitename\.com [NC]
RewriteRule ^(.*)$ https://sitename.com/$1 [L,R=301,NC]

301 redirection - front page to subdirectory

I needed to redirect my od domain to a new one. All paths are same on both domains except the front page, which needed to be redirected from www.mydomain.com to www.mydomain2.com/newpath. I googled and came up with this code which works. My question is if it is valid and if all pageranks will be transfered without problems. Thank you
RewriteEngine on
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.domain1.com/folder/ [L,R=301]
RewriteRule ^(.*)$ http://www.domain1.com/$1 [L,R=301]
Your code should work but it can be fine tuned a bit. Please consider this refactored code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
Rewriterule ^$ http://www.domain1.com/folder/ [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ http://www.domain1.com%{REQUEST_URI} [L,R=301]

Resources