HTACCESS Mod-rewrite - how do you do it just for homepage?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

HTACCESS Mod-rewrite - how do you do it just for homepage?

Post by simonmlewis »

Code: Select all

RewriteEngine On
RewriteRule ^/$ index.php?page=home&menu=home[L]
Hi.
I am trying to get the htaccess to work for the first time - I've never done this before. Am reading all about it, but frankly I only need it for the homepage. ie. http://www.domain.co.uk/

That needs to go to the full index.php...... url.
How do I use my rewrite code to do this??

Simon
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: HTACCESS Mod-rewrite - how do you do it just for homepage?

Post by pickle »

If possible, this is something you should set up in the Apache config files. I think there's a directive for default page, or directory index, or something along those lines. If you can't change the config files, you should be able to use those exact same directives in your .htaccess file. You don't need mod_rewrite for this.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: HTACCESS Mod-rewrite - how do you do it just for homepage?

Post by jackpf »

You mean like this?

Code: Select all

RewriteEngine On
RewriteRule ^$ index.php?page=home&menu=home [L]
EDIT
Pickle, didn't see you there. Probably better option tbh. Yeah, there should be a directoryindex directive, where you can specify several pages, which will be tried for existence in order.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTACCESS Mod-rewrite - how do you do it just for homepage?

Post by simonmlewis »

Perfect!!!

I did try it by faking the index.html page, but it refused to show the &menu=home

God knows why, coz ur's works.

Brilliant. Thanks.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: HTACCESS Mod-rewrite - how do you do it just for homepage?

Post by jackpf »

Well, I removed the forward slash from the regex, since it's not considered part of the location apparently...

I also put a space between the filename you're using to handle the request and "[L]".

Code: Select all

index.php?page=home&menu=home[L] #will show $_GET['menu'] as 'home[L]', and apache will not process it
index.php?page=home&menu=home [L] #correct
;)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTACCESS Mod-rewrite - how do you do it just for homepage?

Post by simonmlewis »

Code: Select all

RewriteRule ^$ index.php?page=home&menu=home&head=welcome [L]
This fails too for another site - and I have the space...??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: HTACCESS Mod-rewrite - how do you do it just for homepage?

Post by jackpf »

Have you got

Code: Select all

RewriteEngine On
in there anywhere?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTACCESS Mod-rewrite - how do you do it just for homepage?

Post by simonmlewis »

Yes.
It's just that line that's wrong somehow.

Code: Select all

Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^$ index.php?page=home&menu=home&head=welcome [L]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: HTACCESS Mod-rewrite - how do you do it just for homepage?

Post by jackpf »

Works for me. Is that the whole htaccess file? Are htaccess files even enabled on the server?

Visiting just http://localhost/ , I get

Code: Select all

Array ( [page] => home [menu] => home [head] => welcome )
when I

Code: Select all

print_r($_GET);
:)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTACCESS Mod-rewrite - how do you do it just for homepage?

Post by simonmlewis »

No, this is the entire file

Code: Select all

DirectoryIndex index.htm index.html index.php
order allow,deny
deny from 81.144.190.133
allow from all 
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^$ index.php?page=home&menu=home&head=welcome [L]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: HTACCESS Mod-rewrite - how do you do it just for homepage?

Post by jackpf »

It works for me. As I said, are you sure you have htaccess files enabled on that server?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTACCESS Mod-rewrite - how do you do it just for homepage?

Post by simonmlewis »

Yes - it's been checked.
Without the mod-rewrite bit, it's fine.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: HTACCESS Mod-rewrite - how do you do it just for homepage?

Post by jackpf »

I'm clueless then :/

Since it works for me, I can't really start to debug it since...well, it works :P

Sorry.
Post Reply