mod_rewrite - including removal of part of query string

Associate
Joined
17 Apr 2006
Posts
549
Location
Staffordshire
I'm trying to rewrite 3 URLs, but because the 1st and 2nd rule have the same pattern, the 2nd rule is never reached, and so music.php is served in the browser, instead of music2.php.

Original URLs:
https://example.com/music.php?title_url=example-string
https://example.com/music2.php?country=england
https://example.com/music2.php?country=england&region=staffs


Relevant mod_rewrite rules in htaccess:
Code:
RewriteRule ^music/([a-zA-Z0-9-]+)$ music.php?title_url=$1
RewriteRule ^music/([a-zA-Z0-9-]*)$ /music2.php?country=$1
RewriteRule ^music/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ music2.php?country=$1&region=$2


Result I want:
https://example.com/music/example-string
https://example.com/music/england
https://example.com/music/england/staffs


Is there a way of adding a prefix or suffix to the "title_url" parameter of the first URL, to distinguish between the first and second patterns in my htaccess, without that prefix or suffix appearing in the URL that displays in the browser? E.g. every title_url would end with 999 (the title URLs may contain numbers, so I can't use a number alone to identify):

https://example.com/music.php?title_url=example-string999

The rewrite for that should display:

https://example.com/music/example-string

The following can be used to remove 3 number 9s, but I've no idea if it's possible to combine it with the other rule that affects title_url:
Code:
9{3,}

I'll be extremely grateful for any solutions. If this isn't possible, maybe there's another way around it?
 
Last edited:
If memory serves, mod rewrite stops on a match so if you swap the order of rule 2 and 3 I think you will get what you are looking for.

You mean like this?

Code:
RewriteRule ^music/([a-zA-Z0-9-]+)$ music.php?title_url=$1
RewriteRule ^music/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ music2.php?country=$1&region=$2
RewriteRule ^music/([a-zA-Z0-9-]*)$ /music2.php?country=$1

That won't work, because it stops on rule 1, so will never get to the 3rd rule (just as it wouldn't when it was the 2nd).
 
Sorry I'd not read this correctly. So the issue is really the first 2 example /music/example-string and /music/england are really the same thing in terms of your current match. The only alternative I can think of is changing that up to match a know list, so something like;

Code:
RewriteRule ^music/([a-zA-Z0-9-]+)$ music.php?title_url=$1
RewriteRule ^music/(england|scotland|wales)$ music2.php?country=$1
RewriteRule ^music/(england|scotland|wales)/([a-zA-Z0-9-]+)$ music2.php?country=$1&region=$2
 
Sorry I'd not read this correctly. So the issue is really the first 2 example /music/example-string and /music/england are really the same thing in terms of your current match. The only alternative I can think of is changing that up to match a know list, so something like;

Code:
RewriteRule ^music/([a-zA-Z0-9-]+)$ music.php?title_url=$1
RewriteRule ^music/(england|scotland|wales)$ music2.php?country=$1
RewriteRule ^music/(england|scotland|wales)/([a-zA-Z0-9-]+)$ music2.php?country=$1&region=$2
Brilliant! Thanks. I had to move the 2nd rule above the 1st, which you'll see why, but that works perfectly. I'd spent hours trying to get the PHP for the two pages onto the same page with an if else statement, so I'm glad I can ditch that.
 
Back
Top Bottom