How to change the home page of the directory through . htaccess?

Asked

Viewed 2,388 times

0

I have a website that when accessed, you are redirected to the page index.php, but actually I want it to be redirected to the page inicio.html. How can I change that?

  • You want the initial p[agina to be the file inicio.html, or you want to rewrite the URL http://site/inicio.html to access from the server side as index.php and yet the user will see inicio.html? Could explain better?

  • I’m sorry if I wasn’t clear. I wanted that when accessing my site, the person would be redirected to start.html.

3 answers

3


By default, when the browser requests, the file is returned index.html of the requested site. In this case, thinking that you want an answer that solves the problem, in this scope specifically, use in the file .htaccess:

DirectoryIndex inicio.html

Whereas your file is at the root, along with the Apache configuration file, the .htaccess, you are saying that the index of the directory will be called inicio.html, and it must be carried instead of the index.php.

If you want to change, the general Apache configuration, to at all times look for inicio.html, instead of the standard, index.html, you need to change the file settings httpd.conf. Follows the same line of code:

DirectoryIndex inicio.html

However, if you want to use friendly Urls, I advise reading the following question that already covers, well the subject, as well as the search for related questions.

If you are looking for 301 and 302 redirects, and do not change the "home page" of .htacess, Please read this one reply.

1

I wanted that when accessing my site, the person be redirected to start.html. - Robson

If I understand, you want to redirect yourself, when accessing something like http://meusite.com it will direct to http://meusite.com/inicio.html.

301 vs 302

You have two options, 301 redirect, which is permanent redirect:

Redirect 301 / http://meusite.com/inicio.html

Temporary redirection:

Redirect 302 / http://meusite.com/inicio.html

Permanent redirection will cause the user’s browser to disregard the / just as the searchers will not index the /, because it considers both the same URL, being that inicio.html is the new address, already in the 302 is temporary, both Urls still exist, but technically / will be temporarily unavailable and so will be pointing to the inicio.html, I think you want the 301.


Note that files with extension .html by default will not execute scripts .php "embarked" within it, if you want the inicio.html be a php, but with extension .html, in addition to redirecting, you can use the mod_rewrite in his .htaccess, thus:

#direciona / ou index.php (que estejam na raiz) para inicio.html
Redirect 301 / http://meusite.com/inicio.html
Redirect 301 /index.php http://meusite.com/inicio.html

#Ativa o Rewrite
RewriteEngine On

#Ao acessar http://meusite.com/inicio.html será exibido o conteudo index.php
#mas você poderá escrever conteudos no `.php`
RewriteRule ^inicio\.html$ index.php [L]

Now if you want all scripts to have the extension .html in the URL, but you still use the .php server-side, you can use it like this:

#direciona / ou index.php (que estejam na raiz) para inicio.html
Redirect 301 / http://meusite.com/inicio.html
Redirect 301 /index.php http://meusite.com/inicio.html

#Ativa o Rewrite
RewriteEngine On

#qualquer url que tiver a extesão `.html` irá acessar um .php, mas o usuário irá ver como .html
RewriteRule (^|/)([^/]+)\.html$ $2.php [L]

For example if you access this http://meusite.com/foo.html on the server side will be executed foo.php, but the user will see in the url foo.html, so you can write dynamic pages in PHP and show the extension .html.

0

If you are using Apache, open the httpd.conf file and change the line DirectoryIndex for:

DirectoryIndex  inicio.html index.html index.htm

This way, the default file that Apache will search for will be inicio.html.
If this file does not exist, then it will search for a file called index.html (the standard), and so on.
If you do not find any files, you will list the contents of the directory if this is allowed.

Alternatively, you can put the same directive in the file .htaccess, as explained in another reply.
The difference is that by changing the file, its configuration is valid for the "scope" of the file. Usually only your domain or directory.
Changing in Apache, the configuration is valid for all domains.
The change in the file .htaccess always overwrite Apache settings.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.