问题
I have the following regex:
RewriteRule ^blogs/([^/]*)/([^/]*) blogs/index.php?blogger=$1&blog=$2
This works fine for the following cases:
- http://myurl.com/blogs/blog-name/blog-article/
- http://myurl.com/blogs/blog-name/blog-article
- http://myurl.com/blogs/blog-name/
however it does not handle:
- http://myurl.com/blogs/blog-name
How can I make the "/" separator optional in this regex?
回答1:
I'd use:
^blogs/([^/]*)(/([^/]*))?
And you'd just have to check and make sure that $2
is still correct (with the two capture groups, it might be $3
… I can't remember).
回答2:
If the '?' is causing troubles then try:
^blogs/([^/]*)(/([^/]*)){0,1}
来源:https://stackoverflow.com/questions/3077705/htaccess-regex-rewrite-rule