How to remove .html when someone visit your static website - html

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.

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]

Load index.html when URL points to subfolder

I'd like a URL that points to sub-folder to load index.html from that folder:
User types:
www.example.com/subpage/
Browser loads:
www.example.com/subpage/index.html
I'm using GoDaddy hosting and cPanel but their support said it's beyond their range of services to help me (!).
I tried adding .htaccess file to this sub-folder with this but it did not work:
DirectoryIndex index.html
So far I have found that this works:
RedirectMatch ^/subfolder/?$ /subfolder/index.html
The problem is that the user is redirected and the "index.html" is visible in the address bar which is not ideal.
Any idea why DirectoryIndex wouldn't work?
Thank you
Turns out I had a php rewrite rule in .htaccess that looked like this that was interfering with the default DorectoryIndex behavior.
RewriteCond %{REQUEST_URI} \.html$
RewriteRule . /rewrite.php [L]
Removing this fixed the problem! Yay!

htaccess redirection for html files and other links

I was in the need of migrating my site to a subfolder, it is a php script, its links are like this :
aff_link.php?id=812 (number changes at the end)
and html files like
gifts.html
rss_feeds.html
and others ending in .html
I need to redirect those two kind of links to a subfolder called "old"
any advice on this?
Thank you in advance.
You can use this rule in site root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)q=id=\d+ [NC]
RewriteRule ^aff_link\.php$ /old/$0 [L,NC,R=301,NE]

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]

Removing the index.html from url

Ok, maybe a pretty dumb question but I can't find an answer on Google.
I am coding my site by hand. How do I make the index.html disappear from the url's? Do I use a piece of code in my html? Do I have to change my href's in my files?
Hope you guys can help!
EDIT:
I've tried this with a .htaccess file
RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
It does work, but all my links aren't working anymore. I discovered I had to remove all the index.html from the href's in my documents. But that would be a lot of work. Is there an other code for .htaccess that just hides index.html?
A SIMPLE WAY TO DO THIS in Html:
(example in my case is a simple dual language site)
If your link looks like this:
Homepage
You should change it to this:
Homepage
If trying to link to another folder in your directory, like is my example:
English language
You should change it to this:
English language
Notice that "/" goes back to your root directory and automatically selects index.html, so that is why I used "en" for the English language site, because the name of the folder in that case is "en". You should also make sure that you have index.html in your English language folder, and not index-en.html.
Apache has .htaccess files and mod_rewrite,
In your .htaccess file, set:
DirectoryIndex index.html
You can also set this up in the Apache site config files too
You can specify a list of filenames, so if it doesn't find the first it moves to the next.
IIS has .config files
mod_rewrite module is responsible for all the rewriteEngine. Check on your server whether is module is present and enable.
You need to create a file called '.htaccess' in the root directory of your site containing the following code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
And then make sure all the links on your site don't contain '.html' at the end, e.g.:
<a href="/Home.html">
should be replaced with:
<a href="/Home">
Hope this helps!
if you dont find .htaccess, you just need to create a new file using your text editor the same way you would any other html or css file, but save it as simply '.htaccess'
And save it into the root directory, so the same folder that you have your index.html file.
I think this is configured in IIS when you deploy the site, I'm not to sure on it but I'm sure you can specify a start point that your URL will use when you just enter the UL, that implies the Index.html page.
Sorry I'm not too helpful here, hopefully it will point you in the right direction.
Often these things such as Apache or IIS have this set up already, and it looks for the Index.html, Index.php first when you just put your URL in.
Great SEO idea! This is similar to nginx redirect loop, remove index.php from url and Apache .htaccess to hide both .php and .html extentions, as well as mod_rewrite not working htaccess — the idea here, for both Apache's mod_rewrite and nginx ngx_http_rewrite, depends on the differences between the external and internal URLs — in order for the redirect from /index.html to / work, yet not cause redirect loops when actually serving index.html, you gotta make sure you only issue the redirect for external index.html requests, but never for internal ones; this can only be accomplished by looking into the actual request_uri.
Here's the code for nginx ngx_mod_rewrite:
index index.html index.txt;
if ($request_uri ~ "^(.*/)index\.(html|txt)$") { return 301 $1; }
On Apache's mod_rewrite, it'll be something like the following:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^.*/index\.html$
RewriteRule ^(.*/)index.html$ $1 [R,L]
Related:
nginx redirect loop, remove index.php from url
Apache .htaccess to hide both .php and .html extentions
mod_rewrite not working htaccess
References:
http://nginx.org/r/index
http://nginx.org/r/if
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#RewriteCond
Change the link that goes to your homepage to the website address.
You may have:
Link Here
Change that to:
Link
Or try this
Simply don't type the index.html in your browser and don't advertise it as such.
You can set the 'default document' on the web server (whichever you happen to be using) to serve 'index.html' in the absence of a file part. This way, someone going to http://www.mysite.com would be served http://www.mysite.com/index.html