Working with RewriteEngine
Working with RewriteEngine
am 02.01.2008 21:48:15 von Jason Carlton
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:
www.mydomain.com/username (which doesn't exist)
They'll see:
www.mydomain.com/profile/view.php?id=username
Here's the recipe I'm using in my .htaccess, uploaded to /home/
mydomain/ (before the /www/):
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^([a-zA-Z0-9|_]{4,10})/(.*) /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 %{REQUEST_FILENAME} !-d
# Sets a condition: if DIRECTORY doesn't exist
RewriteCond %{REQUEST_FILENAME}/index.php !-f
# Same as above: if FILE index.php doesn't exist
RewriteRule ^([a-zA-Z0-9|_]{4,10})/(.*) /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 www.mydomain.com, but
BEFORE the next /
What does the {4,10} 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 ^/(.*) http://www.mydomain.com/profile/view.php?id=$1
Any ideas?
TIA,
Jason
Re: Working with RewriteEngine
am 03.01.2008 02:10:33 von Jason Carlton
On Jan 2, 3:48=A0pm, Jason Carlton wrote:
> 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:
>
> www.mydomain.com/username(which doesn't exist)
>
> They'll see:
>
> www.mydomain.com/profile/view.php?id=3Dusername
>
> Here's the recipe I'm using in my .htaccess, uploaded to /home/
> mydomain/ (before the /www/):
>
> RewriteEngine on
> RewriteBase /
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteCond %{REQUEST_FILENAME}/index.php !-f
> RewriteRule ^([a-zA-Z0-9|_]{4,10})/(.*) /www/profile/view.php?id=3D$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 %{REQUEST_FILENAME} !-d
> # Sets a condition: if DIRECTORY doesn't exist
>
> RewriteCond %{REQUEST_FILENAME}/index.php !-f
> # Same as above: if FILE index.php doesn't exist
>
> RewriteRule ^([a-zA-Z0-9|_]{4,10})/(.*) /www/profile/view.php?id=3D$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 afterwww.mydomain.com, but
> BEFORE the next /
>
> What does the {4,10} mean?
>
> And am I correct that, because of the RewriteBase that is set, this
> would rewrite to /home/mydomain/www/profile/view.php?id=3D$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 ^/(.*)http://www.mydomain.com/profile/view.php?id=3D$1
>
> Any ideas?
>
> TIA,
>
> Jason
As an addendum, I made a discovery today. This recipe works, although
with problems:
RewriteEngine On
RewriteOptions MaxRedirects=3D5 # safety first
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/(.*) http://www.mydomain.com/profile/view.php?id=3D$2
[L]
Remember that the address I'm working with is www.mydomain.com/username.
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 that
$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 I
can't understand why I get an error.
Any insight that you guys can give would be greatly appreciated!
Jason
Re: Working with RewriteEngine
am 03.01.2008 10:36:48 von sean dreilinger
Jason Carlton wrote:
> 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:
>
> www.mydomain.com/username (which doesn't exist)
> They'll see:
> www.mydomain.com/profile/view.php?id=username
>
> Here's the recipe I'm using in my .htaccess, uploaded to /home/
> mydomain/ (before the /www/):
>
> RewriteEngine on
> RewriteBase /
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteCond %{REQUEST_FILENAME}/index.php !-f
> RewriteRule ^([a-zA-Z0-9|_]{4,10})/(.*) /www/profile/view.php?id=$1
....
> Any ideas?
try this:
RewriteEngine on
### 1. skip everything if this is already a request for a user profile
RewriteCond %{REQUEST_URI} !^/profile/view\.php
### 2. set this regular expression to just those characters permissible in
### valid profile usernames:
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9_.-]+)$
### 3. ensure the request is not for a real live file in the filesystem (btw, is
### there any mechanism in-place to prevent a namespace conflict between
### top-level directory names and profile usernames?)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
### 4. optional, expensive: ensure the user profile exists before redirecting
RewriteCond /profile/view.php?id=%1 -U
### 5. finally, redirect /username to /profile/view.php?id=username
RewriteRule ^.*$ /profile/view.php?id=%1 [redirect=permanent,last]
hth
--sean
--
sean dreilinger - http://durak.org/sean/
Re: Working with RewriteEngine
am 03.01.2008 11:24:17 von Jason Carlton
On Jan 3, 4:36=A0am, sean dreilinger wrote:
> Jason Carlton wrote:
> > 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:
>
> >www.mydomain.com/username(which doesn't exist)
> > They'll see:
> >www.mydomain.com/profile/view.php?id=3Dusername
>
> > Here's the recipe I'm using in my .htaccess, uploaded to /home/
> > mydomain/ (before the /www/):
>
> > RewriteEngine on
> > RewriteBase /
> > RewriteCond %{REQUEST_FILENAME} !-f
> > RewriteCond %{REQUEST_FILENAME}/index.php !-f
> > RewriteRule ^([a-zA-Z0-9|_]{4,10})/(.*) /www/profile/view.php?id=3D$1
> ...
> > Any ideas?
>
> try this:
>
> RewriteEngine on
>
> ### 1. skip everything if this is already a request for a user profile
> RewriteCond %{REQUEST_URI} !^/profile/view\.php
>
> ### 2. set this regular expression to just those characters permissible in=
> ### valid profile usernames:
> RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9_.-]+)$
>
> ### 3. ensure the request is not for a real live file in the filesystem (b=
tw, is
> ### there any mechanism in-place to prevent a namespace conflict between
> ### top-level directory names and profile usernames?)
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteCond %{REQUEST_FILENAME} !-d
>
> ### 4. optional, expensive: ensure the user profile exists before redirect=
ing
> RewriteCond /profile/view.php?id=3D%1 -U
>
> ### 5. finally, redirect /username to /profile/view.php?id=3Dusername
> RewriteRule ^.*$ /profile/view.php?id=3D%1 [redirect=3Dpermanent,last]
>
> hth
>
> --sean
>
> --
> sean dreilinger -http://durak.org/sean/- Hide quoted text -
>
> - Show quoted text -
Awesome, Sean, it worked like a charm!
I'm hoping you can explain the concept behind using %1 in the rule,
though, instead of $1. I ran into this misunderstanding in a different
thread, and I'm still confused on where the %1 comes from. What part
of the recipe that you posted defined it?
TIA,
Jason
Re: Working with RewriteEngine
am 10.01.2008 05:16:30 von Jason Carlton
> try this:
>
> RewriteEngine on
>
> ### 1. skip everything if this is already a request for a user profile
> RewriteCond %{REQUEST_URI} !^/profile/view\.php
>
> ### 2. set this regular expression to just those characters permissible in
> ### valid profile usernames:
> RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9_.-]+)$
>
> ### 3. ensure the request is not for a real live file in the filesystem (btw, is
> ### there any mechanism in-place to prevent a namespace conflict between
> ### top-level directory names and profile usernames?)
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteCond %{REQUEST_FILENAME} !-d
>
> ### 4. optional, expensive: ensure the user profile exists before redirecting
> RewriteCond /profile/view.php?id=%1 -U
>
> ### 5. finally, redirect /username to /profile/view.php?id=username
> RewriteRule ^.*$ /profile/view.php?id=%1 [redirect=permanent,last]
Well, I thought it was working like a charm!
I have several PHP scripts that are called via variable. Such as:
www.mydomain.com/dir/subdir/?id=12345&h=081214
This page would make 2 MySQL queries, and the page
www.mydomain.com/profile/view.php?id=whatever would make 3 queries.
I found that when I used this .htaccess recipe, though, the database
immediately started getting slammed, and started giving errors that it
was getting queries at once than it could handle (I think 150).
I'm guessing that every page loaded was loading /profile/view.php
first, then jumping to the correct page. I've never had that
particular problem before, and removing the .htaccess recipe removed
the error.
So, am I correct that the view.php page was being loaded, even when
the file was found? If so, is it because I'm calling the page by /
subdir/?id=12345 instead of /subdir/index.php?id=12345?
Re: Working with RewriteEngine
am 10.01.2008 10:21:12 von sean dreilinger
Jason Carlton wrote:
....
>> ### 4. optional, expensive: ensure the user profile exists before redirecting
>> RewriteCond /profile/view.php?id=%1 -U
....
> Well, I thought it was working like a charm!
....
> So, am I correct that the view.php page was being loaded, even when
> the file was found? If so, is it because I'm calling the page by /
> subdir/?id=12345 instead of /subdir/index.php?id=12345?
if you're using that optional rule, #4, then yes, the user existence of the
profile is being tested by an internal subrequest. and that's an expensive thing
to do if you have dynamically-generated content that isn't cached anywhere along
the way.
if you turn off (comment out) item #4, you'll eliminate the internal subrequest
that is doubling queries to your database. you might further improve things by
writing some error handling (in view.php) to deal with requests for nonexistent
userids as efficiently as possible.
hth
-sean
--
sean dreilinger - http://durak.org/sean/
Re: Working with RewriteEngine
am 11.01.2008 05:35:01 von Jason Carlton
> if you're using that optional rule, #4, then yes, the user existence of the
> profile is being tested by an internal subrequest. and that's an expensive thing
> to do if you have dynamically-generated content that isn't cached anywhere along
> the way.
>
> if you turn off (comment out) item #4, you'll eliminate the internal subrequest
> that is doubling queries to your database. you might further improve things by
> writing some error handling (in view.php) to deal with requests for nonexistent
> userids as efficiently as possible.
Sean, I actually didn't use # 4. I didn't even try, since you
mentioned the overhead, and my server is already bogged down as it is.
- J
Re: Working with RewriteEngine
am 14.01.2008 22:06:15 von Jason Carlton
On Jan 10, 11:35=A0pm, Jason Carlton wrote:
> > if you're using that optional rule, #4, then yes, the user existence of =
the
> > profile is being tested by an internal subrequest. and that's an expensi=
ve thing
> > to do if you have dynamically-generated content that isn't cached anywhe=
re along
> > the way.
>
> > if you turn off (comment out) item #4, you'll eliminate the internal sub=
request
> > that is doubling queries to your database. you might further improve thi=
ngs by
> > writing some error handling (in view.php) to deal with requests for none=
xistent
> > userids as efficiently as possible.
>
> Sean, I actually didn't use # 4. I didn't even try, since you
> mentioned the overhead, and my server is already bogged down as it is.
>
> - J
For anyone reading this in the future, I was able to resolve the
problem by adding a second check for the directory and file in my PHP
script BEFORE querying the database.
It's not perfect, because it required me to add a text file for each
account instead of just querying the MySQL database, but it works.
I've had it running for a few days, and so far I haven't had any
problems.
- J