trouble htaccess redirects non-www page - .htaccess

newbie on htaccess and redirects, I hope someone can help me with this. At the moment all but one http urls are redirecting correctly to https.
The following script has been updated as requested to remove the duplicate, the problem still persists.
Examples followed:
http://example.com redirects to https://www.example.com/
http://www.example.com redirects to https://www.example.com/
http://subdomain.example.com redirects to https://subdomain.example.com
Trouble comes when
http://example.com/somepage this only redirects to https://www.example.com/
Following is a snip from my htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteBase /
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^admin$ admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*\.php)$ $1 [L]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule . index.php [L]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This is a valid way of redirecting all non https:// calls to their secure versions. Using this would redirect http://example.com/somepage to https://www.example.com/somepage
Perhaps your 301 is just cached? Clear your cache and try again. I would recommend using [R=302] flags until you confirm the redirects are working and then update your .htaccess to use 301's.

Related

htaccess redirect stops working after SSL installation

My .htaccess file used to be successful in redirecting traffic from www.mysite.com or mysite.com to www.mysite.co.uk
Then I installed an SSL certificate on www.mysite.co.uk
Subsequently, anyone attempting to reach www.mysite.com or mysite.com received "This site is not secure" or "Warning: Potential Security Risk Ahead" warnings.
My question is whether this can be avoided in the way the redirection is set up in the .htaccess file
The original .htaccess entries that did work were:
# 301 Redirect 1
RewriteCond %{HTTP_HOST} ^web\.mysite\.com$
RewriteRule (.*) http://www.mysite.co.uk/$1 [R=301,L]
# 301 Redirect 2
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
RewriteRule (.*) http://www.mysite.co.uk/$1 [R=301,L]
# 301 Redirect 3
RewriteCond %{HTTP_HOST} ^mysite\.com$
RewriteRule (.*) http://www.mysite.co.uk/$1 [R=301,L]
The equivalent post-SSL .htaccess entries that produce the security warnings are as below. I believe the additional three lines in each entry are not relevant to the problem and have been added automatically for other reasons.
# 301 Redirect 1
RewriteCond %{HTTP_HOST} ^web\.mysite\.com$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://www.mysite.co.uk/$1 [R=301,L]
# 301 Redirect 2
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://www.mysite.co.uk/$1 [R=301,L]
# 301 Redirect 3
RewriteCond %{HTTP_HOST} ^mysite\.com$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://www.mysite.co.uk/$1 [R=301,L]
Any pointers as to how to get mysite.com or www.mysite.com to redirect to https://www.mysite.co.uk without browser security warnings would be very gratefully received.

Correct .htaccess to redirect all https:// pages to https://www

I want to redirect all non-www pages to https://, but only the home page does so as expected. That is, http://kraftgiftbox.com will redirect to https://www.kraftgiftbox.com/ as I expect, that's ok!
But none of the sub-level page will, like https://kraftgiftbox.com/odd-shaped-boxes.html, it won't redirect to the www version.
I had a .htaccess file as follows:
RewriteOptions inherit
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://kraftgiftbox.com/$1 [R,L]
ErrorDocument 404 /404.shtml
RewriteCond %{HTTP_HOST} ^kraftgiftbox\.com$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?$ "https\:\/\/www\.kraftgiftbox\.com\/" [R=301,L]
Can someone please help me correct the code? Thanks!
The last line of rules caused that because you only capture root request by this RewriteRule ^/?$ , to achieve what you want try this :
ErrorDocument 404 /404.shtml
RewriteOptions inherit
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?kraftgiftbox\.com$ [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://www.kraftgiftbox.com/$1 [R=301,L]
Rules above will do both , forcing https and www as well.
Note clear browser cache then test.

.htaccess file is not redirecting HTTP to HTTPS

I just purchased a UCC/SAN SSL certificate from GoDaddy and now I'm trying to get my website (mannyrothman.com) to automatically redirect from HTTP to HTTPS.
This is the content of my ".htaccess" file:
## Go Daddy servers need the Options -MultiViews code below
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Hide .html extension
## External Redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\index.html [NC]
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ %1 [R,NC]
## ## Internal Redirect
RewriteCond %{REQUEST_FILENAME}index.html -f
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ %{REQUEST_URI}index.html
# Begin Muse Generated redirects
# End Muse Generated redirects
You can use this code
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
or
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
and if you want to redirect from https://ww.stackoverflow.com/ to https://stackoverflow.com/ use this too
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.rawertest.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

htaccess to non www with SSL and file extension rewrite

I have a website that rewrites the .php file extension so:
http://example.com/about.php
becomes
http://example.com/about
I also redirect www.domain.com to non www.
Now I am going to move it over to SSL, so I want to keep those 2 rules and redirect it all to https as well.
Here is what I currently have:
# Redirect to non www version
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://example.com/ [R=301,L]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^index.php / [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteRule ^(.*)$ $1.php
ErrorDocument 404 /404.php
How can I do this without too many redirects? Thanks in advance!
Try it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L,NE]
RewriteRule ^index.php / [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteRule ^(.*)$ $1.php
ErrorDocument 404 /404.php

Trailing slash duplicate content

I don't know what i've done wrong here but can someone look at my .htaccess code and work out why I keep have duplicate content with a trailing /
I just want to redirect all http to https without www and its duplicating my content.
Im running my site on Concrete5
.htaccess code here
RewriteOptions inherit
# -- concrete5 urls start --
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule . index.php [L]
</IfModule>
# -- concrete5 urls end --
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://defrae.com%{REQUEST_URI} [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^www.defrae.com [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://defrae.com%{REQUEST_URI} [L,R=301,NC]

Resources