Regex for Wordpress redirection plugin - regex

I'm using this redirection plugin https://redirection.me and I'd like to redirect traffic if a query parameter is not present:
/relative-location/?must-be-there=value&other1=value&other2=value
If must-be-there is not there or has no value, I'd like to redirect - but I do not know how to write the regular expression to make that work. I started with
^((?!must[-]be[-]there).)*$
but I don't know where to go next to create the full url and have it work with the plugin.

To match when the path starts with /relative-location/, but query parameter must-be-there either has with no value or is missing:
^/relative-location/(?!.*(\?|&)must-be-there=[^&]).*$
See live demo.

Related

Regex to match url path - Golang with Gorilla Mux

I'm setting up an api endpoint which after parsing the url I would get a path such as /profile/<username> e.g. /profile/markzuck
The username is optional though as this endpoint returns the the authenticated users profile if username is blank
Rule set:
I'm not the best at regex but I've created an expression that requires /profile after that if there is a following / e.g. /profile/ then you need to have a <username> that matches (\w){1,15}. Also I want it to be allowed to match any number of combinations if there is another following / e.g. /profile/<username>/<if preceding "/" then anything else>
Although I'm not 100% sure my expression is correct this seems to work in JavaScript
/^\/(profile)(\/(?=(\w){1,15}))?/
Gorilla Mux though is different and it requires the route matching string to always start with a slash and some other things I don't understand like it can only use non-capturing groups
( found this out by getting this error: panic: route /{_dummy:profile/([a-zA-Z_])?} contains capture groups in its regexp. Only non-capturing groups are accepted: e.g. (?:pattern) instead of (pattern) )
I tried using the same expression I used for JavaScript which didn't work here. I created a more forgiving expresion handlerFunc("/{_dummy:profile\/[a-zA-Z_].*}") which does work however this doesn't really follow the same rule set I'm using in my JavaScript expresion.
I was able to come up with my working expresion from this SO post here
And Gorilla Mux's docs talks a little bit about how their regex works when explaining how to use the package in the intro section here
My question is what is a similar or equivalent expression to the rule set I described that will work in Gorilla Mux HandlerFunc()?
If you're doing this with mux, then I believe what you need is not regex, but multiple paths.
For the first case, use a path "/profile". For the one containing a user name, use another path "/profile/{userName}". If you really want to use a regex, you can do "/profile/{username:}" to validate the user name. If you need to process anything that comes after username, either register separate paths (/profile/{username}/otherstuff), or register a pathPrefix "/profile/{username}/" and process the remaining part of the URL manually.

regex - Match # part of the url on the server

I'm trying to write a regex to match parts of urls and use a SEO redirection wordpress plugin to create a 301 redirect on the matching results.
if, for example, I write these URLs:
https://www.test.com/my-site
https://www.test.com/my-site/
I want to be redirect to:
https://www.test.com/your-site/
but if the urls are followed by an hash (#) like the one below:
https://www.test.com/my-site/#/..
Do not redirect.
I have played around for a bit with regExr and this is as far as I could get:
regexr.com/3scpb
But when try to implement it inside the plugin the redirect doesn't work.
What am i doing wrong here?
Is it better to do it straight inside the .htaccess file?
would it be better and more robust/reliable that way?
Thanks
The hash is never sent by the browser.
The hash is used internally by the browser to see which fragment of the document is focused on. This is called fragment identifier. This means your server will never see the # coming up. You cannot prevent this behavior.

Need help creating regex redirects for URLs with query strings attached

The issue is when using infusionsoft or another email platform, when a URL is used in an email, it adds a query string to the URL. If that URL is being redirected, it will not redirect properly with the query string attached, sending the user to a 404 page.
I am trying to figure out how to correctly create a regex expression in order to redirect the page and catch that query attached to it in order to redirect properly.
I think I've figured out how to do THAT, but then need to figure out how to exclude a URL that has the same beginning text...
For example:
If the original url is: /page-url/
And needs to redirect to /page-url-free/
So these versions need to redirect:
/page-url/page-url/
/page-url/?inf_contact_key=474a03f754bb3dadf5415b3b652fc7baa6979sf0112d3fe
But I need the regex expression to NOT catch /page-url-free/ since that would cause an infinite loop.
Any advice would be amazing. Thanks so much
If you just need to replace page-url with page-url-free, with keeping the query parameters (if provideds) exactly the same, the following regex will work:
\/(page-url)(?:\/\?.+)?$
The above regex will capture page-url which can then be easily replaced with page-url-free depending on what you are using.

Regex to match a URL with parameters but reject URL with subfolder

Short Question: What regex statement will match the parameters on a URL but not match a subfolder? For example match google.com?parameter but not match google.com/subdomain
Long Question: I am re-directing a few URLs on a site.
I want a request to ilovestarwars.com/page2 to re-direct to ilovestarwars.com/forceawakens
I setup this re-direct and it works great most of the time. The problem is when there are URL parameters. For example if someone sends the URL using an email program that tracks links. Then ilovestarwars.com/page2 becomes ilovestarwars.com/page2?parameter=trackingcode123 after they send it which results in a 404 on my site because it is looking for the exact URL.
No problem, I will just use Regex. So I now re-direct using ilovestarwars.com/page2(.*) and it works great accepts all the parameters, no more 404s.
However, trying to future proof my work, I am worried, what happens if someone adds content inside the page2 folder? For example ilovestarwars.com/page2/mistake
They shouldn't, but if they do, it will take them forever to figure out why it is redirecting.
So my question is, how can I create a regex statement that will match the parameters but reject a subfolder?
I tried page2(.*?)/ as is suggested in this answer, but https://www.regex101.com/ says the slash is an unescaped delimiter.
Background info as suggested here, I am using Wordpress and the Redirection plugin. This is the article that goes over the initial redirect I setup.
A direct answer to your question would be something like this: ^/([^?&/\]*)(.*)$
This assumes the string starts at the first / (if it doesn't, remove the / that follows the ^). In the first capture group you will get the page name (page2, in the case of your example URL) and in the second capture group, you will get the remaining part of the url (anything following one of these chars: ?, &, /, \). If you don't care about the second capture group, use ^/([^?&/\]*).*$
An indirect answer would be that you don't do it this way. Instead, there should be an index page in folder page2 that uses a 301 redirect to redirect to the proper page. It would make much more sense to do it statically. I understand that you may not have that much control over your webpage, though, since it is Wordpress, in which case the former answer should work with the given plugin.

Changing Friendly url format

I need to modify URL pattern for IIS Url rewrite module and I could not come up with proper regex pattern.
I already have a rule for front end of the site:
/[event-name] ==> show-event.aspx?event=[event-name]
I need another one for admin login page.
/[event-name]/admin==> admin-login.aspx?event=[event-name]
But IIS gives me this option:
/admin-login/[event-name] ==> admin-login.aspx?event={R1}
How can I change order in this regex pattern for the user friendly url format I want?
^admin-login/([^/]+)/?$
PS: Rewrite Maps is not an option because the event-name will be a parameter and maps are static.
Thanks.
You are using presets rather than empty rules, so at first your options are limited. You can, however, edit your rule and change the pattern to:
^([^/]+)/admin/?$
and keep the rewrite target (admin-login.aspx?event={R:1}). That should do the job.