ORC Webhosting Logo

.htaccess redirects and rewrite rules

Introduction

.htaccess files can be used in the root directory of a website (for example, /home/user/public_html/) to redirect visitors from one document to another within your website. .htaccess rules are recursive, which means that rules written in /home/user/public_html/.htaccess apply to /home/user/public_html and /home/user/public_html/test/directory, unless the specific rule is overridden by a rule in an .htaccess file in a directory below it.

There are several types of Redirectswhich can be set up via .htaccess files.

Requirements

This function requires the mod_rewrite module in Apache. This is installed by default on all ORC Web Hosting systems activated.

301 (permanent) forwarding

This is the most common type and usually the one you want to use. The difference between 301 and 302 is small, but notable. 301 (permanent) indicates that the old link URL (redirected URL) is no longer in use. While a 302 redirect indicates that the old URL is temporarily offline and will be available again shortly.

302 (Temporary) forwarding

As mentioned earlier, 302 redirection is a temporary redirection solution. It is also the default redirection rule of Apache. So when you set your method/option, you need to know whether it is a permanent or temporary redirect.

For all documentation/articles we use 301 redirects.

Create the .htaccess file and upload it to the server

Creating the .htaccess file for 301 or 302 redirects is easy. Just open your favorite text/document editor and create a new empty file. Add the appropriate code/content to the document and save the file as "htaccessrules.txt". After checking the exact rules in the document, use your favorite FTP or SFTP client and upload the document to your website's document root (public_html). After you upload the document, simply rename it to .htaccess.

Code examples and redirection scenarios

Redirecting a single page URL (HTML/PHP file) to another page

Redirect 301 /alteseite.html http://www.meinedomain.ch/neueseite.html

Redirecting the URL of a folder/directory (including all subordinate content) to another folder/directory

RedirectMatch 301 ^/altername/ http://www.meinedomain.ch/neuername/

Redirect an entire domain name to another domain name.

RedirectMatch 301 ^(.*)$ http://www.meinedomain.ch

Redirection from .html to .php

RedirectMatch 301 (.*)\.html$ http://www.meinedomain.ch$1.php

Redirect from www to non-www (remove www)

RewriteEngine On
  RewriteCond %{HTTP_HOST} ^www.mydomain\.ch$ [NC]
  RewriteRule ^(.*)$ http://meinedomain.ch/$1 [L,R=301]

Redirect from non-www to www (force www)

 RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www\.mydomain\.ch$ [NC]
  RewriteRule ^(.*)$ http://www.meinedomain.ch/$1 [L,R=301]

redirect http to https (force https)

Here are two different examples of how to do this. We can do this with Redirect in an If block:

Redirect permanent "/" "https://meinedomain.ch"

This can also be done using rewrite rules. It may be better to avoid this complexity, but if rewrite rules are already present in the .htaccess file, it may still be necessary to use rewrite rules:

  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=permanent,L]

The official Apache .htaccess redirect documentation can be found here: Apache Rewrite Guide.

How do I edit the htaccess file via the cPanel file manager?

The .htaccess file contains instructions that tell the server how to behave in certain scenarios and directly affect the way your website works.

Redirects and URL rewrites are two very common instructions that we have already explained above that can be found in an .htaccess file. Many scripts like WordPress, Drupal, Joomla and Magento add instructions to the .htaccess to make these scripts work.

You may need to edit the .htaccess file at some point for various reasons. This video shows how to edit the file.

Log in to your cPanel account and click the File Manager icon in the Files section.

htaccess file and rewrite rules
Edit .htaccess in cPanel

The cPanel file manager opens. Navigate to the directory where your .htaccess file is located. If you want to edit the .htaccess file of your root directory, navigate to the public_html folder as in our example.

Right-click the .htaccess file and then select "edit" from the context menu.

htaccess file and rewrite rules
File in public_html folder

When the small editing window is open, click the Edit button. A new window opens with options that you can edit.

htaccess redirects

Make the changes to your .htaccess file. Then click the Save Changes button.

the changes

You can't see your htaccess?

If the .htaccess file is not displayed, click the Settings button in the File Manager. The Settings button is located on the right side of the top menu. A small window will open. Check the Show hidden files option and click the Save button. The .htaccess file will be displayed. If it does not exist, you need to create a new file.

htaccess file and rewrite rules
Show hidden files in public_html

YOU don't use ORC web hosting for the best web hosting experience? Why not? Ask our team what ORC Web Hosting can do for you to improve your web hosting experience.

ORC Web Hosting offers top-notch technical support around the clock. A dedicated team is ready to help you whenever you need our assistance. Contact us at any time.

Hat Ihnen diese Anleitung geholfen?