2

I've developed a small (and powerful and lightweight :-D) php MVC Framework

the issue I'm currently having is: the routes follow this order

index.php?option=samplemodule&action=sampleController

That would link to a samplemodule and would execute the sampleController, pretty much as zf used to do a while ago.

I'm trying to get the .htaccess to rewrite the rules as /module/action

but for some reason it's not working, here's the piece of the code

RewriteEngine On    # Turn on the rewriting engine
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule     ^[A-Za-z0-9-]+/([A-Za-z0-9-]+)/?$ index.php?option=$1&action=$2 [NC,L]
RewriteRule     ^[A-Za-z0-9-]+/([A-Za-z0-9-]+)?$ index.php?option=$1&action=$2 [NC,L]
RewriteRule     ^([A-Za-z0-9-]+)/?$ index.php?option=$1&action=main [NC,L]
RewriteRule     ^([A-Za-z0-9-]+)?$ index.php?option=$1&action=main [NC,L]

Dealing with regexes is quite difficult

i'd also like it to, when passing variables, these could be in the form of /module/action/var1/value1/var2/value2

but i'm not sure of how the rewrite pattern engine works.... does it use several rules recursively?

4

1 回答 1

1

它是否递归地使用多个规则?

确实如此,但并不像听起来那么简单。

至于可以无限期继续的 URL 模式,例如:/module/action/var1/value1/var2/value2

你可以尝试这样的事情:

# turn everything after /module/action/XXXX to query string variables
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([^/]+)/([^/]+)(.*)$ /$1/$2/$5?$3=$4 [L,QSA]

# turn /module/action/ into option= and action=
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?option=$1&action=$2 [L,QSA]

# turn /module/ into option= and action=main
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?option=$1&action=main [L,QSA]
于 2013-10-08T14:03:21.673 回答