Mod rewrite help

Hey Guys and Gals,

i have never ventured into mod_rewrite before and i have read a few tut’s on them. I want to have “friendly” urls in my application and here is the rewrite i have come up with

RewriteEngine on
RewriteRule ^/([a-zA-Z_]+)/([a-zA-Z_]+)(/([a-zA-Z_]+)?)(/([0-9]))?/?$ index.php?view=$1&do=$2&action=$3&id=$4 [L]

i have done the test on this page ( and it worked. So i know mod_rewrite is set up in my vhost file, and it well work. I am not sure what’s going on. Also i hope I did the conditional ones correctly.



I made two changes.

RewriteRule ^/([a-zA-Z_]+)/([a-zA-Z_]+)[COLOR=“#FF0000”]B[/B]/COLOR?/?$ index.php?view=$1&do=$2&action=$3&id=$4 [L]

RewriteRule ^/([a-zA-Z_]+)/([a-zA-Z_]+)[COLOR=“#FF0000”]B?[/B]/COLOR?/?$ index.php?view=$1&do=$2&action=$3&id=$4 [L]

The first was to change FONT=Courier New[/FONT] to FONT=Courier New[/FONT]. Parentheses can serve two jobs: to remember a matched value and to group values. But sometimes you want to group values without remembering the match. That’s what FONT=Courier New[/FONT] is for. In this case, you only wanted to group the slash with the action. But because your regexp used FONT=Courier New[/FONT], then $3 would have been “/action”, $4 would have been “action”, and $5 would have been “id”.

The second change was to move the ? outside one more set of parentheses. Otherwise you were saying that the action is optional, but the slash preceding it is required.

Thanks for the help! I forgot to mention i was getting 404s. With your version i still get 404s

i did a simple test (again) to make sure mod_rewrite was working here is what i added

RewriteRule ^test\\.html$ test.php [L]

and when i go to test.html i get what is in test.php showing. ( and this is in the same directory, so i know it’s not a permissions issue).

Any ideas why it is not been re-written?



Ahh, it’s possibly due to a missing quantifier on your ID pattern.

RewriteRule ^/([a-zA-Z_]+)/([a-zA-Z_]+)(?:/([a-zA-Z_]+))?(/([0-9]+))?/?$ index.php?view=$1&do=$2&action=$3&id=$4 [L]

If that still doesn’t work, then you’ll have to also show us your directory structure and the kind of URLs you’re trying to use.

yah that still not working…

So heres what the directory looks like:

-Main Directory that everything sits in
–app folder
—.htaccess file
—index.php (that calls the controllers etc)
— controllers

urls i am trying to use would be


You’ll need to remove the first slash. Whether that slash is needed depends on whether your rewrite rule is placed in your vhost in your main configuration file, or if it’s placed in a per-directory context in an .htaccess file. When you’re in an .htaccess file, the directory prefix is stripped off before any processing begins.

Also, I noticed one more parenthese group that should be non-capturing.

RewriteRule [1]//COLOR/([a-zA-Z_]+)(?:/([a-zA-Z_]+))?[COLOR=“#FF0000”]B?/?$ index.php?view=$1&do=$2&action=$3&id=$4 [L]

RewriteRule ^([a-zA-Z_]+)/([a-zA-Z_]+)(?:/([a-zA-Z_]+))?[COLOR=“#FF0000”]B?/?$ index.php?view=$1&do=$2&action=$3&id=$4 [L]

  1. COLOR=“#FF0000↩︎

Thanks Jeff! It’s working :slight_smile: