.htaccess regex rewrite rule

老子叫甜甜 提交于 2020-01-16 08:37:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!