Attempting to remove "www" and force "https" on domain and trying to make it do both - html

RewriteEngine On
#Remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteCond %{HTTPS}s/%1 ^(on(s)|offs)/(.+)
RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]
#force https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#ignore folder for removing slash & removing .html
RewriteRule ^(whmcs) - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
So this is my current code. Here is my results:
website.com --> https://website.com
http://website.com --> https://website.com
www.website.com --> https://website.com
http://www.website.com --> https://website.com
All good except...
https://www.website.com --> https://www.website.com <--No good.
I have been researching and attempting to rewrite this to no success. Please any help and if you would be so kind to leave documentation so I could read further. Either way I need a solution. Thank you!

You have written so many re-writing conditions to remove www and force https. If they not any other specific needs then you can just try this simple solution
RewriteRule (.*) https://website.com/$1 [R=301,L]

I had the same issue before, here is how i fixed it.
### WWW & HTTPS
#Remove WWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
### WWW & HTTPS

Related

Rewrite rules for .htaccess

I have a basic question about the .htaccess file.
Currently mine consists of:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
Redirect all links to the hypertext transfer protocol secure (https) of the website.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([^\.]+)$ $1.html [L]
Rewrite all .html files without their file extension.
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^/?(.+)/?$ /$1/index.html [L]
RewriteRule ^/?(.+/)index\.html$ /$1 [R=301]
Remove the index.html from site URLs. Should I remove the file extension (.html) because of the 2nd rule?
ErrorDocument 404 /404.html
Self-explanatory. Should I remove the file extension here as well?
And overall - is the code correct, is this the way it should look like in my .htaccess file?
I appreciate your help.
I ended up using this code:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([^\.]+)$ $1.html [L]
ErrorDocument 404 /404.html
The index.html remove from URLs rule was useless, because I changed my links in the html part:
Homepage
to:
Homepage
and analogical:
English Homepage
to:
English Homepage

How to add HTTPS via .htaccess file

I have installed an SSL on my website and I have a .htaccess file that has some conditions already written. I have looked at a number of methods shown online on how to force https. However, none of them seem to work. I keep having broken links.
The code I am currently using, the rule commented out does not work for https
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
#This condition doesn't seem to work
#RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect non www. to www.
#RewriteCond %{HTTP_HOST} !^www\.MYWEBSITE\.co\.uk
#RewriteRule (.*) http://www.MYWEBSITE.co.uk/$1 [R=301,L]
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1? [R=301,L]
# Remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php)
RewriteRule ^(.+)\.php /$1 [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
If you would like to force HTTPS you can use the following:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

remove www and html while forcing https

How would i remove www from my url and force https and remove .html all at the same time?
RewriteEngine on
# force ssl
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
# Remove .html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
Try adding the following to your .htacces instead of your code above.
RewriteEngine on
#Force HTTPS.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Start rewrite.
RewriteBase /
# Load page without extension.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
# 301 While Rewrite.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
# Handle Trailing Slashes.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R,L]
# Rewrite rule to add .html extension back internally.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+[^/])$ $1.html
This will result in http://www.example.com/test.html to be handled by the browser as https://example.com/test while still loading the test.html file.
Put the following code at .htaccess in main directory
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://yoursite.com/$1 [L,R]
#the code above will redirect the entire site into https without www unless the request come with https://www .
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^ https://yoursite.com/$1 [L,R]
#The code above will catch any request with www if passed first code.
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.html[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
#the code above will remove any .HTML.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
# you can use the two lines if file without extension not work and you want to map them to .html files and if you want to go the same file .html without extension keep it as it is but , If you want to change it to same name .php file , replace $.html in the last line to $.php .

htacess: triple rewrite HTTPS + no-WWW + no-.html

I am trying to write a multi rewrite function in htacess.
change http into httpS
remove www
remove .html
It should handle all the cases:
http://www.example.com/about.html
should be rewritten as
http S://example.com/about
But of course if there is only one item to rewrite it should work too:
http://example.com/about
should be rewritten as
http S://example.com/about
Here is my code (https and no-WWW work but the no-.html which is commented does not work).
Please note that when rewriting you need to use the previously rewrited URL and not what the user typed into his address bar otherwise the rule will overwrite the change that another rule did.
RewriteEngine on
#strip the www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
#remove .html if any
#RewriteCond %{REQUEST_FILENAME}.html -f
#RewriteRule ^(([^/]*/)*[^/.]+)$ /$1.html [L]
#switch to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
# Disable the server signature
ServerSignature Off
You can use:
RewriteEngine on
#strip the www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
#switch to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
#remove .html if any
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
# Disable the server signature
ServerSignature Off

Removing duplicate copies (html extension vs non-html)

Is there any way to have the first URL redirect to the second URL?
http://www.example.com/podcast/episode.html
http://www.example.com/podcast/episode
Is there any way to force the .html extension to redirect to the non-html version of the URL, so as not to show up as duplicate copies of the same page.
My htaccess code at the moment is:
RewriteEngine On
RewriteBase /
#removing trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
#non www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#shtml
AddType text/html .html
AddHandler server-parsed .html
#html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
#index redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://example.com/ [R=301,L]
Is this possible?
Not entirely sure if I understand the question, but if you want anything ending in .html to redirect to the same URL without the .html, this should do the trick:
RewriteRule ^/(.*)\.html$ /$1 [R=301,L]
This is how you can do the redirection from html to non-html only.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (/[^\ ]+)\.html\ [NC]
RewriteRule ^ %1 [L,R=301]