开发者

Simple regex for .htaccess?

开发者 https://www.devze.com 2023-02-05 02:31 出处:网络
I have a regular expression for a framework to convert host.com/controller/method in to host.com/index.php?controller/method

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?


Something like this?

RewriteRule ^(\w+)/(\w+)$ /index.php?controller=$1&method=$2 [L]


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

0

精彩评论

暂无评论...
验证码 换一张
取 消