With a redirect, for example, an old URL that no longer exists is redirected to a new URL. There are various ways to implement redirects, including adapting the .htaccess configuration file.
When is a 301 redirect even necessary?
Redirects are justified for various reasons and may even be necessary. In particular, if a link is to be established from an old to a new resource. Redirects are also useful when:
- Old URLs, for example after a website relaunch, must be assigned to new destinations, as the Permalinks have changed.
- backlinks should be redirected to pages that no longer exist in order to avoid a loss of link juice (link strength of backlinks on your website).
- 404 error pages should be excluded and navigated to the correct target URL instead.
- The creation of duplicate content should be avoided, for example by synchronously calling up a page with and without "www".
Create redirects via .htaccess
.htaccess requirements
The basic requirement is that you have an .htaccess file. If you have already installed WordPress, you will find the file in your root directory.
If you do not yet have an .htaccess file, you must create one in advance. All you need to do is create an empty text document (e.g. with the editor). The file must then be saved via "Save file" and file type "All files". The file name must be ".htaccess".
Note: If you have an FTP program (e.g. FileZilla), the files beginning with a dot may be hidden. Depending on the software, this must be shown again.
Include .htaccess comments
So that you don't lose track if you make a lot of entries, it is advisable to work with comments in the .htaccess file. These are marked with a hash (#) and introduce a comment line by line.
# This is a comment on the 301 redirection in the .htaccess
Redirect versus RewriteRule instructions
Simply put, a redirect and a RewriteRule instruction differ in their functionality.
A redirect is referred to as forwarding and means that an old URL is redirected to a new URL.
A RewriteRule instruction or rewrite engine is used to rewrite internal requests. The Apache module "mod_rewrite" is required for this. Put simply, this allows URLs to be "manipulated" and changed at will. An example of this would be changing the URL to make it more interesting for search engines such as Google.
# Paramater URL before modification by mod_rewrite:
http://wwww.beispiel.de/index.html?lang=de?page_id=302
# search engine friendly URL after using mod_rewrite:
http://www.beispiel.de/lesbare-angepasste-url
Setup and structure of 301 redirects/redirects using .htaccess
The structure of a 301 redirect is relatively simple and clear:
"Instruction" + "Source URL" + "Destination URL"
# Basic structure:
Redirect permanent OldRelativeURL NewAbsoluteURL
# Using an example:
Redirect permanent /old-website.html https://www.beispiel.de/neue-seite.html
Instruction: In this context, the instruction "Redirect permanent". This means that it is a permanent redirect. The server sends the HTTP status code 301, which informs the browser that the redirect is permanent. The browser will therefore replace the old URL with the new one and use it in future.
Source URL: The original or old URL that is to be redirected is specified here. In your example, it is "/old-website.html". This is the path of the old resource relative to the root directory of the website.
Target URL: This is the URL to which the request is redirected. In the example, it is "https://www.beispiel.de/neue-seite.html". This is the absolute URL of the new resource.
Example of a RewriteRule application
# Forwarding from non-www to www:
RewriteCond %{HTTP_HOST} !^www.beispiel.de$ [NC]
RewriteRule ^(.*)$ http://www.beispiel.de/$1 [L,R=301]
Let's go through everything step by step:
RewriteCond %{HTTP_HOST} !^www.beispiel.de$ [NC]
This line sets a condition (RewriteCond
) for the following rewrite rule. It states that the rule should only be applied if the condition is fulfilled. The condition is defined by the expression!^www.beispiel.de$
which means that the condition is true if the value of%{HTTP_HOST}
i.e. the host name in the HTTP request, does not already correspond to "www.beispiel.de". The[NC]
-flag at the end indicates that the condition check is not case-sensitive.RewriteRule ^(.*)$ http://www.beispiel.de/$1 [L,R=301]
This line is the rewrite rule itself. It is only applied if the previous condition is fulfilled. This rule takes the entire path after the domain (defined by^(.*)$
) and redirects the request to the same URL, but with the prefix "www" (http://www.beispiel.de/$1
). The[L]
-flag indicates that this is the last rule to be applied (no further rules are checked), and the[R=301]
-flag instructs the server to send a 301 redirect, which means a permanent redirect.
In summary, these lines cause all requests where the hostname does not already begin with "www.beispiel.de" to be redirected to the version with the "www" prefix, returning a 301 status code. This helps to ensure the consistency of the website URLs and tells search engines that the redirect is permanent.
Of course, many more applications are possible with RewriteRules. A compendium of applications can be found, for example, in the Apache documentation.
2 Responses
Thanks, I've always used an extra WordPress plugin for this, but it's much easier and better!
Yes, above all it saves you another plugin 🙂