How to redirect index.html to / - html

How can I redirect every index.html file to /
For example redirect this URL: https://www.example.com/contact/index.html to https://www.example.com/contact/
Note: I don't want to redirect to the root, I want to redirect to the same directory just remove the index.html
Thank you in advance for your time

Assuming you are using Apache the following lines in .htaccess file under the /contact folder will do the trick:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/index.html$ %{CONTEXT_PREFIX}/folder/$1/ [R=301,L]
RewriteRule ^index.html$ %{CONTEXT_PREFIX}/folder/ [R=301,L]
where folder is contact in your example.
Earlier I suggested using the following rule for a shortcut of the above two:
RewriteRule ^(.*)index.html$ %{CONTEXT_PREFIX}/folder/$1 [R=301,L]
However, this has an unwanted side-effect of redirecting say something like https://www.example.com/contact/myindex.html to https://www.example.com/contact/my
Note: Please be careful not to cause redirection "loops" like:
xxx/index.html -> xxx/ -> xxx/index.html -> xxx/ ...

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.

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]

.htaccess and DirectoryIndex

I have a two files in my root folder: main.html and index.php.
Well, normally index.php takes places as it has higher priority to respond, just wanted to change the logic, so then at first the user has to visit the main.html and then get redirected to index.php. I tried the below code in my .htaccess but it doesn't work as I expected. Actually after doing that, now the first page is correctly changed to main.html, but it never get redirected to index.php!!! It actually returns to main.html again and again (like loop!)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
DirectoryIndex main.html /main.html
ps:
1- even if i try to request the index.php it will open main.html instead!
You can change the default page for directory using the following in your htaccess file, as you have done in the question. Users would land on main.html (instead of index.php) when navigating to "www.coolsite.com/".
//Change default directory page
DirectoryIndex main.html
To redirect the user to another page, you can use the following:
window.location = "http://www.coolsite.com/index.php";
Well, it seems wordpress and static content [index page!] does not not come along! (mostly because of the existing RewriteRule[s] + WP internals)
The workaround is to create a 'page template' (the static content you wish to display as index page) and ask WP to display it as Front-page .

Link to index page of website

Is there a way to link to the index page of a website without specifying the name of the index page or the full website URL?
I have this:
Home
But when I click the link, my address bar shows:
mydomain.com/index.html
I would like it to show:
mydomain.com
Can I do that without putting the full URL (mydomain.com) in the href? If so, how?
For future viewers, I also found this question helpful.
You can just do this:
Home
Any href preceded by a slash is relative the root directory.
It should be noted that when viewing a webpage from a local hard drive in a browser, this will cause the link not to function.
I know this post is old and has an answer. But here is my contribution that also supports index that is not exactly in the root directory
to goto
mydomain.com/index.html
without index.html showing in address bar
Home
this will always point to mydomain.com no matter where the directory your file was saved to.
to point to the index in a directory use this:
Home
this will point to mydomain.com/subdir for mydomain.com/subdir/index.html while the accepted answer will always point to mydomain.com (and this is not always the desired action)
Create a ".htaccess" file and upload to your host public folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.yourwebsite.com.ph/ [R=301,L]
RewriteCond %{THE_REQUEST} \.html
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
</IfModule>