Convert these rewrite apache rules to nginx - wordpress

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;

Related

Wordpress - How to create redirection in NGINX?

I have a dilemma. I migrated my wordpress site from Apapache server to NGINX.
In the process I changed permalink in WP from
/index.php/%postname%/
to
/%postname%/
Now, users coming to site from Google, are getting 404's because of the permalink change. Typically I would just redirect any page via WP plugin, but because of this index.php in the permalink, plugins don't work. So I have no choice but to create a redirection somewhere in NGINX conf file.
Please advise what to do.
server {
rewrite ^/index.php/(.*)$ /$1 permanent;
...
}
In the server configuration file (the file will be located at /etc/nginx/nginx.conf).
If it does not exist there, it may also be at /usr/local/nginx/conf/nginx.conf or /usr/local/etc/nginx/nginx.conf.
For temporary redirect:
rewrite ^/oldlocation$ http://www.newdomain.com/newlocation redirect;
For permanent redirect:
rewrite ^/oldlocation$ http://www.newdomain.com/newlocation permanent;

nginx rewrite wp default /?page_id=10 to /my-cool-page

I've got a friend whose WP site is using the default /?page_id=xx permalink structure. I have been trying to create a nginx rewrite to convert the links to a post_name scheme.
There are dozens of pages that need to be redirected so I added a file (snippets/rewrite.conf ) to handle the rewrites and included it in my nginx.conf file:
When I have the permalinks set to post_name, this works (indicating that rewrite is enabled):
rewrite https://example.com/features https://example.com/my-cool-page permanent;
I've tried every which way to make this work for /?page_id=xx permalink structure but it doesn't want to redirect.
When I have the permalinks set to default, this doesn't work:
rewrite ^/?page_id=45 /?page_id=60 permanent;
I've been googling for hours and I just can't find a solution. What is the correct way to form the rewrite.
Ubuntu 16.04
Nginx 1.10.0
MariaDB 10.1.14
PHP 7.0.4

Redirect Wordpress Permalink in htaccess

I want to change the permalink structure of my site from /%postname%/ to /%category%/%post_id%/%postname%/ , and have the old backlinks redirect to the new structure (currently getting 404's). I have tried a couple of different redirection plugins (Redirection, 301 Simple Plugins) and I can't get them to work.
Can someone show me the rule to put in the htaccess file instead?
Thanks!
If you know the explicit category and post_id for a given postname, then you can create the redirects individually:
Redirect 301 /example-post-name-to-redirect/ /example-category/1234/example-post-name-to-redirect/
or using mod_rewrite (note, these rules must be before your wordpress rules:
RewriteRule ^example-post-name-to-redirect/$ /example-category/1234/example-post-name-to-redirect/ [L,R=301]
But the better solution is just using wordpress to do this for you, have you tried: UrbanGiraffe Redirection plugin, Scott Yang's Permalink Redirect WordPress Plugin, or Yoast's Permalink Redirect WordPress Plugin?
On the Redirection Wordpress Plugin you must add your old structure to Site->Permalink Migrations, something like this:
/%year%/%monthnum%/%postname%/
On the .htaccess file you must use regex like this one:
RedirectMatch 301 /^/\d{4}/\d{2}/(.*) https://yourdomain.com/$1/

301 htaccess redirection from old permalinks to new permalinks

I recently transferred my Blogger blog to WordPress, and I want to redirect old Blogger permalinks to the new site.
Old:
http://sitename.com/2012/04/my-post-url.html
To new:
http://sitename.com/my-post-url/
I've tried following .htaccess code. It removes years and months, but doesn't remove .html. Can anyone tell me how to get / instead of .html at the end of the link?
Any modification for the following code?
RedirectMatch 301 /\d{4}/\d{2}/\d{2}/(.*) http://mydomain.com/$1
RedirectMatch 301 /\d{4}/\d{2}/\d{2}/(.*)\.html http://mydomain.com/$1
That should do the trick. :)

Changing Permalinks

I want to change my permalinks from /%year%/%monthnum%/%day%/%postname%/ to /%postname%/
but when I added the following to the .htaccess file, posts didn't redirect the way I thought they would:
RedirectMatch 301 /dddd/dd/dd/(.*) /$1
What do I need to put into my .htaccess file to make it work?
My site is http://SweatingTheBigStuff.com
I think everyone is missing the point here - I think #Daniel has changed his permalinks, and now wants to redirect old permalinks.
The problem is your RedirectMatch regex is only matching a literal 'd', not digits.
Personally I would use this instead;
RedirectMatch 301 ^/[0-9]{4}/[0-9]{2}/[0-9]{2}/(.+)$ /$1
However, #markratledge is right in saying that there are issues with using just the postname.
You don't go into any code to change permalinks; that's changing WP core files, you'll break things and you'll loose your changes on an upgrade. It's much easier than that: go to Wordpress/Dashboard/Setings/Permalinks. If your .htaccess isn't writable, you'll get a warning.
And, using only the postname in permalinks is not recommended for performance reasons: Using only Postname in Permalinks « WordPress Codex
A relevant answer to TheDeadMedic,
You could use a plugin called Redirection to redirected your old permalinks to new permalinks.
But if you need to change your permalinks from the old version to a new version, then follow markratledge's advice

Resources