Stripping "blog" from wordpress URLs with Regex - regex

I've managed to get myself totally mired in the world of regex and could do with a hand out.
I've recently moved my wordpress install from just the /blog subdomain to the whole site so I need to strip /blog/ from all incoming urls except an exact match as /blog is still the blog.
For example I need:
http://foo.com/blog/bar
http://foo.com/blog/foobar/bar
foo.com/blog/bar
to all lose the /blog/ but I need
http://foo.com/blog
foo.com/blog
to keep theirs.
I'm using the wordpress Redirections plugin to manage this as it tracks 404 errors which put me on to this.
Can anyone help!?

You need to replace \/blog\/(.+) with /$1
Only replacing /blog/ with / would also replace http://foo.com/blog/
Update:
That works perfectly except that my previous previous post structure
was /y/m/d/postname. How would I strip that as well when it's present
but not affect the other redirect when it isn't?
In this case you could use \/blog\/(\d{4}\/\d{2}\/\d{2}\/(.+)).
Result:
url: http://foo.com/blog/2014/03/09/bar
$1: 2014/03/09/bar
$2: bar
Update 2:
In case you wanted to have both ways stripped use \/blog\/(?:\d{4}\/\d{2}\/\d{2}\/)?(.+)
Result:
url: http://foo.com/blog/bar
url: http://foo.com/blog/2014/03/09/bar
$1: bar
url: http://foo.com/blog/tag/sometag
$1: tag/sometag

Why not try simple replace()?. Unless I misunderstood the question, isn't the output shown the same as what you need?
public static void main(String[] args) {
String s = "http://foo.com/blog/bar";
String s1 = "http://foo.com/blog";
System.out.println(s.replace("/blog/", "/"));
System.out.println(s1.replace("/blog/", "/"));
}
O/P :
http://foo.com/bar
http://foo.com/blog

Related

How to redirect using htaccess and regex the middle part of URLs leaving the dynamic end as is?

I need to redirect a lot of URLs by changing just some part that is in the middle or at the end of the URL. If that part is in the middle, the end must stay the same.
I need to accomplish this by using htaccess and regex.
I found a lot of examples online, but most of the are one on one redirection, and not covering enough dynamic websites. Also I haven't found answer to this in other questions here.
I have URL:
www.domain.com/something (alone) OR URLS like
www.domain.com/something-aaa-bbb-ccc OR only
www.domain.com/something-aaa
How to make it:
www.domain.com/somethingELSE but ALSO:
www.domain.com/somethingELSE-aaa-bbb-ccc
(KEEP aaa-bbb or whatever goes at the end the same)
To make this more human readable, let's say these are shops with a lot of locations:
www.domain.com/my-shop - Without anything else after OR:
www.domain.com/my-shop-belgrade ... or
www.domain.com/my-shop-new-york ... or ... shop-wherever-wherever-wherever
TO BE:
www.domain.com/my-office - Without anything else after OR:
www.domain.com/my-office-belgrade ... or
www.domain.com/my-office-new-york ... or ... my-office-wherever-wherever-wherever
The following was my urgent solution, but it's not dynamic, and it only covers 2 examples, but I need solution for the dynamic website, the solution that covers all the above.
<IfModule mod_rewrite.c>
RewriteEngine On
Redirect 301 /my-shop /my-office
Redirect 301 /my-shop-new-york /my-office-new-york
</IfModule>
This is the solution that worked out for me.
In order to replace the leading /my-shop with /my-office and leave everything afterwards as is, I used a capturing group:
(.*)
and replaced it with $1:
RedirectMatch 301 /my-shop(.*) /my-office$1

Regex in rewerite rule in .htaccess

I'm trying to redirect some urls of my old website after my migration , urls that need to be redirected are:
first block:
/fr/YYYY/mm/dd/
/fr/YYYY/mm/
/fr/YYYY
target:
blog/
Second block:
/fr/tag/tag1
/fr/tag/tag4
/fr/tag/tag3
/fr/tag/tag2
target:
blog/targetpage
Third block
fr/categorie/categoryName/
target
blog/categoryName
For the first block I tried the following:
RewriteRule ^fr/d{4}/(.*) /blog [R=301]
It does only work for pattern like: fr/2015 but not fr/2015/12/02
I tried another regex https://regex101.com/r/4oZpxX/1 but I do not how to write it htaccess style.
Could please help me with this, thanks in advance.
For your first block, \/fr\/(\d{4})\/?(\d{2})?\/?(\d{2})? will recognize all of those date formats correctly.
\d{#} recognizes a string of # digits
? after \/ and \d{#} makes them optional
For the second and third block I need more information about what kind of strings the "tags" and "categories" are.

In Nginx, how to match and entire URL and Query String and redirect to a URL and Query String

I have been searching for a while now, trying work arounds and haven't come up with anything useful.
I have a (large) list of URL's from an site migration and need to match the entire URL + Query String and redirect to another URL.
As far as I can see the following only matches /mens, but not the rest of the query string.
rewrite "^/mens?brand%5B%5D=27&section%5B%5D=5&price-min=0&price-max=2000&sort=newest" "/t/gender/men" permanent;
The reason it's important is that I have a bunch of similar URL's with slightly different Query Strings, which need to be redirected, similar to below, but actually work.... :-/
rewrite "^/mens/shop?q=road+map+polo" "/t/category/golf-knits" permanent;
rewrite "^/mens/shop?q=six+pocket+pant" "/t/category/golf-pants" permanent;
#etc... ad noiseam
Thanks in advance,
Paul.
The $request_uri variable contains the entire URL. You could use a map to translate it into a redirection.
map $request_uri $target {
~*^/mens/shop\?q=road\+map\+polo /t/category/golf-knits;
~*^/mens/shop\?q=six\+pocket\+pant /t/category/golf-pants;
}
server {
...
if ($target) { return 301 $target; }
...
}
See this document for details.

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]

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