Loading...
Real Time Concepts

Rewrite Rule Reference

Rewrites

Rewrite domain.com to www.domain.com

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

Rewrite www.domain.com to domain.com

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

Rewrite http:// to https://

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

Rewrite www to non www / https://

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R]

Rewrite http:// to https:// (with an SSL terminated load balancer)

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

Note: If using SSL-Termination w/Wordpress, you also need this in wp-config.php:

if ($_SERVER[‘HTTP_X_FORWARDED_PROTO’] == ‘https’)
$_SERVER[‘HTTPS’]=’on’;

Rewrite images to Cloud Files:

<Directory /var/www/vhosts/domain.com/content/images>
RewriteEngine On
RewriteRule ^(.*)$ http://c0000.cdn2.cloudfiles.rackspacecloud.com/images/$1 [R=302,L]
</Directory>

Rewrite One domain to another

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain1.com [NC]
RewriteRule ^(.*)$ http://domain2.com$1 [R=301,L]

Redirect a Path to a New Domain

Redirect 301 /path http://new.domain.com
Redirect 301 /otherpath/somepage.php http://other.domain.com

Rewrite page with query string, and strip query string

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^/pages/pages\.php$
RewriteCond %{QUERY_STRING} ^page=[0-9]*$
RewriteRule ^.*$ http://www.newsite.com/path/? [R=301,L]

Rewrite page with multiple query strings and strip query string

RewriteBase /
RewriteCond %{REQUEST_URI} ^/Page\.php$ [NC]
RewriteCond %{QUERY_STRING} (^|&)var1=val1&var2=val2&var3=val3&var4=val4(&|$) [NC]
RewriteRule ^(.*)$ http://www.domain.com/newpage? [R=301,L,NE]

Redirect old html URL to new dynamic URL

RedirectMatch 301 ^/old_page.html$ http://www.newsite.com/new-page/

Leave a Reply

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