Help me write a Regex RewriteRule for htaccess - regex

I need help writing a simple regex for RewriteRule for mod_rewrite in htaccess. So, here is what I am trying to accomplish:
books/2010-the-world-by-hopkins-139_PPS-1234567
should go to
index.php?pagename=mypage&PPS=1234567&description=2010-the-world-by-hopkins-139
So, in pseducode, the regex has to split the part after books by _ and I should get it into two parts:
PPS (it is always a numeric with 1+ variable number of digits). This is the part after _PPS-
Description (it is always a string). This is the part containing ANYTHING before the _.
I guess the RewriteRule will be something like:
RewriteRule books/(.*)_(.*) index.php?pagename=mypage&PPS=$2&description=$1
But I need correct regex. Plese help.

something like: but flip the $1 and $2 ;)
^books/(.*)_PPS-([0-9]{1,})$

Related

htaccess - combine rewrites into single

I have a couple of RewriteRules that I'd like to combine into one. I'm not sure how to do it, though. I think look arounds would need to be used? The difference between the two is the first would match something like:
search/foo or search/foo/
and the second would match
search/foo/10 or search/foo/10/
Rewrites:
RewriteRule ^search/([a-zA-Z]+)/?$ index.php?page=search&query=$1&pn=1 [L]
RewriteRule ^search/([a-zA-Z]+)/([0-9]+)/?$ index.php?page=search&query=$1&pn=$2 [L]
Without using look arounds, my attempt would be
^search/([a-zA-Z]+)/?([0-9]+)?/?$
But i think that would match something undesirable like this?
search/foo10
edit:
I'm tryin to get the regex to match the following URIs:
search/foo
search/foo/
search/foo/1
search/foo/1/
^search/([a-zA-Z]+)(/)?([0-9]+)?\/?$
will this work? you might have make '/' also as match pattern and ignore $2.Same can be followed for the trailing '/' and ignore $4.

mod rewrite friendly url

I'm new to regular expressions and need to rewrite an example URL:
http://domain.com/quiz.php?id=1
To the friendly URL,which looks like forum URL, like this:
http://domain.com/1-quiz-title
So 1 is the GET variable. Title must be lowercase only
I tried the following but it seems incorrect:
RewriteRule ^([a-z0-9\-]+)$ quiz.php?id=$1 [L]
It's picking the GET variable as 1-quiz-title while it should be only 1
Thanks
If the get variable is only numbers, you want the regex to be like this:
RewriteRule ^([0-9]+)- quiz.php?id=$1 [L]
So the regex matches some amount of numbers first and groups it (the parentheses), the matches a "-". Note that there isn't a $ for end of match, this is essentially going to ignore the title completely, not even going to try to match it. The title doesn't need to be in the rewritten URL so we don't really care what comes after the -.

mod_rewrite: match string within URL, which regex to chose?

I would like to use mod_rewrite to capture a string within brackets in my URL and do a redirect.
My URL:
something?var_a=A&var_b=(B)&var_c=C
my .httaccess file with the regex:
RewriteEngine on
RewriteRule ^/?.+var_b=\((.*)\)$ somedir/$1 [R]
I just would like to capture what's in between the round brackets, so my redirect should look something like this: somedir/B
I test my regex at http://htaccess.madewithlove.be/ but I get no match.
I don't know what I am missing here, even if I try much simpler regexes, e.g. .+var_b(.*)$ I get no match. Only if my regex was looking for a pattern at the beginning, I get a match, so for example the regex something(.*)$ works.
What am I missing here?
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)var_b=\((.*?)\)(&|$) [NC]
RewriteRule ^.*$ somedir/%2? [R]
The reason is that RewriteRule does not receive the ?x=y part of the query. The %2 variable refers to the pattern from the last RewriteCond, while $2 would refer to the pattern from this RewriteRule. The ? at the end prevents the query part ?x=y from being automatically appended at the end of the result.
The (^|&) and (&|$) in the pattern guarantee that var_b=(B) is the complete parameter and not a part of it. Without these, the pattern would also match ?xyzvar_b=(B) or ?var_b=(B)xyz. With these, it will only match ?var_b=(B) or ?a=b&var_b=(B)&x=z etc.

Regex to grab string until a hyphen

I think I'm having an issue since I'm using two key regex values in this expression.
RewriteRule ^([^-]*)-([^-]*)-((foot|basket)(ball))-schedule$ /schedule.php?sport=$3&school=$1&year=$2&schedule=true [NC,L]
I this to be caught when someone types
domain.com/michigan-1999-football-schedule
. It currently doesn't recognize this string with this htaccess line, and I'm 99% it has to do with the regex part. I think it's because the [^-] part of the line. I am hoping this grabs the data until a hyphen, but I think there's an issue since both are key characters in regex.
This is working for me as-is. Do you have other rules that you are using?
Make sure that you have the following:
RewriteEngine on
RewriteBase /
I would advise you to use + instead of *, in the case some fields would be empty:
RewriteRule ^([^-]+)-([^-]+)-((foot|basket)(ball))-schedule$ /schedule.php?sport=$3&school=$1&year=$2&schedule=true [NC,L]

Match most punctuation using ModRewrite regex

I'm using ModRewrite to send requests for
/2/Blog-Title
to
/?post=2&title=Blog-Title
Originally I used ([A-Za-z0-9-_/]+), but then realised that people were using lots of punctuation in their titles. I've gradually added lots more punctuation, but am feeling that this is the wrong way to go about this... I'm now using
RewriteRule ^([0-9]+)\/([A-Za-z0-9-_/\.\?\!':\&]+)$ /?post=$1&title=$2 [L]
How can I ignore any requirements for the title, and just match any text following the number? (I don't actually need the title= bit set, the text is really for SEO, not for internal working.)
If you want to ignore title just do:
RewriteRule ^([0-9]+)\/.*$ /?post=$1 [L]
.* will match any string after / so it will matches anythong like:
/2/lores-ipsum_etc356