Load index.html when URL points to subfolder - html

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!

Related

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.

How can I redirect a subfolder to the index file in that subfolder?

I used this code to open /subfolder/index.html when user access to /subfolder
[public_html/subfolder/.htaccess]
DirectoryIndex redirect.php
[public_html/subfolder/redirect.php]
header("Location: http://website.com/subfolder/index.html");
die();
But I think it is not optimal. Is there another way to do that? (maybe using just .htaccess file). Thank you very much.
Just using DirectoryIndex index.html will load index.html, but will not change the url in the address bar.
Otherwise, you can just use RedirectMatch from mod_alias:
RedirectMatch ^/subfolder/?$ /subfolder/index.html
put the following code at subfolder .htaccess file:
RewriteEngine on
RewriteRule (.*)$ index.html
The above code will redirect any /subfolder/ request to /subfolder/index.html

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

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>

Make index.html default, but allow index.php to be visited if typed in

I have the following line in my .htaccess file:
DirectoryIndex index.html index.php
Everytime I go to index.php it takes me to index.html. Is it possible to allow for both, but leave index.html the default for users visiting www.domain.com?
By default, the DirectoryIndex is set to:
DirectoryIndex index.html index.htm default.htm index.php index.php3 index.phtml index.php5 index.shtml mwindex.phtml
Apache will look for each of the above files, in order, and serve the first one it finds when a visitor requests just a directory. If the webserver finds no files in the current directory that match names in the DirectoryIndex directive, then a directory listing will be displayed to the browser, showing all files in the current directory.
The order should be DirectoryIndex index.html index.php // default is index.html
Reference: Here.
If you're using WordPress, there is now a filter hook to resolve this:
remove_filter('template_redirect', 'redirect_canonical');
(Put this in your theme's functions.php)
This tells WordPress to not redirect index.php back to the root page, but to sit where it is. That way, index.html can be assigned to be the default page in .htaccess and can work alongside index.php.
I agree with #TheAlpha's accepted answer, Apache reads the DirectoryIndex target files from left to right , if the first file exists ,apche serves it and if it doesnt then the next file is served as an index for the directory. So if you have the following Directive :
DirectoryIndex file1.html file2.html
Apache will serve /file.html as index ,You will need to change the order of files if you want to set /file2.html as index
DirectoryIndex file2.html file1.html
You can also set index file using a RewriteRule
RewriteEngine on
RewriteRule ^$ /index.html [L]
RewriteRule above will rewrite your homepage to /index.html the rewriting happens internally so http://example.com/ would show you the contents ofindex.html .
DirectoryIndex index.html index.htm default.htm index.php index.php3
index.phtml index.php5 index.shtml mwindex.phtml
it doesn't has any means?
you may be just need to add like this!
<IfModule dir_module>
DirectoryIndex index.php index.html index.htm
</IfModule>
Hi,
Well, I have tried the methods mentioned above! it's working yes, but not exactly the way I wanted. I wanted to redirect the default page extension to the main domain with our further action.
Here how I do that...
# Accesible Index Page
<IfModule dir_module>
DirectoryIndex index.php index.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(html|htm|php|php3|php5|shtml|phtml) [NC]
RewriteRule ^index\.html|htm|php|php3|php5|shtml|phtml$ / [R=301,L]
</IfModule>
The above code simply captures any index.* and redirect it to the main domain.
Thank you
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php%{QUERY_STRING} [L]
Put these two lines at the top of your .htaccess file. It will show .html in the URL for your .php pages.
RewriteEngine on
RewriteRule ^(.*)\.php$ $1.html%{QUERY_STRING} [L]
Use this for showing .php in URL for your .html pages.