Is this possible with .htaccess / mod_rewrite

I’ve got 3 pages that need a clean mod_rewrite url and I’m not sure if I can do this.

[B]http://www.domain.com/event_name[/B] => http://www.domain.com/events.php?name=event_name

[B]http://www.domain.com/event_name/participant_name[/B] => http://www.domain.com/participants.php?participant_name=participant_name&event_name=event_name

[B]http://www.domain.com/event_name/participant_name/donate[/B] => http://www.domain.com/donate.php?participant_name=participant_name&event_name=event_name

Yes that’s possible


RewriteEngine On
RewriteRule ^/?(\\w+)/?$ /events.php?event_name=$1
RewriteRule ^/?(\\w+)/(\\w+)/?$ /participants.php?participant_name=$2&event_name=$1

The RewriteRule for the donate page is left as an exercise for the reader :slight_smile:

Scallio - that’s similar to my initial approach. I tried the code you posted but it gave me an internal server error.

Hm, maybe your server is running apache 1.x, that doesn’t understand \w

Try this:


RewriteEngine On
RewriteRule ^/?([^/]+)/?$ /events.php?event_name=$1 [L]
RewriteRule ^/?([^/]+)/([^/]+)/?$ /participants.php?participant_name=$2&event_name=$1 [L]

Same Thing

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, jon@xxxx.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2.0.54 (Fedora) Server at xxx.xxx.xxx.xxx Port 80

Silly me,


RewriteRule ^/?([^/]+)/?$ /events.php?event_name=$1 [L]

causes an infinite rewrite loop.

This works:


RewriteEngine On

RewriteRule ^/?([^/]+)/([^/]+)/?$ /participants.php?participant_name=$2&event_name=$1 [L]

RewriteCond %{REQUEST_URI} !events.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /events.php?event_name=$1 [L]

Note that this will send every request to a file/directory that does not exist in the root dir to events.php

Scallio - that works! Only issue is that the stylesheet isn’t being loaded. Any ideas?

Yes, the request to the style sheet is caught by the rewriterule for particapants and is thus sent to participants.php

To avoid this, change the .htaccess like so:


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [S=2]

RewriteRule ^/?([^/]+)/([^/]+)/?$ /participants.php?participant_name=$2&event_name=$1 [L]

RewriteCond %{REQUEST_URI} !events.php
RewriteRule ^([^/]+)/?$ /events.php?event_name=$1 [L]

Lines 2 through 4 state that if the request is for a file or directory that exists, the following two rewriterules must be skipped ([S=2]).

So, if you add the RewriteRule for the donations.php, you should change [S=2] to [S=3].

Btw, did you figure out the RewriteRule for the donations.php? :slight_smile:

Am I supposed to change {REQUEST_FILENAME} to the filename?

Nope, %{REQUEST_FILENAME} is a valid statement in a RewriteCond, so you can just copy/paste the complete .htaccess I posted.

See http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond

Hi,

Of course! Your mod_rewrite is changing the directory level of the {REQUEST_URI} so the relative links are offset by the number of levels changed.

As usual, have a read of my signature’s tutorial for more information (look for Missing Support Files for solutions).

Regards,

DK