WordPress: Create 301 Redirects

Speaks many languages, but currently only uses PHP, JavaScript and WordPress (I consider it its own language, since it's a big sandbox).
Search for a command to run...

Speaks many languages, but currently only uses PHP, JavaScript and WordPress (I consider it its own language, since it's a big sandbox).
No comments yet. Be the first to comment.
Recently, a friend asked me to send a script to remove pre tags from a string, including content. He uses a script to calculate the reading time for the content, but doesn't want the pre-tags to be included in the scoring. So if you ever have a simil...

When there are problems with one's WordPress website, it happens that one has to create a user without having access to the database or the WordPress admin. Then it is useful to be able to create or log in a user using FTP. So here you will learn how...

Occasionally it happens that overlaps occur in a large table and the message Duplicate entry '0' for key 'PRIMARY' for query then gets the upper hand in the error logs. This error can be fixed quite easily with a short line of SQL. Fix duplicate entr...

WordPress is packed with useful features. It is easy to lose the overview. Here you will find a selection of functions that deal with arrays and objects. A small collection of useful functions that you can always use, if you know that they exist. wp_...

As a rule, searches are created with the MySQL LIKE. This usually works very well, as long as the database table is not too large. However, if you work with a large database (millions+ entries), then you quickly notice how long a search in it actuall...

WordPress itself does not offer the possibility to create redirects with the WordPress Admin, therefore you have to either use a plugin or create all your redirects with .htaccess. This post is about exactly these two points. First you will learn how to create a redirect with the .htaccess and then a useful plugin which will take the work out of it.
If you have access to your FTP server, you can create redirects manually using your .htaccess. To do this you need to add the following things to your .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RedirectMatch 301 ^/new.php$ https://domain.com/old/ # target is a .php file
RedirectMatch 301 ^/new-2/$ https://domain.de/old-2/ # target is a path
</IfModule>
In the example we redirect once from https.//domain.com/alt/ to /new.php. We also redirect from https://domain.de/old-2/ to /new-2/. You can copy the individual lines completely, paste them right under there and edit them. The changes should take effect immediately after saving.
The easiest way to create your continuations is to use a plugin. Also because you don't need FTP access and you can create your redirect with just a few clicks directly in the WordPress Admin. The plugin which I use for redirects, also for example to hide partner links:
You can find the plugin directly in the WordPress Admin under Plugins and Install. Once installed and activated, you will find all settings under Tools and Redirection and also your links, if you have created them there.
You can create your redirect by clicking on Add new. There you enter the origin and your desired destination. In our example from above the whole thing would look like this:

Here we redirect again from https://domain.de/old/ to /new.php. The destination can be any address, even addresses that do not link to your website. So you can create a group for affiliates with the help of the groups and store your affiliate links there.
In the options you can also activate a log for everything. Once activated, the executed redirects will be logged including IP address, if you want to. The IP address can also be masked so that you can't do anything with it and the GDPR leaves your wallet alone.
You know a better method (especially .htaccess) to create a redirect? Share it with pleasure!