Regex for filename and querystring from url in 2 groups - regex

I'm trying to write a mod_rewrite rule using a regular expression, and I'm a bit green as to some of the processes involved.
I believe I can do what I want if I can figure out how to get this regular expression right.
String is http://www.a.com/b.css?v=1234
I know I can get b.css?v=1234 with the regex
([^\/]+$)
What I'm looking for is it grouped so that %1 is b.css and %2 is 1234. Any help is appreciated. Thanks.

Based on the url you provided:
http://www.a.com/b.css?v=1234
You can use:
/(\w+\.\w{3})\?v=(\d+)
Debuggex Demo
For java remember to escape backslashes to:
/(\\w+\\.\\w{3})\\?v=(\\d+)
Hope to help

You need both a Condition and one or more Rules.
One of several ways to do it, tested on Apache 2.2 and 2.4:
RewriteCond %{QUERY_STRING} v=(\d+)
RewriteRule ^([^/]*) DoSomethingWithFile_$1_AndDigits_%1?
Input url: www.yoursite.com/b.css?v=1234
%1 contains 1234
$1 contains b.css
Rewritten url: www.yoursite.com/DoSomethingWithFile_b.css_AndDigits_1234

Related

htaccess dynamic target using regular expressions

Hi I want to create a rule to remove the first directory on the url, see the example:
request url: http://www.example.com/San-Salvador/help
I want to redirect that to
Target url: http://www.example.com/help
the pattern is [base url][city name][directory] and I want to recreate it as this [base url][directory name]
Here is a general regex which should work:
^(http:\/\/www\.example\.com\/)(.*\/)(.*)
Each term in parentheses is a group which will potentially match an input string. For the input string:
http://www.example.com/San-Salvador/help
here are the matching groups:
http://www.example.com/
San-Salvador/
help
The groups you want to retain are the first and third ones, i.e. http://www.example.com/ and help to give you http://www.example.com/help
You can explore this regex here at Regex 101.
For the most part this is a rule that you can use to remove the city. However your code will need to handle what happens after the redirected URL is requested. Meaning what is displayed when this is called http://www.example.com/help
RewriteEngine on
RewriteRule ^(?:[^/]+)/([^/]+)/?$ /$1 [L]

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

Regular Expression to capture URLs with ascii encoded characters

Having migrated a Wordpress site to a new build, I need to capture a lot of old URLs and redirect them to the same content on the new site. The problem is that the old site has a lot of URLs with ascii-encoded chars and Wordpress has stripped them out on the current site. For example:
/blog/uncategorized/germany%E2%80%99s-ageing-population-working-longer-working-better.html
would redirect to:
/blog/germanys-ageing-population-working-longer-working-better/
Can anyone provide a regular expression that would remove the ascii-encoded characters?
For matching the encoded characters, you would use the following regex pattern:
%[A-Z0-9]{2}
How you perform the replacement will depend on the language/tool you are using.
You have to match against the request here, because with redirect and rewrite rules, the URI is decoded before the patterns get applied. That means you'd be matching against stuff like รข instead of the encoded strings. So you'll want something like:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /blog/([^\?\ ]*)\%[A-Z0-9]{2}([^\?\ ]*)
RewriteRule ^ /blog/%1%2 [L,R=301,NE]

REGEX: Extract parameter from URL

What REGEX should I use to extract the following parameter from a url string:
/?c=135&a=1341
I basically want to get the value of the a parameter from this string.
Thanks,
If you want to extract the value of a, and the value consists of one to many digits, this regex should work:
preg_match("/a=(\\d{1,})/ui", $_SERVER['REQUEST_URI'], $matches)
Then use $matches[1] to display the a value
I am going to answer a slightly more general Q which is suggested by your ? prefix that you are trying to remove a specific parameter from a URI request string (which drops the leading ?). And in this case using the mod_rewrite engine so that you can implement this in your .htaccess file.
The rule is somewhat more complex because you don't necessarily know where in the query parameters a=XXX comes, so you need different regexps for the case where a is first and a is a subsequent parameter. You do this by ((?=a=)regexp1|regexp2) so here it is:
RewriteEngine on
RewriteBase \
RewriteCond %{QUERY_STRING} ^(?(?=a=)a=[^&]*&?(.*)|(.*)&a=[^&]*(&.*)?)
RewriteRule ^.* $0?%1%2%3 [L]
If a is first the %1 contain rest otherwise %2 and %3 the bookends (%3 may be blank).
If you want this to occur for specific scripts then replace the rule regexp ^.* by a more specific one.
Enjoy :-)

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