htaccess - markhowellsmead/helpers GitHub Wiki

WordPress Multisite

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

</IfModule>

Redirect by language

Meta Refresh

<meta http-equiv="refresh" content="1;url=https://www.OTHERWEBSITE.ch/" />

Redirect all

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ https://www.NEWDOMAIN.ch/ [R=301,L]

Force SSL

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Force www

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.org$ [NC]
RewriteRule ^(.*)$ https://www.example.org/$1 [L,R=301]

Force without www

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

Custom expiry length

@ 13.6.2019 - SH

### marker BROWSER CACHE start ###
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType image/vnd.microsoft.icon "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"

ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"

ExpiresByType video/ogg "access plus 1 year"
ExpiresByType audio/ogg "access plus 1 year"
ExpiresByType video/mp4 "access plus 1 year"
ExpiresByType video/webm "access plus 1 year"

ExpiresByType text/css "access plus 1 year"
ExpiresByType text/javascript "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType application/x-javascript "access plus 1 year"

ExpiresByType application/x-font-ttf "access plus 1 year"
ExpiresByType application/x-font-woff "access plus 1 year"
ExpiresByType application/font-woff "access plus 1 year"
ExpiresByType application/font-woff2 "access plus 1 year"
ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
ExpiresByType font/ttf "access plus 1 year"
ExpiresByType font/woff "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
</IfModule>
### marker BROWSER CACHE end ###

Google Analytics campaign

Redirect a custom subdomain to a URL containing Google Analytics tracking parameters.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^campaign.example.org$
RewriteRule (.*) https://www.example.org/?utm_source=mycampaign&utm_medium=socialmedia&utm_campaign=mycampaign&utm_content=mycampaign [L,NC,R=301]

Password protection

Apache reference / .htpasswd Generator

AuthName "Password Protected Area"
AuthType Basic
AuthUserFile $WEBROOT/.htpasswd
AuthGroupFile /dev/null
require valid-user
Order allow,deny
Allow from 1.2.3.4
satisfy any

With different rules per domain

The following snippet requires a valid user for the subdomain test.domain.ch but will allow anyone from the IP address 1.2.3.4 to access the subdomain without logging in. This is useful if multiple domains or subdomains point at the same folder, e.g. in WordPress Multisite.

SetEnvIfNoCase Host test\.domain\.ch$ require_auth=true
SetEnvIf Remote_Addr 1\.2\.3\.4 AllowMeIn

AuthName "Password Protected Area"
AuthType Basic
AuthUserFile $WEBROOT/.htpasswd
AuthGroupFile /dev/null
Order allow,deny
Allow from all
Allow from env=AllowMeIn
Deny from env=require_auth
Require valid-user
Satisfy any

Redirect after a certain time

Caution, whitespace on the RewriteCond line is critical. (No space after the >.)

RewriteEngine On

# Check the current domain
RewriteCond %{HTTP_HOST} ^naming\.example\.com$

# Check the current date and time in a concise format
RewriteCond %{TIME} >202407141415

# Redirect to the new URL preserving the language part of the URL
RewriteRule ^(de|en|it|fr)/?$ https://www.example.com/$1/naming-system [R=302,L]
⚠️ **GitHub.com Fallback** ⚠️