How to redirect old permalinks with regex? - wordpress

I am trying to solve an issue with link rot that is a result from a permalink change a year ago on a high traffic site. I know you can redirect certain permalink structures in apache but how is this done in regex?
I need to move all link request that are in this format:
http://www.*****.com/permalinks/year/month/day/title
to this current (as of 2015) format:
http: //www.*****.com/year/month/day/title
Basically I just need to remove the name permalinks from the url request to fetch it from the database.
I tried forcing a search on anything with a 404 but it has not been reliable, along with it being very slow since the database is large.
I tried this in regex but it does not work:
/permalinks/(d*)/(d*)/(d*)/(.*) => /(d*)/(d*)/(d*)$4

You need simple regex rewrite for remove permalinks/ and keep rest of url:
server {
listen 80;
server_name example.com;
rewrite ^/permalinks/(.*) http://example.com/$1 permanent;
}

Thanks for the response. I will look into that code. It appears to be for nginx.conf? I do not have access to that at the moment so I can't try it.
I went with this regular expression through the urban giraffe redirection plugin and it works:
/permalinks/(.*) ==> /$1

Related

nginx how to redirect all pagination

I need help with my nginx configuration.
The goal is to have all requests to my site like site/page1 site/smth/page1 be redirected to just site/ and site/smth/ basically for all requests ending like page[number]
I tried some examples that I found like rewrite ^/page/(.*)$ /$1; still wasn't able to get the redirection. Maybe I misplaced it, not quite sure where I should put the sting. Tried location and server blocks.
The nginx documentation examples for redirecting were a bit too hard to understand for me, so a little explanation would be great.
If you need a 301 HTTP redirection, try this rewrite rule (before the first location block):
rewrite ^(.*/)page\d+$ $1 permanent;
You can try something like this (not tested)
location ~ ^/(.+)/page[0-9]+$ {
rewrite ^/(.+)/page[0-9]+$ /$1 last;
}

Redirect AMP files on nginx

I'm trying to make redirect on nginx but unfortunately it doesn't work.
What I'd like to achieve, it is to redirect amp files on mobile.
What I'd like to do :
from
https://www.example.com/uri-759.html
to
https://www.example.com/uri-759-amp.html
What I did as redirect
if ($mobile_redirect = perform) {
redirect ^(.*)(\.html)$ $1-amp$2 permanent;
}
what I obtain
https://www.example.com/uri-759-amp-amp-amp-amp-amp-amp-amp-amp.html
Does someone have a solution to perform this redirection ?
You can use a negative lookbehind assertion to avoid matching the rewritten URI.
For example:
rewrite ^(.*)(?<!-amp)(\.html)$ $1-amp$2 permanent;
See this document for more.

nginx: Redirect special characters like "%23" in URL to "#"

My nginx site has a few bad links pointing to it like this:
/some-page%23some-part
/some-page">
This is causing 404's and Google Webmaster Tools is complaining too.
The URL /some-page#some-part does get processed properly and works.
How can I get nginx to redirect the %23 in a URL to #? What about the "> junk?
The links out there cannot be changed, so I'm looking to 301 redirect them myself.
Thanks!
Edit: thanks to Deadooshka for the help. My working solution, rewrite ^(.*)\#(.*)$ /$1#$2 redirect;, is discussed within his answer's comment thread.
not tested. I'm not sure which symbols get the pattern.
rewrite ^/([^\#]+)\#([^\#]+)$ /$1#$2 redirect;
rewrite '^/([^\&]+)\&quot\;\&gt\;$' /$1 redirect;

NGINX: How do I redirect all of the old posts from a /blog url to a /blogs/ url?

The situation is this:
I moved my site a while ago, and I changed http://www.getridofthings.com/blog to http://www.getridofthings.com/blogs/ to include more authors with their own blogs. The problem now, is that Google Webmaster Tools is telling me I have a bunch of 404s. We deleted the old blog posts because they were poor quality, but I want to collect all of the incoming links from those various blog posts and direct them simply to the /blogs/ url.
How do I write this rule in NGINX? I've tried many methods, and I keep getting "too many redirects" or "redirect loop" errors.
This should do the trick.
server {
listen: 127.0.0.1;
server_name: example.com;
rewrite ^/blog/(.*)$ /blogs/$1 redirect;
}
It works by saying that it wants /blog/ to be at the beginning of the url. If there's anything after the /, it will be stored in $1 which is appended to /blogs/. There are no redirect loops because /blogs/ cannot match those conditions.
Redirects that will happen:
/blog/ to /blogs/
/blog/blog-article-1/ to /blogs/blog-article-1/

Convert these rewrite apache rules to nginx

In my Wordpress, I changed the posts permalinks structure. In order to don't get 404 erros for the old links, I would like to redirect the old permalinks to the new permalinks. According to this, the following code must be added in my .htacess so that I can get the redirect working from old posts links to the new posts link:
RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([^/]+).html$ http://myurl.com/$3
That's good, however, I don't use Apache – I use nginx. How can I convert this rules to nginx? I've already tried a apache to nginx online converter, with no success.
Thanks!
I believe this is what you want. "permanent" is 301 according to this page.
rewrite "^/([0-9]{4})/([0-9]{2})/([^/]+).html$" http://myurl.com/$3 permanent;

Resources