---
title: "Basic rewrite rule examples"
date: "2025-02-05T23:31:07+00:00"
summary:
image:
type: "article"
url: "/acquia-cloud-platform/help/93136-basic-rewrite-rule-examples"
id: "2e38caeb-7349-4c1e-b3f1-1f12eb1b276b"
---

Table of contents will be added

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](http://en.wikipedia.org/wiki/HTTP_301) 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](https://www.drupal.org/project/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