RegEx to Strip text from middle of URL - regex

I have a client that's moving her site from Blogger to a Wordpress blog. I put some code in Blogger to redirect visitors to the right Wordpress post/page. So when Blogger redirects a post, it comes through as www.domain.com/?bloggerURL=/yyyy/mm/the-post-slug.html
With this Regex, I'm successfully returning the proper Wordpress URL: www.domain.com/yyyy/mm/the-post-slug (with "?bloggerURL=/" and ".html" removed)
^\?bloggerURL=/(.*)\.html$
Blogger Pages get redirected like www.domain.com/?bloggerURL=/p/the-page-slug.html I tried just adding the /p to the Regex to strip out this case also, but it's not working.
^\?bloggerURL=/p/(.*)\.html$
For instance, www.domain.com/?bloggerURL=/p/about.html should be redirected to www.domain.com/about, but the URL is remaining as www.domain.com/?bloggerURL=/p/about.html
I'm probably missing something simple to get it to pick up the first part of the string and remove it. Is there something I need to add/remove to get that case working?

Just going through old, unanswered questions... hopefully you've solved this one, but in case not, the only problem I see with the regex is the non-escaped slashes. I think you would need to use:
^\?bloggerURL=\/p\/(.*)\.html$
Hope this might help someone else.

Related

In Chrome, eventually using redirect extension with regex, how to redirect all facebook.com URLs to mbasic.facebook.com URLS

Have been searching for days for this, seems like none has had the same idea as mine regarding redirecting Facebook urls from the normal website to the mbasic version site.
My idea is to redirect all "normal" facebook.com urls into mbasic.facebook.com ones at ALL times. Both when i click links on the web, and when i enter an url in the adress bar. Preferably matching facebook.com, www.facebook.com, http(s)://facebook.com, http(s)://www.facebook.com
-- essentially, let them all have a "mbasic." before "facebook" and never see the ordinary facebook site again, but only use the basic version.
.
I've found some redirector extensions in Chrome that uses regex (right now using Requestly), and I think i'm close, but this regex seems invalid:
^((http(s)?:\/\/)?)((www.)?)facebook.com(\/)?.*$
This is what it matches for me at RegExr: https://i.imgur.com/eljJ1vP.png .
Also tried this one, but is also invalid apparently:
^(http://)*(www.)*((?!mbasic).)*.$
Other times using either regex or wildcard matching, I could get a query for facebook.com to change to mbasic.facebook.com, but whenever I entered, say, an event page (facebook.com/events/ID), it would not redirect, or, the "mbasic" part would be repeated, resulting in a "mbasic.mbasic.facebook.com" redirect.
Also looked at userscripts and changing hostfiles, but I can't seem to find a solution just yet. Hope you can help a bit. Please ask for more information if needed! Thanks in advance.
Since you have already mentioned you are using Requestly, let me try to put up a simple solution using Requestly Redirect Rule and Wildcard operator.
Try Redirect Rule:
Source - Url Matches (WildCard) http*://*facebook.com*
Destination - http$1://mbasic.facebook.com$3
The explanation is very simple
First Match ($1) - 's' if https protocol or '' if http protocol
Second Match ($2) - Subdomain of facebook e.g. www. or any other
Third Match ($3) - Anything after facebook.com
We are interested in changing $2 value with mbasic and keep $1, $3 as it is and this is what is done in the Destination field.
Here is a screenshot for reference -
I tried it and it looked like working fine.
Disclaimer - I work at Requestly. For further questions feel free to reach out to us at contact#requestly.io or tweet at #requestlyIO

How to use regex to redirect all pages of one site to another (using Redirection WP plugin)?

I need to redirect all pages of site /old-domain/ to /new-domain/ and am having difficulty figuring out simple regex using the redirection plugin for Wordpress. Googling hasn't helped, or I'm missing something basic.
For instance, I would like to redirect
/old-domain/photos/cats TO: /new-domain/photos/cats
and /old-domain/products/shoes TO: /new-domain/products/shoes
I though I should enter:
old-domain/(.*) > new-domain/$1
But that had no effect.
Thanks in advance for any help.
Thanks everyone for the help. You were absolutely correct about the syntax. Also, you can't put the original domain into the source box.
Here is the answer and syntax in case someone else finds this (I had a very difficult time googling)
To redirect olddomain.com to newdomain.com:
/(.*)
http://newdomain.com/$1/
<make sure the regex button is checked>
Based upon the examples on https://redirection.me/, it looks like you need a slash at the start to match correctly.
Try:
/old-domain/(.*) .... /new-domain/($1)

IIS URL rewrite not working properly 404 errors

I am upgrading a joomla website setup on IIS 10. Now I have oldsite.com and a newsite.com. My new site has slightly different folder structure but page names and content is same. Rightly so client doesn't want to lose SEO ranking on the old pages and want to redirect them to correct one on the new upgraded site.
i need to do following
is wildcard and will get replaced with whatever will be typed in the URL in it’s place
/div-services/* will redirect to /div/*
/div-questions/* will redirect to /div/questions/*
/fm-lw-services/* will redirect to /fm-lw/*
/locations/* will redirect to /contact/*
/resources/blog/* will redirect to /blog/*
/contact-us/* will redirect to /contact/*
I initially setup my pattern to
(.*)(div-services)(.*) becomes {R:1}( div){R:1}
It worked well till I have matching phrase to repeat in some form in the url. which in this case is “div-services” coming again in the URL, it gets replaced as well.
For example if the url is newsite.com/div-services/xyz/abc-div-services then per the rule it will replace both occurrences of “div-services” which is not desired I only need to replace the first occurrence. I though it’s a easy fix and made my pattern as following
(.*)(/div-services/)(.*) replace to {R:1}(/div/){R:1}
Even though in the test pattern it validates with success but it just doesn’t work and does not re write the URL I even tried with the escape character
(.*)(\/div-services\/)(.*) becomes {R:1}(/div/){R:1}
Still no luck. After digging and digging I found following example
div-services/(.*)$ becomes div/{R:1}
this worked generally well but now if I don’t have the ending forward slash it won’t work
for example if URL is is newsite.com/div-services it won’t work but is newsite.com/div-services/ and is newsite.com/div-services/xyx will work fine.
I am just at loss, any help will be much appreciated. I just don’t understand why can’t I detect the forward slash /
fyi I figured why this was not working (.*)(/div-services/)(.*) becomes {R:1}(/div/){R:1}
it is because input start the after the first forward slash, i was assuming that it would be the whole URL that is why my regular expression validates but actually doesn't work. As when it run it is only taking URL after the first slash, that clarifies so many things and logical explanation on why many of my patterns were not working even though they would pass the pattern test. Hopefully it save others the hours and hours i wasted because i didn't have clear understand how it was working
enter image description here

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.

Regex to change old url to new with wordpress redirection

I want to redirect for example
www.mydomain.com/my-profile.html?userId=18681
to
www.mydomain.com/members
what shall i put in my Source URL?
I have more than 2000 404 errors on webmaster because i changed from cms to cms, so i want to fix my redirection regex so not to enter the errors one bye one because I have
/my-profile.html?userId=18681
/my-profile.html?userId=12451
/my-profile.html?userId=9251
How can i make it general so it automatic redirects all to www.mydomain.com/members
I use this plugin http://wordpress.org/plugins/redirection/
I'm not sure how you're going about implementing the redirect. But from a purely regex standpoint, If I wanted to convert the top url format to the one you put below it, here is the find-and-replace format I would use:
s/(my-.+\d+)$/members/
So find 'my-', then one or more of any character, then ENDING with one or more digits. Replace that (starting with my- and ending with the digits) with 'members'.
Sorry if this does not solve your issue, and keep in mind this is 'perl compatible' format for regex, find-and-replace may (likely) be a formatted differently for the language you are implementing this with.