Apache URL rewrite (mod_rewrite) and uuid - regex

I want to rewrite the following URL in apache:
abc.php?id=1234&token=xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
where xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx is a V4 UUID
I have tried to make rewrite rule like:
RewriteRule ^/ABC/([0-9]+)$/^([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}.([0-1]){1}/ /abc.php?id=$1&token=$2
but it seems that it's not really working.
I think i dun need a regex that exactly represent a V4 UUID but just simple regex to represent the Characters and hyphen in the UUID string, anybody has good idea in apache regex?

This is what you want:
RewriteRule ^/ABC/([0-9]+)/([a-zA-Z0-9\-]+)$ abc.php?id=$1&token=$2
If you want to further validate the token, it would be better to do so in PHP, seeing as that's its intended use.

Related

Regex for filename and querystring from url in 2 groups

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

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]

.htaccess RewriteRule - matching patterns & forwarding posts

I'm trying to write a rule that maintains legacy urls with a rule that matches a pattern but I would like to add something that redirects url posts as well. I'm not good a this htaccess stuff. Here is a scenario:
Legacy URL:
www.blah.com/register/player
Legacy RewriteRule:
RewriteRule ^register/(.*)/?$ /account/register.php?type=$1 [NC,L]
New URL:
www.blah.com/register/player?email=bob#g.com
New RewriteRule???
RewriteRule ^register/(.*)/?$ /account/register.php?type=$1 [NC,P]
I found this post Is it possible to redirect post data? that helped me find the P flag which works without the pattern match but it doesn't work when I try to do both.
I don't want to just add another pattern match because my url post data can be arbitrary with more then one param (the case described above is simplified). It seems like this should be doable. Is it? Thanks.
Seems like you need the query string attached on redirect, hence the QSA flag
Try:
RewriteRule ^register/(.*)/?$ /account/register.php?type=$1 [QSA,NC,P]

Redirect all URLs starting with STRING that does NOT include a SUBSTRING

I need some help with regex.
I'm building some 301 rules for an .htaccess
I need to redirect all urls starting with a specific string excluding one that has a given word in the match-all part
this is the simple rule I'm using:
/my/sample/url/(.*)
I need to edit the (.*) part to say: anything except if contains "foobar"
if contains "foobar" I need a different 301 rule
This looks like is working:
^(?!.*foobar)/my/sample/url/(.*)
does anybody have a better solution?

IIS7 URL Rewrite with Regex

I'm trying to do a URL rewrite when a user accesses a certain URL of my site:
Accessed URL: https://client1.domain.com
Rewritten URL: https://new-client1.otherdomain.com
My site has many URLs that point to it, so the simple HTTP redirect module will not be a valid solution for this. What would the Regex be and what would I want to fill in for each section in a rewrite rule?
Thanks
Try this:
s/client1.domain/new-client1.otherdomain/g
You can use this regex pattern to search for client1.domain in order to replace it:
(?<=//)([^.]+)\.domain
Replace it with a backreference to client1 and the new domain like so:
$1\.otherdomain