Forum Moderators: phranque
I've only written these by following tutorials so I was hoping someone could cast an expert eye over them and tell me if there is anything wrong with them...
Example 1
---------------------------
Options +SymlinksIfOwnerMatch
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/information/guides/([a-z]*).guide
RewriteRule ^(.*) /information/guides/guides.php?subject=%1 [L]
---------------------------
Example 2
---------------------------
Options +SymlinksIfOwnerMatch
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/resources/productreviews/([0-9]*)-([0-9]*).review
RewriteRule ^(.*) /resources/productreviews/review.php?c=%1&p=%2 [L]
---------------------------
Thanks in advance,
Darren
i don't know how much the regex will need, but maybe you can optimize it by adding a $ at each regex end if .guide and .review is at the end of each request uri.
another speed-up might be if you simplyfy them. this can be done with the first example like that i think:
Options +SymlinksIfOwnerMatch
RewriteEngine on
RewriteRule ^/information/guides/([a-z]*).guide /information/guides/guides.php?subject=$1 [L]
that way you do not need to use the RewriteCond. and maybe check to use the RewriteBase Directive if this example does not work. This might also reduce load maybe.
also i'm wondering why these quite simple rules produce that much load at all.
-hakre
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^information/guides/([a-z]*)\.guide$ /guides.php?subject=$1 [L]
RewriteRule ^resources/productreviews/([0-9]*)-([0-9]*)\.review$ /resources/productreviews/review.php?c=$1&p=$2 [L]
Note: I'm assuming your URLs are in this form:
/information/guides/myguide.guide
/resources/productreviews/348-471.review
If they are not, this code is wrong and you will need to provide more detailed info.
Too quick for me Hakre, good call
You missed one thing, I think. No leading slash on the pattern.
From what you've both said I gather there is nothing really wrong with the way I did it there are just better ways.
To be honest I'm not entirely convinced that these mod_rewrite rules are entirely to blame. Apparently server load has reduced since I removed them so I can't really argue. But certainly the first script has been in use for quite some time on a different host and on my current host for several months with no mention of a problem.
The problem seems to have begun around the same time my account was switched over to 'High Security' mode (Ensim control panel) So I'm wondering if something to do with the server configuration is making these mod_rewrite rules play up. I'm no expert so can only speculate.
The only difference is a change from this:
Options +FollowSymlinks
to this:
Options +SymlinksIfOwnerMatch
for it to work under high security mode.
I will have to see if my host is willing to let me try your ideas for refining the scripts to see if that alleviates the problem.
thanks again
You may want to look at that code again. It looks to me like you could be causing recursion. You should move the REQUEST_URI pattern to the RewriteRule as hakre and Birdman have stated above. However, your use of a period in the existing RewriteCond pattern is causing unexpected consequences.
A period in regular expressions matches any single character. If you want to match a literal period, you must escape the period by preceding it with a "\".
What may be happening now is that the period in your RewriteCond is matching a slash, and therefore will cause an infinite loop if the server does any internal subrequests after the rule is invoked the first time (which it usually will). Since the period matches a slash, the substitution URL will match the pattern, and a loop will be entered for every internal subrequest.
So, rolling this all into one, try:
Options +SymlinksIfOwnerMatch
RewriteEngine on
RewriteRule ^/information/guides/([a-z]*[b])\.g[/b]uide /information/guides/guides.php?subject=$1 [L]
Jim
BTW, here are a couple of example lines (2 of many) from my error log from around the time of high load, which is what put the mod_rewrite in the frame in the first place
Does anyone know exactly what could have happened to cause that. Is it the recursion?
-------------------
[Mon Jan 19 19:04:41 2004] [error] [client 217.**.115.114] (2)No such file or directory: mod_rewrite: can't access text RewriteMap file /etc/virtualhosting/subdomain/site82.map
[Mon Jan 19 19:04:42 2004] [error] [client 217.**.115.114] (2)No such file or directory: mod_rewrite: can't access text RewriteMap file /etc/virtualhosting/subdomain/site82.cgimap
-------------------
[edited by: jdMorgan at 5:19 pm (utc) on Jan. 23, 2004]
[edit reason] Anonomized IP address [/edit]