Table of Contents



Status Definition
301 Moved permanently
307 Temporary redirect
Redirect a single file
Redirect 301 /d/file.html /ddd/file.html
Redirect entire site to new domain
Redirect 301 /
Redirect site to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Redirect all non-domain names to the domain
RewriteEngine On
RewriteCond %{HTTP_HOST} !^
RewriteRule (.*)$1 [R=301,L]

Same with not specifying a specific domain – works for virtual hosts:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Redirect non-www prefix for a domain to www
RewriteCond %{HTTP_HOST} ^
RewriteRule (.*)$1 [R=301,L]
Rewrite if REQUEST_URI matches string

You can use Perl regular expression syntax with RewriteCond

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(dmc)?wiki
RewriteRule ^(.*)$$1


Allow access from subnet only
Order Deny,Allow
Deny from all
Allow from
Require either user authentication or subnet access

The DirectoryMatch here restricts access to *-dev sites:

<DirectoryMatch "/var/www/html/(.*\-dev)/">
        Order allow,deny
        Allow from
        AuthType Basic
        AuthName "Development Sites"
        AuthUserFile /var/www/dev-access.htpasswd
        Require valid-user
        Satisfy any