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

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]

Related

want to redirect an html page as sub page of other page

Like in wordpress you can make a page as sub page of other page how can we do it with HTML files with the help of .htaccess?
I have HTML pages on my website like this
abc.com/1.html and abc.com/2.html
I want to remove the .html extention like this
abc.com/1 and abc.com/2
And I want abc.com/2 to redirect to
abc.com/1/2 Please do remember the 1 in this url is not a folder.Both these urls abc.com/1 and abc.com/2 are in the same folder.
How can I do it.
To mask the file extensions, first enable MultiViews into your VirtualHost configuration file or .htaccess:
Options +MultiViews
Then, you could just provide hyperlinks without the .html extension. The HTTP server will search for file by adding extension itself. But you can also add a rewrite rule in your .htaccess:
RewriteCond %{THE_REQUEST} /([^.]+)\.html
RewriteRule ^ /%1 [R=301]
Here you get the %{THE_REQUEST} and parse it to rewrite URL without the .html at the end of the filename.
To redirect 1/2/ to 2/:
RewriteRule ^1/2/(.*)$ 2/$1
Please note that in this second rule, you do not use the [R] flag because you do not want to change URL in the browser.
Be sure to enable RewriteEngine before using rewrite rules:
RewriteEngine On
For more information about URL rewriting: https://httpd.apache.org/docs/2.4/rewrite/intro.html
In order to use regex better to use mod_rewrite which is more powerful than mod_alias.
Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^answer-now(/.*|)$ http://www.abc..net/2/? [L,NC,R=301]

How to remove .html when someone visit your static website

Right now I have a static website that is served by nginx.
Example page: www.mydomain.com
If I or anyone visits www.mydomain.com, it shows as www.mydomain.com/index.html.
How can I prevent the trailing .html and end up with www.mydomain.com/index showing up instead?
I can find many questions talking about similar issues but none of the proposed solutions worked.
Thanks
You can redirect the www.yourdomain.com/index.html to just www.yourdomain.com using the .htaccess file
Here is a guide to redirect and rewrite URLs using .htacess
Removing the index.html and the .html is something different.
For the index.html you need a "redirect" in your htaccess file
RewriteEngine On
RewriteBase /
# redirect html pages to the root domain
RewriteRule ^index\.html$ / [NC,R,L]
Now, for the .html is not working the same way. If you add the below
RewriteEngine On
RewriteRule ^/?about/?$ about.html [L]
RewriteRule ^/?otherlink/?$ otherlink.html [L]
then when you visit www.mydomain.com/about it will "read" the www.mydomain.com/about.html . So you need to add the above in your htaccess file. Check that is working (by just typing the URL ) and then change all your links to the one without the file extension.

webpage won't work unless I add .html to the end of it

This may be a dumb question, but after I load my webpages through GoDaddy's cPanel, every page won't work unless it has a .html at the end of it. Am I doing something wrong when I'm uploading it? It doesn't seem normal to need .html at the end of every page.
The files are probably written in HTML which is the most common. So if you are making new windows you must add .html at the back so the browser knows it needs to read html code and not something else. So if you were to make an "about us" page you need to save it as about-us.html
I might not have asked the question as clearly as I should. I ended up figuring out the answer regarding removing the .html from the webpage url.
Create a file called .htaccess in the root directory and add this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
And then all the links on your site should look like this:
<a href="/portfolio">

Not having `.html` in Website URL?

How is it possible to have a url of a basic website, not include the .html at the end of a webpage?
My website is just a few pages, but I don't like having the .html in my link: www.mywebsite.com/mypage.html
If I type: www.mywebsite.com/mypage, of course, it's not found :(
I navigate around my site just using typical hyperlinks.
Probably a dumb question, but I've googled it and the stuff I find is way more complex than just html code and is usually referring to php or perl.
Thanks.
You sinply make an .htaccess file and put the following in it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
What web server software are you using? If Apache, you will likely be interested in the rewrite mod.
Something like the following:
RewriteRule ^mypage/?$ mypage.html [NC,L]
In your .htaccess file. I believe you need to enable the mod first too.
Simple solution is to change the name of your website from mypage.html to mypage

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"