Page 1 of 1

Apache mod_rewrite

Posted: Wed Sep 15, 2010 12:12 pm
by alex.barylski
How do i force a redirect to index.html when someone accesses my web site without any URI, just the domain itself?

For example:

http://domain.com => http://domain.com/index.html

I have found many examples of forcing www subdomain or the reverse, but cannot find anything for the problem I describe above. :(

Cheers,
Alex

Re: Apache mod_rewrite

Posted: Wed Sep 15, 2010 12:26 pm
by califdon
I'm just getting into using .htaccess and mod_rewrite, myself, but I think it's merely having a RewriteRule like: RewriteRule .* index.html [L]

In other words, no matter what the URL is, past the domain name, go to index.html and don't do any more rewriting (that's what the [L] does).

Re: Apache mod_rewrite

Posted: Wed Sep 15, 2010 12:54 pm
by josh
RewriteRule !(index\.htm) index.htm [R]


In psuedo code, if the user is not at index.htm, then rewrite them to index.htm
[R] flag issues an http redirect instead of rewriting internally. Note though this will chop off any query string. There is a way to preserve that if needed, I'd have to check the manual but I think you need rewrite conditions.

Re: Apache mod_rewrite

Posted: Wed Sep 15, 2010 1:27 pm
by califdon
Yeah, that's my inexperience showing. I've been reading about RewriteCond. If you need the query string, that's what you have to do (but it's pretty simple)*. But if you simply want to redirect to index.html, no matter what, you have 2 options, the internal redirect, which I showed, which doesn't change what is displayed in the browser's address box, or the HTTP redirect, which Josh showed, which does change what is displayed in the browser's address box, which is probably a better idea, since it lets the user know what the appropriate URL is and avoids them bookmarking a bad URL, etc.

*[Edit: No, as I give it second thought, you just need to use parens in your regex and then $1 for the first set of parens, etc. So you define what the query string must be with parens, then you rewrite it with $1 representing the query string.]

Re: Apache mod_rewrite

Posted: Wed Sep 15, 2010 2:27 pm
by alex.barylski
I think it's conflicting with my existing .htaccess:

Code: Select all

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I have tried placing what josh provided immediately before and after the last rewrite rule and nothing seems to happen. At best when I access the site with index.html the page loads but all th eimages, etc don't.

Cheers,
Alex

Re: Apache mod_rewrite

Posted: Wed Sep 15, 2010 2:41 pm
by califdon
As I understand it, RewriteCond's apply to all RewriteRule's below them, so probably your new line needs to come before the RewriteCond's. Then if it doesn't meet that pattern, it will drop through to the the Cond's and subsequent Rule. But remember, I'm a noobie at this, too!

Re: Apache mod_rewrite

Posted: Thu Sep 16, 2010 7:24 am
by VladSun
http://httpd.apache.org/docs/current/mod/mod_dir.html

You really don't need mod_rewrite to do this...

Re: Apache mod_rewrite

Posted: Thu Sep 16, 2010 12:27 pm
by alex.barylski
I think I do need mod_rewrite. Maybe I didn't explain well enough, I'll try again.

I have a Drupal CMS installed so the directory already has index.php, etc. Problem is, without a file/folder in the URI (ex: domain.com/) Drupals default homepage loads, not the content pages I have created. The best way to solve it would be to indicate to Drupal that the homepage should be a node I have created but I cannot for find this for the life of me, so as a short term "hack" I was going to just force a visitor to index.html which is the node I want shown as the front page.

So a redirect to domain.com/index.html or whatever node I want shown as homepage is what I need. I think the directive you linked to is simply for indicating which file loads on default when no file is indicated, close to what I want but I don't think it'll work cause then the index.php will not fire and the node will not display as it'll be looking for a quasi index.html not physically present only in the eyes of drupal.

Cheers,
Alex

Re: Apache mod_rewrite

Posted: Thu Sep 16, 2010 12:43 pm
by mikosiko
PCSpectra wrote:... The best way to solve it would be to indicate to Drupal that the homepage should be a node I have created but I cannot for find this for the life of me, so as a short term "hack" I was going to just force a visitor to index.html which is the node I want shown as the front page.
not sure if this is valid solution... but maybe worth to look at it
http://drupal.org/node/277049

Re: Apache mod_rewrite

Posted: Thu Sep 16, 2010 1:07 pm
by josh
Use a RewriteLog if you want to learn to fish. http://httpd.apache.org/docs/2.0/mod/mo ... rewritelog
(the setting below it goes hand in hand)

Re: Apache mod_rewrite

Posted: Thu Sep 16, 2010 1:10 pm
by VladSun
Does Drupal insist a .htaccess rewrite rule? If not, then just try it:
[text]DirectoryIndex index.html index.php[/text]

Re: Apache mod_rewrite

Posted: Thu Sep 16, 2010 1:15 pm
by John Cartwright
Am I missing something?

[text]RewriteEngine On
RewriteRule ^$ index.php[R][/text]

Re: Apache mod_rewrite

Posted: Thu Sep 16, 2010 1:25 pm
by VladSun
PS: I'mnot sure what do you mean by that:
PCSpectra wrote:...looking for a quasi index.html not physically present only in the eyes of drupal.

Re: Apache mod_rewrite

Posted: Thu Sep 16, 2010 2:32 pm
by alex.barylski
VladSun: Drupal directory looks like this:

Code: Select all

includes/
misc/
modules/
profiles/
scripts/
sites/
themes/
.htaccess
cron.php
index.php
install.php
robots.txt
update.php
xmlrpc.php
The .htaccess (in part) looks like this:

Code: Select all

# Set the default handler.
DirectoryIndex index.php

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on

  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  #RewriteRule !(index\.htm) index.htm [R]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
There is no index.html and cannot be one, otherwise it would be served by Apache and Drupal would not be invoked via index.php. If I understand the directive, it's simply looking up the file index.html as a default but drupal actually needs index.html to be present in the URI otherwise it fetches it's own homepage, which is not what I need or want, so a redirect to index.html seems hackish but it would work for the time being until I finish Drupal setup and ocnfiguration.

Cheers,
Alex