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

Possible Duplicate:
Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?

I am trying to use the following .htaccess rewrite rules but it just does not work for some reason.

RewriteEngine On
RewriteRule ^/([^/]+)/([^/]+)(.*) /index.php?_controller=$1&_action=$2$3 [QSA,L]
RewriteRule ^/([^/]+)(.*) /index.php?_controller=$1$2 [QSA,L]

I would like a url like this:


To be converted to:


I am not sure why my rewrite rules wont work like I want them to, any help would be appreciated. Thanks!

share|improve this question

marked as duplicate by SmallClanger, MDMarra, John Gardeniers, voretaq7 Nov 16 '12 at 16:55

This question was marked as an exact duplicate of an existing question.

These rules do not make much sense: matching ([^/]+)(.*) and then replacing by $1$2 ?? It's the same as (.+) and $1. As I understand you want to place such rewrite rule in /somefolder/.htaccess -- am I correct? (I need to know this in order to write a rule without a need of modifying it later). – LazyOne Aug 26 '11 at 17:23
@LazyOne yes my .htaccess is in the /someFolder/ which is the webroot for the site. – gprime Aug 29 '11 at 15:40

Such rewrite can be done like this:

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?_controller=$1&_action=$2 [QSA,L]

It will rewrite (internal redirect) request for /first/second to /index.php?_controller=first&_action=second.

It will only work if you requesting non-existing resource. So, if you have such folder /first/second, then no rewrite will happen.

If you change pattern from ^([^/]+)/([^/]+)$ to ^([^/]+)/([^/]*)$, then it will also be applicable for this rewrite: /first/ to /index.php?_controller=first&_action= (notice that trailing slash is required here).

If you change pattern from ^([^/]+)/([^/]+)$ to ^([^/]+)/([^/]+)/?$, then both /first/second and /first/second/ (one with trailing slash) will trigger the rule.

If you change pattern from ^([^/]+)/([^/]+)$ to ^([^/]+)(/([^/]*))?$, then this single rewrite rule will match /first/second as well as /first/ as well as /first (obviously, if 2nd segment is not present, then action parameter _action= will be empty).

It is also possible to make trailing slash with 2 segments optional (i.e. /first/second and /first/second/ will be treated as the same (from rewriting point of view). For that -- change pattern to ^([^/]+)(/([^/]*))?/?$.

share|improve this answer

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