Web Hosting Talk







View Full Version : mod_rewrite question..


zerosix
05-25-2004, 10:26 PM
Hi,
I have been looking around at google and I just can't seem to be able to find the code for the mod_rewrite for the following issue:
I have php files structured like this:
page.php?id=3

What I want to do is to use mod_rewrite to make the pages look like this:
page3.html

Is this possible?

Also, will it rewrite the pages fully, so I can even link to the files in this format: page3.html
And also, will what will it show in the status bar of the link when linking, because right now my links are in the format of: page.php?id=3

Thanks alot :)

Burhan
05-26-2004, 03:20 AM
Yes this is possible.

Your links will be to page3.html

This rule *should* work, but its not 100% (untested):

RewriteEngine on
RewriteRule page(.*).html page.php?id=$1

pixd
05-26-2004, 06:33 AM
If you find the code above does not work, add this line to the top
Options +FollowSymLinks It's a common fix for most server problems people have using mod_rewrite.

leight
05-26-2004, 08:57 AM
RewriteEngine on
RewriteRule page(.*).html page.php?id=$1


Ouch, Pure evil.... this would make:
page3-blah-blah-blah.html
turn into:
page.php?id=3-blah-blah-blah

which i'm sure you don't want.

A slightly better idea would be the following.


RewriteEngine on
RewriteBase /dir/containing/page
RewriteRule page([0-9]+).html page.php?id=$1 [PT]


This limits the part on the end of page#.html to a sequence of numbers.

RewriteBase is relative to the sites document root. [PT] just means passthrough, which means the end user doesn't get a "Page Temporarily Moved" header.

If you only want a single digit at the end of page, remove the + from the rule.

Regards,

Leigh.

Burhan
05-26-2004, 09:07 AM
Good catch Leigh :)

zerosix
05-26-2004, 03:31 PM
its not working, here is what my entire htaccess file looks like:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /absolute/path
RewriteRule page([0-9]+).html page.php?id=$1 [PT]

I did change the absolute path and its 100% correct, I know for a fact :)

zerosix
05-26-2004, 05:36 PM
anyone? :(

leight
05-27-2004, 05:55 AM
noo, not an absolute path, relative to the document root

so if page.php is http://www.domain.com/something/pages/page3.html

you should have
RewriteBase /something/pages

might have to allow override of FileInfo too, not sure though.