Skip to content

Instantly share code, notes, and snippets.

@martijngastkemper
Last active October 3, 2016 09:29
Show Gist options
  • Save martijngastkemper/7445663 to your computer and use it in GitHub Desktop.
Save martijngastkemper/7445663 to your computer and use it in GitHub Desktop.
Htaccess lines to permanently redirect secondary domains to a primary domain. For example: www.example.com/foo will redirect to example.nl/foo. And a lot of other examples.
# Only necessary for website not implemented with FifthGear
RewriteEngine On
# Rewrite www.example.nl and example.com to example.nl
RewriteCond %{HTTP_HOST} www.example.nl [NC,OR]
RewriteCond %{HTTP_HOST} example.com [NC]
RewriteRule ^(.*)$ http://example.nl/$1 [R=301,L]
# Rewrite example.nl to www.example.nl
RewriteCond %{HTTP_HOST} ^example.nl [NC]
RewriteRule ^(.*)$ http://www.example.nl/$1 [R=301,L]
# Enforce HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.nl/$1 [R=301,L]
# Exclude url from redirecting to HTTPS
RewriteCond %{HTTPS} off # Default operator is AND, so don't add [OR]
RewriteCond %{HTTP_HOST} !no-https.example.nl [NC]
RewriteRule ^(.*)$ https://example.nl/$1 [R=301,L]
# Make a file available for domain validation.
RewriteRule ^file-name.txt$ - [NC,L]
# Block all traffic to a domain
ErrorDocument 404 "This text will show up in the browser."
RewriteCond %{HTTP_HOST} example.com [NC]
RewriteRule ^(.*)$ - [R=404,L]
# Use Basic Auth to simply secure a page
<Files "secured-page">
AuthName "This is a secured page"
AuthUserFile /home/accountSystemName/app/website/.htpasswd # Search for a htpasswd generator for more information.
AuthGroupFile /dev/null
AuthType Basic
require valid-user
</Files>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment