mod rewrite friendly url - regex

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 -.

Related

negating rewrite rules for apache

I'm trying to make it so that if the ending of a URL contains anything surrounding a number (except that the first part can be any combination of numbers, a hyphen or a p), then the url is redirected with whatever surrounding the number is taken off.
Here's my regex:
RewriteRule ^all/[^p^P^0-9^\-]+([0-9]+).*$ /allof/$1 [R=301,NC,L]
If I tried these test URLs, the redirect should happen, but does not:
http://example.com/all/-a*1
http://example.com/all/plus100
If I tried this test URL, the redirect does not happen which is correct:
http://example.com/all/p1-100
If I tried these test URLs, the redirect happens, which is correct:
http://example.com/all/(100) - redirects to http://example.com/allof/100
http://example.com/all/minus100 - redirects to http://example.com/allof/100
Perhaps my regex is faulty. I tried removing the extra carets in the square brackets except for the first, and that didn't help, and I don't want to replace the square brackets with only a .* since I then won't be able to capture the number. What could I be doing wrong?
You can use negative lookahead in your rule:
RewriteRule ^all/(?!p\d*-)\D*(\d+) /allof/$1 [R=301,NC,L]
RegEx Demo

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.

Htaccess regex to exclude everything except one string

I have done this: http://rubular.com/r/AHI15Tb4ju, and it match the second url (http://gamempire.localhost.it/news/tomb-pc), but I want to exclude that url and match everything that do not have the word "news/" inside (but at the same time end in the way that I have specified).
How to do that?
Basically, i want to match only the third url (http://gamempire.localhost.it/tomb-pc).
Thanks!
You can use a rule like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/news/
RewriteRule -(?:pc|ps2|ps3|ps4|xbox-360|xbox-one|xbox|wii-u|wii|psp|ps-vita|ds|3ds|iphone|ipad|android|playstation)(.*)$ / [L,R]
Since I didn't know any action part I just redirected these matching URI patterns to / that you can change according to your need.
Try using this:
^((?!news).)*-(?:pc|ps2|ps3|ps4|xbox-360|xbox-one|xbox|wii-u|wii|psp|ps-vita|ds|3ds|iphone|ipad|android|playstation)(.*)$
It should be noted that I tried to modify your original pattern as little as possible, assuming you also needed the (.*) at the end even though it appears that this is unnecessary for your purposes, and would match strings such as
"http://gamempire.localhost-pc.it/tomb" and "http://-pcgamempire.localhost.it/tomb".

mod_rewrite rule using date regex

I'm trying to write a rule that when user types in this url:
domain.com/09/13/2013/thisIsMyPageTitle
That url stays in browser window, but content from this url is displayed:
domain.com/contentlibrary/thisIsMyPageTitle
This is my rule that I currently get an error with:
RewriteEngine On
RewriteRule ^((0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d[/])$(.*) /contentlibrary/$1 [L]
I'm trying to match the date with regular expression, and use the (.*) from the initial url in the second one that holds the content and actually exists.
If you're not going to do anything with date then why bother being precise with date semantics. You can simplify your regex:
RewriteRule ^[0-9]+/[0-9]+/[0-9]+/([^/]+)/?$ /contentlibrary/$1 [L]
The error that you're getting is probably because you have unescaped spaces in your regex. Specifically these:
[- /.]
The spaces get interpreted by mod_rewrite as the delimiter between parameters. Additionally, you have this:
$(.*)
at the end of your pattern. The $ matches the end of the string, so you want those swapped:
(.*)$
So:
^((0[1-9]|1[012])[-\ /.](0[1-9]|[12][0-9]|3[01])[-\ /.](19|20)\d\d[/])(.*)$
shold be the pattern that you want.

Help me write a Regex RewriteRule for htaccess

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,})$