Skip to content

Instantly share code, notes, and snippets.

@bitless
Last active August 29, 2015 14:08
Show Gist options
  • Save bitless/39b9e618164d7faa0487 to your computer and use it in GitHub Desktop.
Save bitless/39b9e618164d7faa0487 to your computer and use it in GitHub Desktop.
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
## HOST RULES
# force www on production host
RewriteCond %{HTTP_HOST} !mydev|mystage [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force https on production host
RewriteCond %{HTTP_HOST} !mydev|mystage [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
## FILE RULES
# set R=302 for testing, R=301 when satisfied
# FIRST: redirect file-name.php requests to file-name if file-name.php exists (E)
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# drop trailing shlashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=302]
# VANITIES (V)
# php extensions in targets can be retained for clarity or omitted (E-V-E-F/E-V-F)
# keeps original request URL
RewriteRule ^fancy/vanity$ vanity.php [L,QSA]
RewriteRule ^fancy/vanitwo$ vanit [L,QSA]
# redirects to site URL
RewriteRule ^fancy-vanity/redirected$ /vanity.php [R=301,L,QSA]
RewriteRule ^fancy-vanity/elsoworks$ /vanity [R=301,L,QSA]
# LAST: map request for file-name back to to file-name.php for execution (F)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment