Page 1 of 1

mod_rewrite :: regex question

Posted: Wed Oct 25, 2006 11:41 pm
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 :)

Posted: Thu Oct 26, 2006 12:46 am
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