Apache mod_rewrite

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Apache mod_rewrite

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Apache mod_rewrite

Post 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).
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Apache mod_rewrite

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Apache mod_rewrite

Post 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.]
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Apache mod_rewrite

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Apache mod_rewrite

Post 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!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Apache mod_rewrite

Post by VladSun »

http://httpd.apache.org/docs/current/mod/mod_dir.html

You really don't need mod_rewrite to do this...
There are 10 types of people in this world, those who understand binary and those who don't
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Apache mod_rewrite

Post 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
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Apache mod_rewrite

Post 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
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Apache mod_rewrite

Post 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)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Apache mod_rewrite

Post by VladSun »

Does Drupal insist a .htaccess rewrite rule? If not, then just try it:
[text]DirectoryIndex index.html index.php[/text]
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Apache mod_rewrite

Post by John Cartwright »

Am I missing something?

[text]RewriteEngine On
RewriteRule ^$ index.php[R][/text]
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Apache mod_rewrite

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Apache mod_rewrite

Post 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
Post Reply