Common rewrite rules

The easiest way to redirect one site to another is by adding the following to the Virtualhost:

Redirect 301 / http://newsite.com/

Note: When AllowOverride is set to allow the use of .htaccess files, Apache will look in every directory for .htaccess files. Permitting .htaccess files causes a performance hit, whether or not you actually even use them! The .htaccess file is loaded every time a document is requested.

Redirect https traffic to http

By redirecting everything that comes in on port 443:

RewriteEngine On
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{HTTP_HOST} ^(www.)mydomain\.com$ [NC,OR]
RewriteRule ^/(.*)http://www\.mydomain\.com/$1 [R,L]

or by checking the x-forwarded-proto header:

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://www.domain.com$1 [R=301,L]

Redirect http traffic to https

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Redirect non-www to www:
(If you see an Internal Server error, check the logs for APC errors)

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]

or

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

Redirect www to non-www:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]

Redirect to maintenance page that displays image .jpg

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REQUEST_URI} !/image.jpg$
RewriteRule $ /maintenance.html [R=302,L]

 

Redirect for purpose of changing displayed URL
RewriteCond %{SERVER_NAME}  ^fubar.com$
RewriteRule ^/$ http://fubar.net [L]

#To make all letters in the url lowercase
RewriteEngine on
RewriteBase /
RewriteMap insensitive tolower:
RewriteRule ^[\/]*(.*)$ /${insensitive:$1} [R,L]

 

#For phpMyAdmin
RewriteCond %{HTTPS} off$
RewriteRule ^/phpmyadmin https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]$

RedirectMatch 301 ^(/phpmyadmin[^/]*/.*)$ https://hostname$1

About vicki

Welcome to the Sovereign Republic of Vickistan. I am the President here. Lucky me! No taxes or laws yet. Lucky you!
This entry was posted in Linux. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *