0

I have a regular expression for a framework to convert host.com/controller/method in to host.com/index.php?controller/method

Like this:

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]

I now require it to convert into this,
host.com/controller/method -> host.com/index.php?controller=$1&method=$2

How would I do a match with a regular expression like so within htaccess?

0

2 Answers 2

0

Something like this?

RewriteRule ^(\w+)/(\w+)$ /index.php?controller=$1&method=$2 [L]
1
  • Thank you. I will accept because it made the most sense to me quickly.
    – Tim N.
    Jan 18, 2011 at 13:10
0

If you just want one path segment instead of just anything, use [^/] instead of .:

RewriteCond $1 !=index.php
RewriteRule ^([^/]*)$ /index.php/$1 [L]

And for your second requirement:

RewriteCond $1 !=index.php
RewriteRule ^([^/]*)/([^/]*)$ /index.php?controller=$1&method=$2 [L,QSA]

Here you should also consider to use the quantifier + (one or more) instead of * (zero or more).

2
  • I don't think first is a requirement. it seems it works just fine for OP. Jan 18, 2011 at 13:07
  • Thank you Gumbo. I will look on to this.
    – Tim N.
    Jan 18, 2011 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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