0

The plan is to take domain.com/chat2/roomnumber and redirect to domain.com/chat2/index.php?room_id=roomnumber.

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.

Benny B
  • 159
  • 2
  • 4
  • 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
  • jomey, it's in an .htaccess in the same directory as the pages it references. I've simplified it, but it's still not quite right. It's now: RewriteRule ([a-z0-9_-]+)$ index.php?room_id=$1 [NC,L] But the funny thing is, I end up on index.php, and doing a print_r($_SERVER) I see this weirdness: [REDIRECT_QUERY_STRING] => room_id=sdfgggg [REDIRECT_URL] => /chat2/sdfgggg [GATEWAY_INTERFACE] => CGI/1.1 [SERVER_PROTOCOL] => HTTP/1.1 [REQUEST_METHOD] => GET [QUERY_STRING] => room_id=php [REQUEST_URI] => /chat2/sdfgggg [SCRIPT_NAME] => /chat2/index.php – Benny B Oct 29 '09 at 16:32
  • and then print_r($_GET) gives me: Array ( [room_id] => php ) But I want it to give me Array ( [room_id] => sdfgggg ) – Benny B Oct 29 '09 at 16:33

4 Answers4

0

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?....

womble
  • 96,255
  • 29
  • 175
  • 230
  • 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
0

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_-]+)$ http://my.domain.com/index.php?room_id=$1 [NC,L]
jomey
  • 312
  • 1
  • 3
  • 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
0

Try this:

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

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.

^/?chat...

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]
joebert
  • 195
  • 7