Changing Friendly url format - regex

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.

Related

Regex for Wordpress redirection plugin

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.

Regex to target a page but not its children

I'm trying to write a regular expression to target a URL but not any of its children. My regex is definitely pretty weak and could use some help.
Page I want to target (may include trailing slash and or UTM parameters): https://test.com/deals/
Example of a page I do not want to target: https://test.com/deals/Best-Sellers/c/901
My attempt:
.*Deals\/((?!Best).)*
You can use \/deals\/?(?:[?#]\S*)?$
Check on Regex101
This is a bit more permissive than what your question suggests but it might come in handy.
The main thing is that it tries to match /deals at the end of the line. This ensures that you won't match, say https://test.com/best-deals or similar but only the URL that ends with /deals. Also, the final / is optional - you might get https://test.com/deals.
In addition to that, the regex allows for the URL to end with # anchors or ? followed by parameters. The page might allow this right now or in the future - for example, if a link is used that leads to the same page (e.g. to a specific section), you'd get a # added to the URL. Or there might be something like a filter configuration embedded in the URL https://test.com/deals/?sort=price&productsPerPage=15&page=2&minPrice=100.
Finally, you should make your regex case insensitive to account for the fact the URL might also be https://test.com/Deals/. How you set this flag will depend on where you are using the regex, so I am just adding this as a reminder.

Append UTM tracking to URLs

I'm trying to set a custom rule within the catalog settings in Facebook's business manager, I want to append UTM tracking to my product URLs and cannot do this from a find and replace regex function. A unique identifier of my product URLs is that they always end with a number 0-9. I'm new to regex and can't figure out how to do this, example below for reference:
Existing product URL:
https://www.example.com/product/12345
https://www.example.com/product/54321
Appended UTM tracking:
https://www.example.com/product/12345?utm_source=askjeeves&utm_medium=cpm
https://www.example.com/product/54321?utm_source=askjeeves&utm_medium=cpm
Any help on how to write a find and replace regular expression to help me append tracking to help me achieve similar to my example above would be much appreciated!
Image below from where I am trying to input this rule:
screen grab in FB business manager catalog custom rule settings
You can do this on your .htaccess. Try the following code into your .htaccess file (Create one if you don't have already) and give me a feedback please.
RewriteEngine On
RewriteRule product/([0-9]+) product/$1?utm_source=askjeeves&utm_medium=cpm [L]
With this code you'll navigate to www.example.com/product/12345 but the system will see full URL which is www.example.com/product/12345?utm_source=askjeeves&utm_medium=cpm
I don't know facebook's find and replace function, but generally a regex should look like this:
(^http.*$)
Then replace with:
$1?utm_source=askjeeves&utm_medium=cpm
You can also try with:
\1?utm_source=askjeeves&utm_medium=cpm
If facebook follows 'normal' regexes, this should Work.
Edit: try these things too, it might Work.

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.

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.