Match last occurence of 4 digits htaccess rewrite

2019-09-10 09:58发布

I'm wondering what sort of regex I could use to match the last occurence of a set of 4 digits in my url.

My url is being formatted like this

/ARTIST/ALBUM-4 DIGITS-EXTRA STUFF

In the url, "4 DIGITS" will be the last possible occurence of 4 digits together. The thing is the ALBUM may contain sets of 4 digits, so it needs to match the last occurrence in the url.

Basically I need my rewrite rule to act as follows

RewriteRule ^(.*)/(.*)-(.* MATCH LAST OCCURENCE OF 4 DIGITS)-(.*)?$ album.php?artist=$1&album=$2&releaseyear=$3&EXTRA=$4

Is there any way to do this?

2条回答
Summer. ? 凉城
2楼-- · 2019-09-10 10:14

You can use:

RewriteRule ^([^/]+)/(.+?)-(\d{4})-(\D*)$ album.php?artist=$1&album=$2&releaseyear=$3&EXTRA=$4 [L,QSA,NC]
查看更多
The star\"
3楼-- · 2019-09-10 10:29

I would use this rule:

RewriteRule ^(.*)\/(.*)-(\d{4})(?:-(.*))?$ album.php?artist=$1&album=$2&releaseyear=$3&EXTRA=$4

Try it

查看更多
登录 后发表回答