Loading...


Related Products


Date Published: February 5, 2025

Basic rewrite rule examples

This page will provide you with some options to handle common HTTP redirects via the .htaccess used by the Apache webserver.

Creating a basic redirect

If you just want to create a simple 301 redirect from one URL to another, then use the following code:

Note

If you make one of these additions to .htaccess, every resource (PHP, CSS, JS, images, and so on) will be evaluated individually. For larger lists of redirects, performance can become a major issue. Typically, we recommend using the Redirect module.

RewriteRule ^fileone.html$ filetwo.html

With this in place, any request to fileone.html will be immediately redirected to filetwo.html. If you wanted to force one page on your website to a different domain entirely, you could do something like this:

Redirect 301 /relative/link.php http://www.example.com/points/to/here.php

Enforcing clean URLs

Drupal does not necessarily use a clean URL. When you view content, your URL may look something like this:

http://www.mysite.com/?q=node/1

Drupal provides the ability to use a clean URL. When creating a rewrite rule which uses REQUEST_URI as a condition, be sure this rule is placed above the Drupal clean URL rewrite function which appears as follows in Drupal 7's default .htaccess:

# Pass all requests not referring directly to files in the filesystem to
# index.php. Clean URLs are handled in drupal_environment_initialize().
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]

Stripping query strings

Let’s say all the pages on your website other than your home page are formatted as follows, with query strings instead of page names:

http://www.domain.com/home.html?example=12345abcd

Those aren’t very pretty, and on top of that, search engines will show a bunch of duplicated “home” pages. If you want to get rid of the query string in your page URLs, use the following code:

RewriteCond %{QUERY_STRING} example=
RewriteRule (.*) http://www.domain.com/$1? R=301

This not only gets rid of the query string, but also the preceding question mark.

Ignoring symlink paths

For websites in subdirectories or multisites, a symlink may be used to redirect users from example.com/test to the multisite. This can cause problems for pre-existing websites on the installation that may have URLs existing for the /test path, such as foo.com/test.

This example routes the test and bar paths directly to index.php on foo.com and ignores the symlink in place.

RewriteCond %{HTTP_HOST} !^(www.)?example\.com$
RewriteCond %{REQUEST_URI} ^/bar/ [OR]
RewriteCond %{REQUEST_URI} ^/test/
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule (.*) /index.php [L]

# Pass all requests not referring directly to files in the filesystem to
# index.php. Clean URLs are handled in drupal_environment_initialize().
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [OR]
RewriteCond %{REQUEST_URI} !^/bar/ [OR]
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]

Granting access to a single directory

Drupal prevents access to many directories, including the ability to index the directory, through the .htaccess file. You can grant access to a specific directory by creating a separate .htaccess in that directory and adding:

Options +Indexes

Did not find what you were looking for?

If this content did not answer your questions, try searching or contacting our support team for further assistance.

Back to Section navigation