简体   繁体   中英

In .htaccess what is the difference between (.*)$ and .*$

When using rewrites, what is the difference between (. )$ and . $ for example in the two lines below:

RewriteRule ^blog/blog(.*)$ http://example.com/blog$1 [L,R=301]
RewriteRule ^blog/blog.*$ http://example.com/blog$1 [L,R=301]

Thanks

Those two regex patterns themselves (.*)$ and .*$ mean the same thing:

the . means any single character
the * is the quantifier and means 0 or more occurrences of that any char
the $ is the end of string character...

The difference is that the first one uses a grouping with the parens. It simply means that portion of the match (.*) can then be used in a back-reference with $#. So for the examples you gave:

This makes sense since your $1 in the substitution has a grouping to pull from:

RewriteRule ^blog/blog(.*)$ http://example.com/blog$1 [L,R=301]

This does not make sense since the $1 has nothing to pull from:

RewriteRule ^blog/blog.*$ http://example.com/blog$1 [L,R=301]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM