Halp! im drowning in Mod_Rewrite

If you touch your software enough does it become hardware?

Moderator: Forum Moderators

Post Reply
ProfHawking
Zombie
Zombie
Posts: 2101
Joined: February 20th, 2005, 21:31

Halp! im drowning in Mod_Rewrite

Post 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
Stoat
Site Admin
Site Admin
Posts: 3291
Joined: October 8th, 2004, 15:48
Location: Sheffield, UK
Contact:

Post 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>
ProfHawking
Zombie
Zombie
Posts: 2101
Joined: February 20th, 2005, 21:31

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