homepage Welcome to WebmasterWorld Guest from 50.17.86.12
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
How can I replace index.html by a different file in .htaccess
boajay



 
Msg#: 4653277 posted 1:15 pm on Mar 12, 2014 (gmt 0)

When I browse to http://www.example.com I want to be redirected to http://www.example.com/home.html,

When I browse to http://www.example.com/index.php I want to be redirected to http://www.example.com/index.php,

How can I do that?

i have try follow but not work :

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ /home.html [L]

RewriteCond %{HTTP_HOST} !^(www\.)?example\.com\/index.php$ [NC]
RewriteRule ^$ /index.php [L]

[edited by: phranque at 1:26 pm (utc) on Mar 12, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

 

thomcraver



 
Msg#: 4653277 posted 4:13 pm on Mar 12, 2014 (gmt 0)

You should simply be able to use the DirectoryIndex directive. It specifies which filenames to look for first when a directory is requested.

The format is simply:

DirectoryIndex home.html

You can also specify a list, in case you use different default files in different directories. Apache will go through the list in the order you specify until it finds the first match. It will then use that filename as the directory's index. For example:

DirectoryIndex home.html index.html index.php

This is an Apache directive set in the server config file. If overrides are set properly, you should also be able to specify this in a .htaccess file.

If you set the default page to be home.html, there would be no need to redirect index.php to itself.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4653277 posted 7:35 pm on Mar 12, 2014 (gmt 0)

You should redirect requests for example.com/home.html and www.example.com/home.html to http://www.example.com/ using a RewriteRule with a preceding RewriteCond looking at THE_REQUEST to prevent an infinite loop.

When www.example.com/ is requested you should serve the contents of the /home.html file via the DirectoryIndex directive.

Requests for /index.php need no special directives as the requested URL matches the actual file name on the server.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4653277 posted 10:14 pm on Mar 12, 2014 (gmt 0)

When I browse to http://www.example.com I want to be redirected to http://www.example.com/home.html

No, you don't. Really. It's possible that you want to be served content that lives at "home.html" but you certainly don't want to be redirected from a shorter URL to a longer one.

When I browse to http://www.example.com/index.php I want to be redirected to http://www.example.com/index.php

Say what now? That's an infinite loop.

Are you saying that you have two physical files, "home.html" and "index.php", and you want "home.html" to be the directory index while "index.php" is just another page? That's physically possible-- easy in fact-- but it doesn't strike me as a fantastically good idea.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4653277 posted 11:15 pm on Mar 12, 2014 (gmt 0)

It's the sort of thing you see on sites with a splash page.

However, splash pages are a relic from 1996. :-)

boajay



 
Msg#: 4653277 posted 6:16 am on Mar 13, 2014 (gmt 0)

Hi thomcraver, i have try your solution , but when i browse www.example.com/index.php , it redirect to www.example.com/home.html too

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4653277 posted 7:22 am on Mar 13, 2014 (gmt 0)

welcome to WebmasterWorld, boajay!


when i browse www.example.com/index.php , it redirect to www.example.com/home.html

please show the exemplified version of your current set of mod_rewrite and mod_dir directives.

Johnny Cakes



 
Msg#: 4653277 posted 5:10 pm on May 5, 2014 (gmt 0)

I landed here with a similar issue. ThomCarver's solution seems right, but I can't get it to work. Here's my exact situation.

I have a subdirectory:
www.mysite.com/subdirectory

When people access that, I do not want the index.html file to be displayed, but rather a file in the parent directory called:
www.mysite.com/filename.html

I thought I was following ThomCarver's instructions when I placed a .htaccess file in the www.mysite.com/subdirectory with the following content:

DirectoryIndex [mysite.com...]

However, when accessing thereafter, I got an error page. It seems like I'm close to a solution but messing up somehow. Can anyone point me to my error? Thanks!

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4653277 posted 1:04 am on May 6, 2014 (gmt 0)

The DirectoryIndex directive should give ONLY the filename, not the full path, unless

:: detour to double-check, due to pervasive fear that I will prove to be talking through my hat ::

OK, unless nothing. The directory index file doesn't have to be physically located in the same directory, but it has to be on the same hostname.

If your quoted rule worked-- it doesn't and can't-- it would mean: people who go to
www.example.com/subdirectory/
are shown the content of
www.example.com/filename.html

But I kinda suspect you really want them to see
www.example.com/subdirectory/filename.html

For that, it's simply
DirectoryIndex filename.html
in an htaccess file located within the affected directory. (It sounds as if you got this part right.)

You could also, of course, put a line in your whole-site htaccess saying something like
DirectoryIndex index.html index.php filename.html

Then, if neither index.html nor index.php can be found, the server will look for filename.html instead. This is a valid alternative if
(1) "filename.html" never occurs in a directory that lacks an index.html or index.php file, and conversely
(2) any directory using "filename.html" as its index file does not also contain a file named index.whatever.

When possible, keep your rules in a single htaccess. It isn't always possible.

Johnny Cakes



 
Msg#: 4653277 posted 1:16 am on May 6, 2014 (gmt 0)

If your quoted rule worked-- it doesn't and can't-- it would mean: people who go to
www.example.com/subdirectory/
are shown the content of
www.example.com/filename.html

That's exactly what I want to happen. I want them to be able to type in:
www.example.com/subdirectory

But instead of seeing the index.html from that directory, I want them to see instead
www.example.com/filename.html

(There is a long story why I can't just copy filename.html to the subdirectory and rename it index.html)

So you are saying that that is impossible?

thomcraver



 
Msg#: 4653277 posted 7:02 am on May 6, 2014 (gmt 0)

It is completely possible using the directive mentioned.

DirectoryIndex filename.html

Or, if there is not an existing index.html in that directory, at the Server's main configuration simply use:

DirectoryIndex index.html filename.html

And yes, according to Apache's documentation [httpd.apache.org ], you can add a subdirectory in the DirectoryIndex directive. However, if you do that from the main server config, all directories would use it.

If you're adding it to an .htaccess file local to that subdirectory, make sure your main configuration allows for overriding directives using the AllowOverride directive.

Johnny Cakes



 
Msg#: 4653277 posted 11:37 am on May 6, 2014 (gmt 0)

That actually works! Way cool and thanks! Plus Apache documentation you linked is a good recourse that I will check in the future.

One final question.

As you can probably tell, I'm a complete newbie. I built the website using one of those builder applications. Someday I'll learn code, but right now, that let me slap it up instantly and it actually doesn't look too bad.

When displayed directly (meaning the user types in www.example.com/filename.html) then all the fancy graphics that the web builder application used to create the page are displayed.

However, when the user types in www.example.com/subdirectory then the text from the filename.html is displayed with formating, but all fancy graphics and menus that the "site builder" created are absent.

In other words, both ways go to the exact same page. Accessing the page directly gives the menu and graphics. Accessing the page indirectly (through the DirectoryIndex redirect) does not give the menus and graphics.

Is this because the user is still "in" the subdirectory and the page is looking for resources under that subdirectory whereas those resources are really under the parent directory? That's just a wild guess. I'm a newbie!

I don't want to modify filename.html for several reasons. I was hoping to fix this through Apache magic.

Seriously, is there any way to get this to behave like I want it to?

thomcraver



 
Msg#: 4653277 posted 2:45 pm on May 6, 2014 (gmt 0)

Use more absolute paths to your images, css and other resources in the code. (ie: /images/myimage.png or /styles.css as opposed to "myimage.png" and "styles.png").

Johnny Cakes



 
Msg#: 4653277 posted 3:07 pm on May 6, 2014 (gmt 0)

Thanks, but I can not modify filename.html so I can't put in absolute paths. I can't change that file at all.

So there's no way of doing this with an .htaccess file or other "work around?"

thomcraver



 
Msg#: 4653277 posted 3:30 pm on May 6, 2014 (gmt 0)

Redirects, maybe?

Johnny Cakes



 
Msg#: 4653277 posted 3:45 pm on May 6, 2014 (gmt 0)

Not sure if you mean redirect for every graphical file or to place a redirect in the index.html file in the subdirectory.

I kinda thought that the DirectoryIndex was basically a redirect.

If a solution for this problem doesn't exist, it really should! Surely there are times when you want to send the user to a different page in a different directory, as if they actually typed in that page address.

Right now, I can get show them the page with DirectoryIndex but they are not really "there" hence the elements don't load.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4653277 posted 6:11 pm on May 6, 2014 (gmt 0)

I kinda thought that the DirectoryIndex was basically a redirect.

No, DirectoryIndex is a rewrite. The absolutely crucial difference is that in a rewrite, the browser "thinks" it is at the originally requested URL. So the browser thinks it is at
example.com/subdirectory/
and if it meets a reference to any supporting file, it will ask for them based on this belief.

Example.
page contains a reference to
mystyles.css
If browser thinks it is at
example.com/filename.html
it will ask for
example.com/mystyles.css
BUT if browser thinks it is at
example.com/subdirectory/
it will ask for
example.com/subdirectory/mystyles.css

Why can't you modify filename.html? Sounds like it is not only your site but your whole server, so you might as well have the page itself call the files by their proper name.

Requests to the wrong place can be either redirected or rewritten. Here a rewrite is appropriate. You should never have internal links-- of any kind, whether pages or supporting files-- that lead to a redirect.

RewriteRule ^subdirectory/mystyles.css http://www.example.com/mystyles.css [L]

If the file uses multiple supporting files with the same extension, make a pipe-separated group:

^subdirectory(image1|image2)\.jpg

If you're certain that none of the files exist in /subdirectory/ and they never will, that's all you need. Otherwise you would need a RewriteCond looking at the referer for each rewrite.

Subsidiary issue: Just as with an ordinary "index.html", you should never allow users to go to the file by name. So if there's a direct request for /filename.html, it has to be redirected:

RewriteCond %{THE_REQUEST} filename\.html
RewriteRule ^filename\.html http://www.example.com/subdirectory/ [R=301,L]

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4653277 posted 7:57 pm on May 24, 2014 (gmt 0)

If a solution for this problem doesn't exist, it really should! Surely there are times when you want to send the user to a different page in a different directory, as if they actually typed in that page address.

The solution is to code the links to the css, js and images with a leading slash so that no matter where the HTML file is on the server and no matter what URL you use to read that file, the links will point to an absolute place and not to someplace relative to the URL that was requested by the browser.

After fetching the HTML file, it is the BROWSER that resolves what the full URLs it needs to request for the accompanying css, js and image files are. It does so either as relative URLs, relative to the URL for the HTML file or as absolute URLs. It therefore depends wholly on the code for those links as found inside that HTML file.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4653277 posted 9:38 pm on May 24, 2014 (gmt 0)

:: jumping up & down screaming with excitement ::

g1! You're baaaack!

As long as we're here, I should point out that I have no idea what I meant here:
RewriteRule ^subdirectory/mystyles.css http://www.example.com/mystyles.css [L]

Without the protocol-and-domain, maybe? Careless copy-and-paste-work? Who knows.

I do know I left out a slash here
^subdirectory(image1|image2)\.jpg

Should of course be
^subdirectory/(image1|image2)\.jpg


Editorial comment:
The solution is to code the links to the css, js and images with a leading slash so that no matter where the HTML file is on the server and no matter what URL you use to read that file, the links will point to an absolute place and not to someplace relative to the URL that was requested by the browser.

Relative links are appropriate when you have things that go in a "package", like
/dir1/dir2/pagename.html
/dir1/dir2/localstyles.css
/dir1/dir2/images/first.jpg
/dir1/dir2/images/second.jpg
where everything is currently inside
/dir1/dir2/
If you use relative links, you can later move the whole package without having to change anything. But links to things that aren't part of the package should have the leading slash.

This doesn't apply to supporting files for error documents (or, of course, anything else that has been rewritten). The browser doesn't "know" it's at
/boilerplate/missing.html
and therefore doesn't know that
errorstyles.css
means
/boilerplate/errorstyles.css
unless you say so.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4653277 posted 7:57 pm on May 25, 2014 (gmt 0)

Hi Lucy. Looks like you've been holding the fort just fine.

I'm not really here, at least not for the next few weeks.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved