I'm a rewrite newbie, I'm wondering why this rewrite rule isn't working, it ceases Apache from beginning

RewriteRule ^([-a-zA-Z0-9_]+)?/constant/([-a-zA-Z0-9_]+)/(.*)$ http://$2.domain.com/$1/$3 [R=301, NC]

What this rule must do is URL refactoring, good examples:

http://www.domain.com/controller/constant/variable/action ought to be rerouted to http://variable.domain.com/controller/action


http://www.domain.com/constant/variable ought to be rerouted to http://variable.domain.com

essentially /constant/variable ought to be detected, placed within the subdomain after which removed

Help me within this subtle problem that i'm facing.


Check it out with two rules:

RewriteRule ^constant/([-a-zA-Z0-9_]+)$ http://$2.domain.com/$1 [R=301,NC]
RewriteRule ^([-a-zA-Z0-9_]+)/constant/([-a-zA-Z0-9_]+)/(.*)$ http://$2.domain.com/$1/$3 [R=301,NC]

Also observe that the flags mustn't contain any whitespace: [R=301, NC] is invalid syntax but [R=301,NC] applies syntax.