mod rewrite - is it worth the performance hit?

stndn

Golden Member
Mar 10, 2001
1,886
0
0
A while back, I started to get fascinated with the idea of search engine friendly URL.
Instead of having http://www.somewhere.com/news/script.php?page=index&date=20050901

I would have: http://www.somewhere.com/news/index/20050901/

Now, one of the ways of getting this done is through Apache's mod rewrite (Rewrite Rule)

From what I gathered, having a friendly URL is good because it lets you:
- Hide the real programming language behind the URL
- Make the site looks neater and more professional
- Search engine friendly
- Easier to read and remember

On the other hand:
- Performance hit because of the rewrite rule analyzed with each page access
- It's hard to learn ,p


So, my questions for webmasters and web developers who know more about the website performance:

1. Is the performance hit really that hard for sites utilizing mod rewrites? For sites with about 5000 page access per month, do you think the web hosting company will go nuts with all the extra processing? 500000 pages? 3+ million hits?

2. Is it worth the performance hit to have nice-looking URLs?

3. How noticably slower are sites who use mod rewrite vs ones without?

4. What are the hidden cost and dangers I have to look for?


Thanks -)
 

sunase

Senior member
Nov 28, 2002
551
0
0
Performance hit of mod_rewrite is very neglible in my experience. Although I'm careful to avoid broad regular expressions and do order things for performance (if using not conditions, put the most likely first, etc.). Compared to the resources it takes to run modern day forums, which have hideous amounts of queries and dynamic pages going on, mod_rewrite is nothing.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
I'm not terribly experienced with mod_rewrite, but here are my opinions anyways :p
Originally posted by: stndn
From what I gathered, having a friendly URL is good because it lets you:
- Hide the real programming language behind the URL
That can be done easily enough without mod_rewrite, but I don't know why you'd want to. There aren't really any security benefits.
- Make the site looks neater and more professional
Definitely. This is probably the best reason to use it.
- Search engine friendly
How's that? A url is a url and I don't see how one form would be any better for a search engine.
- Easier to read and remember
Same as 2nd point.
On the other hand:
- Performance hit because of the rewrite rule analyzed with each page access
A simple regex evaluation takes almost no time, especially on such a short string. Unless you have a large number of them or happen to write very complex expressions (not likely in the folder -> parameter mapping style you're talking about) I doubt it would take any noticeable amount of time.
- It's hard to learn ,p
Definitely a consideration. Do you have prior regex experience?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Just as an example, here's one for your example:
RewriteRule /news/([^/])*/([^/])*/? /news/script.php?page=$1&date=$2

Now that's untested and, as I said before, I don't have a whole lot of experience with it, but if it works it wouldn't take long at all. The regex is pretty straight forward and would probably scale linearly with the size of the url (which obviously isn't very big).
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
I see, thanks for pointing that out :). Although I have frequently noticed AT forum threads coming up in google searches.

One other thing to point out is that using rewritten urls can be a bit of a pain, development-wise. You either lose the flexibility of deploying without mod_rewrite or you have to make it configurable and be able to generate both urls.
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
Yah, I would assume the performance hit of mod_rewrite is nothing compared to the database queries that generate dynamic pages. But a few of reference pages that I read up said to minimize the user of mod_rewrite as much as possible.

- Hide the real programming language behind the URL
That can be done easily enough without mod_rewrite, but I don't know why you'd want to. There aren't really any security benefits.
Actually, it's not much of hiding the real programming language for security reason. It's more like, let's say .. if today I'm using PHP and tomorrow I decide to switch to ASP, the link that goes to http://www.somewhere.com/news/index/20050901/ will stay the same. So people won't have to update the bookmark or find broken page if I had used http://www.somewhere.com/news/script.php?page=index&date=20050901 and suddenly decided to change to http://www.somewhere.com/news/script.asp?page=index&date=20050901

Search engine friendly, same reason as mentioned by znaps - sometimes search engines don't index dynamic content, although most of them seem to do that now.

I have experience with regex, but I always have troubles when my per-directory .htaccess has RewriteBase and DirectoryIndex with something like:
RewriteBase /news/
DirectoryIndex script.php
RewriteRule ^(.+)/?$ script.php?page=$1 [L]

I don't know why that forward http://www.mysite.com/news/abc/ to http://www.mysite.com/news/script.php?page=abc

But if I change the RewriteRule to
RewriteRule ^news.php?(.+)/?$ news.php?page=$1

It doesn't work?


Either way, though - I just want to make confirm to myself that performance from mod rewrite is almost negligible. I was in doubt when I tried mod_rewrite and found the rewrite log grew up so fast when I did my learning and testing on my home computer. I definitely don't want to get warning letters from my web hosting, if any.

Btw, do a lot of personal-page web developers actually go out of his/her way of creating well-formed URL for personal web pages that won't even get a mention in the portfolio or CV?
 

znaps

Senior member
Jan 15, 2004
414
0
0
Originally posted by: kamper
One other thing to point out is that using rewritten urls can be a bit of a pain, development-wise. You either lose the flexibility of deploying without mod_rewrite or you have to make it configurable and be able to generate both urls.

My company uses rewritten urls for legacy issues, but with jsp/Struts etc, there is no need for it. A single Servlet Filter or Struts Action would deal with it just as well.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: znaps
Originally posted by: kamper
One other thing to point out is that using rewritten urls can be a bit of a pain, development-wise. You either lose the flexibility of deploying without mod_rewrite or you have to make it configurable and be able to generate both urls.

My company uses rewritten urls for legacy issues, but with jsp/Struts etc, there is no need for it. A single Servlet Filter or Struts Action would deal with it just as well.
I'm not sure I follow. How does that fit in with creating urls within the html?