没有基本名称的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?

You can use this rule-

RewriteRule ^([\w-]+)/([\w-]+)$ filename.php?key=$2 [QSA,L]

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.