Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The plan is to take and redirect to

Here's my code that's NOT workin:

     RewriteEngine on
     RewriteRule  ^/chat2/([a-z0-9_-]+)/$ /index.php?room_id=$1 [NC,L]
     RewriteRule  ^/chat2/([a-z0-9_-]+)$ /index.php?room_id=$1 [NC,L]

I get sent to the 404 page. I'm guessing the problem is my placement of the ^ but I'm not certain.

share|improve this question
What is the resultant URL that produces the 404? In other words, what do the logs say? – Swoogan Oct 29 '09 at 15:28
Have you tried to debug your rewrite with the log? RewriteLog and RewriteLogLevel – jomey Oct 29 '09 at 15:31
/sid#9963e78][rid#9d8a330/initial] (3) [per-dir /www/ox/chat2/] strip per-dir prefix: /www/ox/chat2/asdf -> asdf /sid#9963e78][rid#9d8a330/initial] (3) [per-dir /www/ox/chat2/] applying pattern '^/chat2/([a-z0-9_-]+)/$' to uri 'asdf' /sid#9963e78][rid#9d8a330/initial] (3) [per-dir /www/ox/chat2/] strip per-dir prefix: /www/ox/chat2/asdf -> asdf /sid#9963e78][rid#9d8a330/initial] (3) [per-dir /www/ox/chat2/] applying pattern '^/chat2/([a-z0-9_-]+)$' to uri 'asdf' /sid#9963e78][rid#9d8a330/initial] (1) [per-dir /www/ox/chat2/] pass through /www/ox/chat2/asdf – Benny B Oct 29 '09 at 16:01
Firstly, what is converting '/www/ox/chat2/asdf' to 'asdf'? Secondly, '/www/ox/chat2/asdf' will never be matched by '^/chat2/etc...' The ^ means that the string starts with '/chat2'. – Swoogan Oct 29 '09 at 16:22
Where did you put this rewrite rule? Within a Directory,Location Tag or in the vhost conf? – jomey Oct 29 '09 at 16:29
up vote 0 down vote accepted

Reading #2 of the API Phases, I think that is your problem. Using an absolute URL for rewriting within a .htaccess file worked for me.

RewriteRule ([a-z0-9_-]+)$$1 [NC,L]
share|improve this answer
If your replacement string starts with "http://" you will automatically generate an external 302 redirect instead of an internal URI alias. – joebert Nov 27 '09 at 15:08

Don't you want:

RewriteRule  ^/chat2/([a-z0-9_-]+)/$ /chat2/index.php?room_id=$1 [NC,L]

Since you want to redirect to /chat2/index.php?....

share|improve this answer
heh, yeah, I would eventually figured that out when it handed me the homepage instead of the chat page, but I'm still getting the 404 page... – Benny B Oct 29 '09 at 15:24

Try this:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /chat2/
RewriteRule  ^/chat2/([a-zA-Z0-9_-]+)/?$ /index.php?room_id=$1 [NC,L]
share|improve this answer

A rule of thumb with RewriteRule, if your first two characters are "^/" then your third character better be a question mark to make that forward slash optional.


That's probably not your problem though. Looking at your rewritelog, your first line indicates that the URI is being stripped down to "asdf" before the pattern is applied. The way you read those prefix lines is that anything after the "->" is what the pattern is getting matched against.

So in your case, your RewriteRule should probably look like this

RewriteRule ^([a-z0-9_-]+)/?$ index.php?room_id=$1 [NC,L]

If you're on a modern version of Apache, which I'm guessing you are because I don't believe the + (one or more) quantifier is available in earlier versions of mod_rewrite, you can use Perl-compatible regular expression syntax in your pattern, including shorthands such as "\d" to denote "0-9".

RewriteRule ^([a-z\d_-]+)/?$ index.php?room_id=$1 [NC,L]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.