Notes on Creating Rewrite Rules

Article translated by machine

This text is a machine translation. A revised version is planned.

For Linux Web Hosting packages and Managed Dedicated Server

We show you which error sources you can check if your rewrite rules do not work as desired.

You can rename and redirect pages in a.htaccess file using rewrite rules. For example, rename the URLs of your web pages for search engine optimization (SEO) or redirect the call for different purposes.

Please note: Rewrite rules are defined in a.htaccess file on the server. We explain how to create a.htaccess file in the help article Creating files with a prefixed dot under Windows. Please make sure that the file that you send via WebspaceExplorer or FTP to your web space, which is actually called .htaccess.

mod_rewrite and activation of the RewriteEngine

On the Web Hostingservers, the Apache module mod_rewrite is already active by default. So that your rewrite rules can take effect, switch on the so-called rewrite engine. To do this, enter the following command at the beginning of your.htaccess:

RewriteEngine On

RewriteBase parameter and directory specification

After activating the RewriteEngine, the so-called RewriteBase must also be specified at the beginning of the.htaccess:

RewriteBase /

With the RewriteBase you define from which directory the RewriteRule should access. If the RewriteBase is specified with / as above, the output path is the directory to which the domain to be redirected is linked. You can find out with which directory a domain is currently linked in the 1&1 IONOS in the Domain Center (Domains > Manage Domains).

RewriteBase and RewriteRule in the same directory

If you want the created RewriteRule to take effect in the same directory to which the RewriteBase was defined, do not specify any further directory paths in your RewriteRule. It is sufficient to specify the file with the /.

RewriteEngine On
RewriteBase /
RewriteRule ^([a-z]+)\.html$ /index.php?$1 [R,L] 

RewriteBase and RewriteRule in different directories

If a RewriteRule is to take effect in a directory other than the one specified by the RewriteBase, this must be defined with the corresponding specification of the directory path.

Example RewriteRule, which should work in a different directory than the RewriteBase specifies.

The following RewriteRule specifies that the file index.html should be loaded automatically from the /test directory each time a non-existing HTML page is called.

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ test/index.html 

If the domain had already been connected to the directory /test via the 1&1 IONOS, the specification of the directory /test in the RewriteRule would no longer be necessary, since this is already defined via the specification in the RewriteBase.

Tips for avoiding errors

Please note: Rewrite rules can be very complex and can cause an error of 500 even with the smallest uncleanliness. Such errors can result from the fact that only one character was entered incorrectly. This is not a server failure. As a rule, the website will function without problems as soon as the corresponding lines in the.htaccess have been commented out.

Here we list the most common causes of errors that can ensure that a rewrite rule does not take effect, even if it does not produce an error 500.

  • Make sure that the.htaccess has been placed in the correct directory. Note that an.htaccess is recursive. It therefore affects the directory in which it was saved and its respective subdirectories.

  • Avoid multiple nested.htaccess files on a path level. Errors in a subdirectory's.htaccess can under certain circumstances also negatively affect higher-level rewrite rules at upper directory levels.

  • For this reason, it is best to create a separate directory for each domain or web presence. This allows you to conveniently define separate.htaccess files and rewrite rules for each domain that do not interfere with each other.

  • Have you created a protected directory in the same directory using 1&1 IONOS? This function overwrites self-created.htaccess files and thus makes your rewrite rules ineffective. Our recommendation: Create the directory protection yourself in your.htaccess using the help article Set up directory protection using.htaccess and FTP.

  • Check whether functions are commented out (disabled). Some pre-built scripts and content management systems (CMS), such as Joomla, comment out rewrite rules to avoid problems during installation. In a.htaccess, commands are commented out by prefixing a line with a hash (#):

# Beispiel für Rewrite-Rule:
# (macht aus http://domain.de/xyz.html ein http://domain.de/index.php?xyz
#
RewriteEngine on
RewriteBase /
# RewriteRule ^([a-z]+)\.html$ /index.php?$1 [R,L] 

In this example, the actual RewriteRule was commented out and therefore does not apply. Remove the hash before RewriteRule and save the modified.htaccess on the server.

  • Check whether the required file permissions (chmod) have been assigned to the.htaccess. In the WebspaceExplorer adjust the file rights by right-clicking on the.htaccess and selecting the Change rights item. Enter the value 644 in the Octal notation field. This is sufficient for the functions of a.htaccess.
  • Carefully check the commands for typos and make sure nothing is missing.