Forum Moderators: coopster & phranque

Message Too Old, No Replies

Help with mod_rewrite

         

transistor

11:41 pm on May 2, 2002 (gmt 0)

10+ Year Member



I run a website that has information on artists, their albums and songs. So far, it is the usual url: www.mysite.com/artist.php?id=x (x is an integer).
It seems to me that mod_rewrite can help me give my site a much professional look and probably (correct me if I'm wrong) give spiders (the good ones) the ability to index all my artists, albums and songs, right?
Basically, what I would like to do is rewrite www.mysite.com/artist.php?id=x to www.mysite.com/artist_name/
Maybe the tricky part is that I use a number to refer to the artist and therefore I should seek the name before the rewrite.
I've been searching and reading, but I really don't quite grasp the use of mod_rewrite to get what I want.
Ideas?
Thanks! :)

littleman

8:56 am on May 3, 2002 (gmt 0)



Welcome Transistor,
Mod_rewrite is many site's saving grace.
Check out this thread:
[webmasterworld.com...]
It should get you going in the right direction.

transistor

12:27 am on May 4, 2002 (gmt 0)

10+ Year Member



Thank you very much littleman.
I've read the thread but unfortunately I'm completely unexperienced in this mod_rewrite stuff, so I couldn't make much of it.
I am trying basic stuff on my own to get the hang of it, but I still feel my goal is far from my current state of knowledge.
More help is much appreciated! ;)
Thanks!

Olaf

1:02 am on May 4, 2002 (gmt 0)

10+ Year Member



Hi Transistor,

First thing, lets see your options.

What webserver are you using? (Apache,IIS, webshpere, Oracle IAS, etc..)

How much control do you have over your server configuration?

Greets
Olaf

transistor

11:21 pm on May 5, 2002 (gmt 0)

10+ Year Member



Thank you Olaf,
The server has OpenBSD, Apache, PHP & MySQL.
I have total control over the server (it is mine).

Olaf

6:26 pm on May 7, 2002 (gmt 0)

10+ Year Member



ok, thats good to hear.

Lets try something basic to begin with.

in the bottom of your httpd.conf (under apache install dir /conf)

add these lines:

RewriteEngine on
RewriteRule ^/test/(.*)\.html$ /artist.php?id=$1 [PT,L]

Save and reload the httpd server.

Then try testing it by going to a browser and typing in www.mysite.com/test/x.html

(where x is the value to send to id)

Let us know if that works. Then we can discuss more puzzle like configurations.

Olaf

transistor

12:07 am on May 8, 2002 (gmt 0)

10+ Year Member



I like your style Olaf, thanks!

In my local server (iBook, OS X, Apache, PHP 4.2 & MySQL) I get an Error 404 (1.html not found).
In my production server (OpenBSD) I get an error when restarting Apache, I checked the httpd.conf file and there's no mod_rewrite installed, hehe, so I'll install mod_rewrite, try the test and tell you the results.
Meanwhile, in my local server, why do you think I get an Error 404?
Maybe mod_rewrite is not working? How can I make sure it is loaded and working?

Olaf

1:22 am on May 8, 2002 (gmt 0)

10+ Year Member



Well,

you can test the mod_rewrite with even more basic examples.

If mod_rewrite is working this definetly should work

RewriteEngine on
RewriteRule ^/test/123\.html$ /artist.php?id=123 [PT,L]

Here we have just hard coded the url www.mysite.com/test/123.html to the base /artist.php?id=123

Check your apache error log. If it says something to the effect "file /webroot/test/123.html not found" Then its ignoring the rewrite rule and looking for a flat file. (Are you sure that you're adding to the correct httpd.conf? (I have to ask) :) )

Olaf

transistor

3:58 pm on May 8, 2002 (gmt 0)

10+ Year Member



It works! it works! hehehe! wow!! this mod_rewrite stuff _really_ some kind of magic!
(sorry, I'm excited! :)
Thanks!!
I'm ready for the second lesson Olaf (that is, if you're up to it) ;)

Olaf

2:00 am on May 10, 2002 (gmt 0)

10+ Year Member



Hehe, great to hear.

The only tricky thing is that if you want the base to be www.domain.com/artist.html

Then we are rewriting all the document root references. Which will mean that someone typing in www.domain.com/index.html will be sent to the server as /artist.php?id=index (grabbing the index in index.html)

That can be cirumvented by creating a special rewriterule to grab all your static files. (which means one rewrite rule line for each static page you want) This is pretty cumbersome and doesnt scale very well.

So using a "dynamic virtual folder" usually does the trick. Such as saying that the every file requested in the folder /artist/ should be rewritten to artist.php?id=xxx
(you decide what folder name you would like to have)

Now, by going to the original rewrite lines I put in here :
RewriteEngine on
RewriteRule ^/artist/(.*)\.html$ /artist.php?id=$1 [PT,L]

This should grab anyone accessing www.domain.com/artist/123.html and send it to /artist.php?id=123 without the user being the wiser (PT stands for PassThrough, ie. the rewriteengine works as a hidden proxy for your script and L stands for Last, stops looking for rewrite directives after matching)

Now when this line is in your httpd.conf and you have restarted your webserver, try accessing www.domain.com/artist/123.html

A good thing to do while testing this is to have a "tail -f" on both your apache error.log and and the weblog
f.i. one telnet/ssh window do a "tail -f error_log" and on another "tail -f combined_log"
(or whatever you have your logging filenames set to)

This should work.
If not show us what your where getting in the error log or access log

I for instance, am running a webserver where every single access point is a script. (except for the base /index.html and the 404 file)

And to make my life easier I have the script names in my language and with a very descriptive naming method. Such as. prc_guestbook_archive and can refer to them as prc_guestbook_archive?page=40&lang=en&entr_p_page=10

But none of that ever gets seen by the user visiting my webpage. He just sees the rewrite name. So that when he clicks the guestbook link he is accessing /path/guestbook.html which looks perfectly normal to him.
It also allows me to develope new versions of scripts and test them. And instead of replacing the original script I just modify the rewrite entry and point it to the new script name.

transistor

11:17 pm on May 14, 2002 (gmt 0)

10+ Year Member



I see, thank you.
Will you give me your opinion on this?
I know this will work, I want to know, in other's opinion, if it is an efficient solution or is there a better way to do this:
1. create a php include with a case, i.e.:
case "someone":
$id=1;
break;
case "someone else":
$id=2;
break;
etc, etc.
This document might be veeery long, like some 1,000 artists, and sure, I need to be sure there are no 2 names for different artists and other stuff, but that's another story.
2. create the "virtual folder" artist
so, when some user wants to get an artist page:
www.domain.com/artist/someone
my idea would be to intercept the 404 error and search for "someone" in the php include, and send the user to artist.php?id=$id
3. of course, all this *must* be at some point mod_rewriten so the user always sees www.domain.com/artist/someone
THE Question: can it be mod_rewriten?
a) is this a good method? mainly: fast?
b) can it be done in a more efficient fashion (again, faster)?
c) any other methods, ideas, suggestions... blessing? ;)

Many thanks in advance
Joe