dongtuanzi1080
dongtuanzi1080
2016-11-29 11:09
浏览 14
已采纳

没有基本名称的htaccess重定向[关闭]

I am handling some urls with the htaccess like below

B)https://www.example.com/online/development/id

Rules:

RewriteRule ^online/(.*)/(.*)$ filename.php?key=$2 [L]

But Now I want to use some urls like below:-

https://www.example.com/development/id

How Can I do this?

图片转代码服务由CSDN问答提供 功能建议

我正在使用下面的htaccess处理一些网址

   B)https://www.example.com/online/development/id
   
 
 

规则:

 <代码>  RewriteRule ^ online /(.*)/(.*)$ filename.php?key = $ 2 [L] 
   
 
 

但现在我想使用一些网址 下面: -

  https://www.example.com/development/id
   
 
 

我该怎么办? 这样做?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doushi8231
    doushi8231 2016-11-29 11:12
    已采纳

    You can use this rule-

    RewriteRule ^([\w-]+)/([\w-]+)$ filename.php?key=$2 [QSA,L]
    
    点赞 评论
  • du8442
    du8442 2016-11-29 11:37

    Your regex matches:

    • ^ - start of the input (the part after HOST)
    • online/ - the literal char sequence online/
    • (.*) - any 0+ chars, as many as possible, up to the last...
    • / - slash,
    • (.*) - Group 2: any zero or more chars up to the...
    • $ - end of input.

    So, the regex can have arbitrary number of subparts in the path.

    If you need to limit the number of subparts, use a negated character class based regex:

    RewriteRule ^[^/]*/([^/]*)$ filename.php?key=$1 [L]
    

    The [^/]+ matches 1+ chars other than /. Since you are not using Group 1, I recommend removing the first set of ( and ) and use $1 in the replacement.

    Now, the regex matches:

    • ^ - start of input
    • [^/]* - zero or more chars other than / (the [^...] is called a negated character class)
    • / - a slash
    • ([^/]*) - Group 1: see above
    • $ - end of input.

    And that means there can be only 1 slash in the path.

    点赞 评论

相关推荐