How to make your website https only? - html

I've got a working SSL certificate, but the only way for a person to visit my https website is by typing "https://" in the search bar. I've got a .htacces file that contains the following code:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
And I also tried this line of code:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But neither of them seem to work.
I've also read that there is a meta tag that sends all users to your https no matter what. But still have yet to find what the meta tag looks like.
Any help is appreciated. Thank you for your time

The basic idea is correct - you will need rewrite rules on your webserver for to make the site HTTPS only
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
Please take a look at the following documentation: https://wiki.apache.org/httpd/RewriteHTTPToHTTPS
Regarding why your rules are not working - you need to know the context of .htaccess file, from documentation:
.htaccess files (or "distributed configuration files") provide a way
to make configuration changes on a per-directory basis. A file,
containing one or more configuration directives, is placed in a
particular document directory, and the directives apply to that
directory, and all subdirectories thereof.
it is however better not to use .htaccess at all if possible since it has negative performance implications and the same thing can be achieved with <Directory> blocks. All of that is in here: https://httpd.apache.org/docs/current/howto/htaccess.html
Regarding the meta tag - all you need to do is add something like this:
<meta http-equiv="refresh" content="0; url=https://yoursite.com/">
The meta tag is definitely not the best practice of HTTP-2-HTTPS redirection due to the fact that it is easy to circumvent and outside of webmasters control since it's a client side redirect.

Related

Is there a way to get all relative URL paths to serve a single absolute page URL?

What I'm looking for is a way for all paths like example.com/blog, example.com/about, example.com/burnt/toast, etc. to all go straight to example.com and get the same index.html file, from which I can respond to the different URL paths with JavaScript.
Is this possible without adding a physical redirect placeholder file for each of the extensions? Without having to use node.
What you are looking for is called a "front controller." It is usually implemented in .htaccess using a rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
That rule says: "except for files that actually exist, handle all URL paths with index.html."

send id via url but hidden

I have an html link:
Link
I formatted the link destination with a htaccess file:
RewriteRule ^destination$ index.php?content=destination [L,NC,QSA]
The question is: How can I give a ID via URL to the destination?
the full link will be index.php?content=destination?ID=x
X will be a dynamic number
but I would like to show this in the url - it should be invisible.
But I don't know how I have to modify the rewriteRule to realize it.
Although not a pretty solution and it obviously comes with flaws, you could always use cookies.
set a cookie in PHP:
setcookie("page_id","987987");
and look for it in htaccess
RewriteCond %{HTTP_COOKIE} ^page_id=([0-9]*)$ [NC]
RewriteRule ^destination$ index.php?content=destination&id=%1 [L,NC,QSA]

How to force loading extension ".html"after a url via .htaccess?

How could I achieve this?
force load extension via .htaccess ".html" after a url?
Pretty simple, but I couldn't find something a stack article on this or something similar to tweak accordingly.
If someone visits Domain.com/about I want them to land on Domain.com/about.html
Simply put I want to force all url's to load .html after the url via .htaccess.
This is to cover visitors, etc. with old links that don't have .html at the end of the url.
Does this page helps you:
https://www.garron.me/en/bits/add-html-extension-nginx-apache-htaccess.html
Or take a look here, you can find a bunch of propositions.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

Trouble with simple htaccess RewriteRule to remove %23 from end of url

I started seeing 404 errors in my logs for urls like:
http://site.example.com/foo/bar.html%23anchor
Clearly the #anchor was being encoded (probably in someone else's email that I can't control) resulting in broken links. The anchor links are not that important, but I don't want my users seeing 404 pages. I thought I could fix this with a simple rewrite, but nothing I have tried has worked and none of the SO answers I looked at worked.
The rewrite codes I tried worked perfectly in RegExr and regex101, but when I try it in my .htaccess, the bad link still results in a 404 error. I have other RewriteRules working, but I can't seem to remove the unwanted %23anchor from the end of the request.
RewriteEngine on
RewriteBase /site
## Externally redirect non-canonical domain requests to canonical domain. ###
## This rule works ###
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://site.example.com/$1 [R=301,NC,L]
## This rule doesn't work ###
RewriteRule ^(\.html)(%23)(.*)$ $1 [R=302,NE,L]
I need to change this:
http://site.example.com/foo/bar.html%23anchor
into this:
http://site.example.com/foo/bar.html
What am I missing?
You are missing everything in front of ".html". Try this rewrite:
RewriteRule ^(.*\.html)(%23)(.*)$ $1 [R=302,NE,L]
https://regex101.com/r/fV3oU3/1
Thank you for all of your suggestions, but none of them solved the initial problem. Because the RewriteBase rule affects all relative rewrites, I could not see a way to write this rule as I originally intended. In the end the only thing that seemed to work was to rewrite to an absolute path. This is the rule I ended up with. It is not flexible and only works for this page, but at least it fixes the specific broken link I am currently trying to correct:
RewriteRule ^(.*bar\.html)\x23.*$ http://site.example.com/foo/bar.html [R=302,NE,L,NC]
The above rule rewrites this: http://site.example.com/foo/bar.html%23anchor
to this: http://site.example.com/foo/bar.html
Replace your last rule with this:
RewriteRule ^(.+?\.html)\x23 /$1 [R=302,NE,L,NC]
%23 is matches by \x23 in RewriteRule.

How to link to pages without the .html extension?

I would like to link to pages inside my website, e.g:
Not: mywebsite.com/about.html But: mywebsite.com/about/
I've seen various websites doing this but it looks like they also react differently to things:
Apple.com:
apple.com/iphone/ works, apple.com/iphone/index.html works, apple.com/iphone redirects.
Opera.com:
opera.com/mobile/ redirects, opera.com/mobile works, opera.com/mobile.html does not work.
Mozilla.com:
mozilla.org/en-US/ works, mozilla.org/en-US redirects, mozilla.org/en-US/index.html does not work.
Which leads to another question: Are there different methods for this?
Edit:
It seems that Apple uses a folder for every page, e.g. a folder called 'iphone' with an index.html file inside it?
But Opera and Mozilla use something in the .htaccess file?
Removing Extensions
To remove the .php extension from a PHP file for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want to remove the .html extension from a html file for example yoursite.com/wallpaper.html to yoursite.com/wallpaper you simply have to alter the last line from the code above to match the filename:
RewriteRule ^([^\.]+)$ $1.html [NC,L]
That’s it! You can now link pages inside the HTML document without needing to add the extension of the page. For example:
wallpaper
They are using .htaccess and URL rewriting. This is part of server configuration. You can not do it with html only.
This page explains basics of URL rewriting.
You folder then has to contain a file: index.*.
Like: /iphone/index.html, which can be /iphone/ as well
Or work with .htaccess
In the .htaccess file in your sites root folder just add the following line:
# ---- Render pages without urls
Options +MultiViews
The most upvoted answer doesn't check whether the URL points to a directory, so you're going to get some mysterious 'not found' errors when it tries to append '.html' to a directory path. Easily fixed:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
The first condition will only pass if the path does not point to a valid directory. The second will only pass if the path points to a valid file when the .html extension is added. If both conditions pass, the rewrite rule simply adds ‘.html’ to the filename.
Notice that we can just match the entire path with .*. You can reject paths that contain a period character if you wish, but it's not really necessary since you've already checked that {REQUEST_FILENAME}.html is a valid file. In any case, it is unnecessary to escape a period character when it's inside a character class. I know you see this [^\.] everywhere, but the slash is redundant. [^.] is how to write it and look like a regex pro. 😎
This kind of redirect will be invisible to the user because, by default, mod_rewrite does the substitution internally, without informing the browser. If you wanted to do a permanent redirect, you would add the [R=301] flag at the end.
Alternatively, as Genus Amar said, you can just enable the Multiviews option on a per-directory basis by adding this Options Directive to the .htaccess file:
Options +MultiViews
It's worth adding that this will only work if the server administrator has enabled MultiViews with the AllowOverride Directive, and it won't allow you to perform additional redirects.
Neither of these solutions (on their own) will remove the .html if it’s part of the requested URL. If you want to do that as well, see my answer to this question.
Make your href attribute equal to the page you want to link or .. If you need to
move up a directory.
Ex: href="contact.html"
Ex: href="../links/contact.html"