.htaccess and DirectoryIndex - html

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 .

Related

How to hidden name end url index.html

What I mean by last name in the url, like index.html for example the url link on my website homepage. the result is like this
https://hijaben.insomnia247.nl/index.html
I want to remove or delete the last name of the url (index.html)
I tried using the .htaccess file but it didn't work at all. Is it possible the script is wrong or the VPS doesn't support or what? the solution ..?
File .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
</IfModule>
https://www.digitalocean.com/community/tutorials/how-to-rewrite-urls-with-mod_rewrite-for-apache-on-ubuntu-16-04
Please check this out im sure it can help you with your problem sir.
https://www.linode.com/docs/web-servers/apache/how-to-set-up-htaccess-on-apache/
Don't overcomplicate things with unnecessary mod_rewrite. Just use DirectoryIndex index.html.

How to redirect index.html to /

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/ ...

Change root folder on HTML links

I have a bunch of HTML files with link tags like this:
[...]
Link 1
Link 2
Link 3
[...]
The website is inside a folder on the server, e.g. http://www.website.com/folder/, but links beggining with / point to the root, e.g. http://www.website.com/, but the files file1.php, file2.php, file3.php, etc. are inside the folder.
How would I be able to make the links act as if the root folder were the folder where they are, without changing HTML contents?
You can try with a internal redirect like this:
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule ^(.*)$ /folder/$1 [L,QSA]
The condition is to redirect only PHP files to the folder, if that gives you a loop you can go with a more specific one:
RewriteRule ^(.*)\.php$ /folder/$1.php [L,QSA]
With RewriteBase something like this:
RewriteBase /folder/
RewriteRule ^(.*)\.php$ $1.php [L,QSA]

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.