Page 1 of 1

Halp! im drowning in Mod_Rewrite

Posted: November 27th, 2008, 23:26
by ProfHawking
This has been driving me nuts for over an hour, and all my googling has failed me.

I want to do something which i think should be quite simple.
I have built a new website which uses mod rewrite for its simple urls.

home.html -> index.php?page=home
blah.html -> index.php?page=blah

All urls are in this format. So i use:

Code: Select all

RewriteEngine on
RewriteRule ^(.*)\.html$ index.php?page=$1 [L]
ErrorDocument 404 /index.php?page=error
Works fine.

Now the complex bit: the old site - it had some weird and horrible urls. Some has been indexed by google, and some are linked to. We dont want to loose this traffic, so it needs to be redirected to the homepage.

What i now need is that anything that isnt the homepage:
/
or the new urls:
/blah.html

to be 301 redirected to:
/

I have been fiddling with something like this:

Code: Select all

RewriteEngine on
RewriteRule ^(.*)\.html$ index.php?page=$1 [L]
RewriteCond %{REMOTE_ADDR} !^(\.html)$ [NC]
RewriteRule .* / [L,R=301]
ErrorDocument 404 /index.php?page=error
But it always ends in disaster - with either an endless redirect loop, a nothing happens or a server misconfiguration.

:cry: :cry: :cry:

Pls can anyone put me out of my misery so i can get some sleep

Posted: November 28th, 2008, 0:02
by Stoat
Not sure if this'll work or not.
Checks that the request is not a file or a directory. index.php is a file, so it should work fine.
You might want to make it disregard css/jpg/gif etc, see how it goes.

Code: Select all

<IfModule>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.html$ index.php?page=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [L,R=301]
ErrorDocument 404 /index.php?page=error
</IfModule>

Posted: November 28th, 2008, 0:17
by ProfHawking
Stoat my good man, you win the beers! :ahoy:

I never though of doing it that way, geniarse.
Will leave images for now, nobody links into them, and it might break the new pages which use them perhaps.

Thanks again!!!