Working with RewriteEngine

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I posted this question almost two years ago, and had some excellent
replies but haven't been able to utilize them until now. I'm a real
slacker, I know! :-)

I'm trying to set up a site so that if someone tries to pull up a
directory that doesn't exist, they'll be directed a PHP page with a
GET variable. For instance, if they go to: (which doesn't exist)

They'll see:

Here's the recipe I'm using in my .htaccess (supplied to me by a
poster in this NG), uploaded to /home/mydomain/ (before the /www/):

RewriteEngine on
RewriteBase /
RewriteCond % !-f
RewriteCond %/index.php !-f
RewriteRule ^([a-zA-Z0-9|_])/(.*) /www/profile/view.php?id=$1

This doesn't work (meaning, I still get a 404 error as if
the .htaccess file didn't exist), and I'm trying to figure out why. So
to start with, I need to make sure that I understand exactly what this
recipe says.

RewriteBase /
# Sets the baseline at the root path. So if I upload it to
the .htaccess file at /home/mydomain/, then the relative path of
anything in this file will start at /home/mydomain/

RewriteCond % !-d
# Sets a condition: if DIRECTORY doesn't exist

RewriteCond %/index.php !-f
# Same as above: if FILE index.php doesn't exist

RewriteRule ^([a-zA-Z0-9|_])/(.*) /www/profile/view.php?id=$1
# Here's where I'm a little lost.

I understand the [a-zA-Z0-9|_] is checking something to make sure that
it contains a letter, number, or _, but what is it checking? I'm
assuming that it's reading everything after, but
BEFORE the next /

What does the mean?

And am I correct that, because of the RewriteBase that is set, this
would rewrite to /home/mydomain/www/profile/view.php?id=$1?

I also understand that the (.*) would read everything after the
first /, and set that to $2, but I'm not using that right now
(although I may in the future).

If I'm correct about all of this, then I get back to the original
question: why isn't it working? I tried a much simpler recipe for
testing, but it had the same failure:

RewriteEngine on
RewriteRule ^/(.*) =$1

Any ideas?



Re: Working with RewriteEngine

Quoted text here. Click to load it

As an addendum, I made a discovery today. This recipe works, although
with problems:

RewriteEngine On
RewriteOptions MaxRedirects=3D5  # safety first

RewriteCond % !-f
RewriteCond % !-d
RewriteRule (.*)/(.*) $2

Remember that the address I'm working with is
Here are the oddities:

1. Notice that I'm using (.*)/(.*), and then only using $2. I
originally just used (.*) and $1, but it plugged in public_html/
username instead of just username. So, I had to add a second / in
there to split this into 2. I haven't tested it, but I'm guessing
$1 would equal "public_html".

2. All of the tutorials said to use ^/(.*), but doing so gave me ISE
errors. I was only able to correct that by removing the ^/.

3. This is my biggest complaint, really. If I use the flag [PT,L] to
force a soft redirect, I get a 404 error. It only works with [L] or
[L,R]. I really don't want it to redirect the browser like that, but
can't understand why I get an error.

Any insight that you guys can give would be greatly appreciated!


Site Timeline