mod_rewrite :: regex question

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

mod_rewrite :: regex question

Post by alex.barylski »

d11, your the token regex wizard :P maybe you can help me?
RewriteEngine on
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
Works awesome...but...

I want it to:

1) Ignore 'page' and still output index.php?page=test
2) Ignore any extensions

My URL's should ideally look like:

Code: Select all

http://www.mydomain.com/submit.php
But appear to scripts as:

Code: Select all

http://www.mydomain.com/index.php?page=submit.php
I think I have to use %{HTTP_HOST} instead of page/ initially in the regex, but I don't know regex well enough...same goes for the allowing extensions...

By default when I enter (even using the page/document.html) a URL with an extension, mod_rewrite ignores the rules and Apache tries to retreive the file (due to the extension). This is undesired. As it stands right now I only have about 3-4 PHP files in the directory

Anyways, can anyone clear this up for me?

Cheers :)
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Can't you just .....

Code: Select all

RewriteEngine on
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
better yet

Code: Select all

RewriteEngine on
RewriteRule ^([^/\.]+)\.php/?$ index.php?page=$1 [L]
So it doesn't match everything.

I also use

Code: Select all

RewriteCond %{SCRIPT_FILENAME} -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule .* - [L]
at the top of my page, so it doesn't try to redirect pages/folders that really exist.

-NSf
Post Reply