Match last occurence of 4 digits htaccess rewrite

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


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?


I would use this rule:

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

Try it

You can use:

RewriteRule ^([^/]+)/(.+?)-(\d{4})-(\D*)$ album.php?artist=$1&album=$2&releaseyear=$3&EXTRA=$4 [L,QSA,NC]

Category:regex Time:2018-12-09 Views:1

Related post

  • How can I match everything that is after the last occurence of some char in a perl regular expression? 2010-08-09

    For example, return the part of the string that is after the last x in axxxghdfx445 (should return 445). --------------Solutions------------- my($substr) = $string =~ /.*x(.*)/; From perldoc perlre: By default, a quantified subpattern is "greedy", th

  • Looking for Java Regular expression to match last 2 digits in the given range 2015-03-10

    I am trying to write a java regular expression to match last 2 digits of a 12 digit number to a range of digits. So for example I want to match the given 12-digit number whose last 2 digits falls in the range of (00-24) or (25-49) or (50-74) or (75-9

  • regex: match a pattern between last occurence of a "/" and the end of the line 2011-04-14

    I can't figure out how to match a pattern between LAST / and the end of the line. I have tons of: /usr/etc/blabla:/etc/bbb /usr/etc/blabla:/etc/ /usr/etc/blabla:/local/fffusr /usr/etc/blabla:/bin/dfusrd /usr/etc/var:/etc/aaaaaf.ju For example

  • How to find last occurence of a number in a string using Ruby? 2009-10-17

    Using Ruby... given the following string: x = "[4543]junk_junk" How do I remove all text after the last number/digit? I thought the easiest way to do this might be by finding the index of last occurrence and then removing everything a

  • htaccess rewrite still some problems, getting a bit complex eek! 2011-04-14

    Another edition to my .htaccess rewriting saga. Everything is now working to a degree but still have some issues. I have numerous rules to move old urls to new urls, passing variables etc - however there are still a couple of things i need to add, an

  • Using an SSL certificate with .htaccess rewrite 2009-10-01

    I'm wondering, if I have an .htaccess rewrite setup on a virtual subdomain, ie. that has a rewrite to and I have an SSL certificate on, will I run into problems if I try and access htt

  • Last occurence of element in a list in OCaml 2009-10-12

    Assuming l is a list and elem is an element, how can I return the last occurence of the element elem in the list l? Also return -1 if the element does not exisit in l. I don't quite understand how to use recursion for iterating through the list... le

  • .htaccess rewritting subdomain + folder to uri 2009-12-20

    I want to redirect this: TO Sorry for being too obvious. I know the htaccess rewritting basis but this is making me a little crazy :) --------------Solutions------------- RewriteBase / RewriteCond %{H

  • Django AND .htaccess rewrites/redirects, is this possible? 2010-05-27

    Is it possible to have Apache htaccess rewrites take effect before it hits django? I want to be able to specify RewriteRules in an htaccess file that take precedence over django, and if nothing matches then it gets dispatched to mod_wsgi/django. We'r

  • How to replace the last occurence of a substring in ruby? 2010-07-06

    I want to replace the last occurence of a substring in ruby. What's the most eastest way? For example, in abc123abc123, I want to replace the last abc to ABC. How can I did that? --------------Solutions------------- "abc123abc123".gsub(/(.*(abc.*)*)(

  • Replace the last occurence of a character in a file 2011-03-09

    I have this AWK script which replaces every form feed character (0x0C, 12) with 5 new line characters: f=${*:-"-"} awk 'BEGIN {FF=sprintf("%c",12); LF=sprintf("\n\n\n\n\n")} {i1=0;for(i2=i1+1;i2<=length($0);i2++) if(substr($0,i2,1) == FF) {print s

  • Regular expression to match last number in a string 2011-03-16

    I need to extract the last number that is inside a string. I'm trying to do this with regex and negative lookaheads, but it's not working. This is the regex that I have: \d+(?!\d+) And these are some strings, just to give you an idea, and what the re

  • htaccess rewrite loop? 2011-07-14

    I have user-uploaded files stored in /public/upload/* for instance: /public/upload/gallery/segway/child-on-a-segway.jpg My current .htaccess rules already eliminate the /public part, but I still need to get rid of the /upload part, BUT, only for file

  • Apply htaccess rewrite-rule on rewritten URL 2011-08-23

    Is it possible to apply a .htaccess rewrite-rule on an already rewritten URL? Example: I want to resize my pictures with timthumb.php (most recent version, so there should be no security flaw any more). But the URL should look fine, therefore I creat

  • Regex to find content of the last occurence of square brackets 2012-01-05

    Hi Everybody, I'm Currently using preg_match and I'm trying to extract some informations enclosed in square brackets. So far, I have used this: /\[(.*)\]/ But I want it to be only the content of the last occurence - or the first one, if starting from

  • Tips for debugging .htaccess rewrite rules 2012-02-05

    Many posters have problems debugging their RewriteRule and RewriteCond statements within their .htaccess files. Most of these are using a shared hosting service and therefore don't have access to the root server configuration. They cannot avoid using

  • .htaccess Rewrite Question 2009-05-27

    I have a number of pages in my site, as one would expect. For example: index.php submit.php view.php?id=blah I want these rewritten like index/ submit/ view/blah Whats the best way of doing this? --------------Solutions------------- The ways of handl

  • Making a .htaccess rewrite rule respect subdirectories automatically 2009-10-14

    I'm making a downloadable self-hosted web app (using CodeIgniter), and need the .htaccess rewrite rule to be able to tell if it's in a subdirectory or not to be able to make the rewrite rule correctly. For example, if the app is being installed at ex

  • Match last word after / 2010-02-16

    so, i have some kind of intern urls: for example "/img/pic/Image1.jpg" or "/pic/Image1.jpg" or just "Image1.jpg", and i need to match this "Image1.jpg" in other words i want to match last character sequence after / or if there are no / than just char

Copyright (C), All Rights Reserved.

processed in 0.372 (s). 13 q(s)