When we make HTML pages, we generally save it with a .htm
or .htm
l extension. But most webpages on the internet have extensions like .com or .org, for example, https://example.com
. How do these pages work?
2 Answers
There are several ways to achieve this:
Create an
index.html
inside root folder of your hosting and open your domain ie.http://example.com
, you can see the output fromindex.html
onhttp://example.com
You can tell your server using
.htaccess
to display the desired file when someone opens your domain.
Create your index.html file. You will see when you publish that it will say 'example.com/page.html'. To remove the .html you need to create a .htaccess file in your root folder. Within that .htaccess file paste this code:
#Removes file extentions from URL
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
This also removes .php file extensions.
It's good to know that if you want HTTPS instead of HTTP, you also use the same .htaccess file for this.
-
This rewrite URLS with no periods that don't have a corresponding file in the file system to internally look for a
.php
file. Otherwise for such URLs that do have a file, it adds a.html
extension. (YourRewriteCond
only applies to the very next rule, not to both rules.) These rewrite are very broken and nobody should be using them. They also don't solve the whole problem. They could allow some extensionless URLs to work, but they don't remove extensions from existing URLs or change any links within your site that contain URL extensions. Feb 10, 2020 at 10:42 -
I'm not sure how you can say that, because when I use it on my websites it removes the .html and .php from the url. So it does work.– DelboyFeb 11, 2020 at 12:11
-
I would recommend changing the condition to
RewriteCond %{REQUEST_FILENAME}.php -f
and adding a second condition before the.html
rule:RewriteCond %{REQUEST_FILENAME}.html -f
. I think those would work, if you could test them and update your question. It would also be ideal to include any code you have for redirecting away from the.php
and.html
URLs and to explain how you link in your pages. Feb 11, 2020 at 12:42
.org
inen.wikipedia.org
is NOT an extension, please do not use this term, even if widely used. It is one of the label of the domain name, and since it is at the extreme right and since DNS is right to left writing basically, it is called the Top Level Domain or TLD for short.