htaccess
Redirects
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 / http://www.domain.com
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} !^www.domain.com RewriteRule (.*) http://www.domain.com$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} ^example.com RewriteRule (.*) http://www.example.com/$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 ^(.*)$ https://nx.beandog.org/$1
Access
Allow access from subnet only
Order Deny,Allow Deny from all Allow from 192.168.1.0/24
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 192.168.12.0/24 AuthType Basic AuthName "Development Sites" AuthUserFile /var/www/dev-access.htpasswd Require valid-user Satisfy any </DirectoryMatch>