Skip to content

Instantly share code, notes, and snippets.

@facelordgists
Last active December 16, 2015 06:48
Show Gist options
  • Save facelordgists/5393660 to your computer and use it in GitHub Desktop.
Save facelordgists/5393660 to your computer and use it in GitHub Desktop.
APACHE: htaccess snippets
# This is also a useful list:
# https://gist.github.com/ScottPhillips/1721489
# ======================================================================
# Wordpress ReWrites
# ======================================================================
#wp/index.php/category/the-topic > blog/category/the-topic
#category rewrite
RewriteRule ^wp/index.php/category/(.*)$ blog/category/$1 [R=301,NC]
#archive by date permalink fix
RewriteRule ^wp/index.php/([0-9]+)/([0-9]+)/$ blog/$1/$2/ [R=301,NC]
# date-based permalink structure rewrite
RewriteRule ^wp/index.php/([0-9]+)/([0-9]+)/(.*)$ blog/$3 [R=301,L,NC]
#Catch all for remaining blog urls
RewriteRule ^wp/index.php/(.*)$ blog/$1 [R=301,L,NC]
RewriteRule ^wp/(.*)$ blog/$1 [R=301,L,NC]
# Rewrite a date based structure to a flat post structure
RewriteRule ^/blog/\d{4}/\d{2}/\d{2}/([^\d]+)(?=-\d) /blog/$1 [R=301,L,NC]
# Example URL:
# http://sugarfix.wpengine.com/blog/2015/10/05/the-green-lady-chicago-solving-163379
# rewrites to
# /blog/the-green-lady-chicago-solving
# ======================================================================
# Redirect entire website
# ======================================================================
Options +FollowSymLinks
RewriteEngine on
#exclude google webmaster tools verification file from redirect
#This allows google to verify the site in google webmaster tools
RewriteCond %{REQUEST_URI} !^/googleeb9600f3c2176395.html
#redirect all requests to...
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
# ======================================================================
# Redirect image requests from month based blog/wp-content subfolder to wp-content
# ======================================================================
RewriteRule ^blog/wp-content/uploads/([0-9]+)/([0-9]+)/(.*)\.jpg$ wp-content/uploads/$1/$2/$3\.jpg [NC]
RewriteRule ^blog/wp-content/uploads/([0-9]+)/([0-9]+)/(.*)\.png$ wp-content/uploads/$1/$2/$3\.png [NC]
# ======================================================================
# Redirect /blog/year/month/post-name to /blog/post-name
# ======================================================================
RewriteRule ^blog/([0-9]+)/([0-9]+)/(.*)$ blog/$3 [R=301,NC,L]
# ======================================================================
# Force no SSL
# ======================================================================
RewriteCond %{SERVER_PORT} ^443$
RewriteRule (.*) http://www.golddustdental.com/$1 [R=301,L]
# ======================================================================
# rewrite urls in a folder to another folder (case insensitive)
# ======================================================================
# Rewrite
RewriteRule ^pics/(.*) photos/$1 [NC]
# Redirect
RewriteRule ^pics/(.*) photos/$1 [R=301,L]
# ======================================================================
# Query Strings
# ======================================================================
#Redirect page id 4 to a simple URL
RewriteCond %{QUERY_STRING} ^p=4$
RewriteRule ^(.*)$ /blog/about-dr-burr-bakke/? [L,R=301]
======================================================================
Redirect image requests from month based blog/wp-content subfolder to wp-content
======================================================================
RewriteRule ^blog/wp-content/uploads/([0-9]+)/([0-9]+)/(.*)\.jpg$ wp-content/uploads/$1/$2/$3\.jpg [NC]
RewriteRule ^blog/wp-content/uploads/([0-9]+)/([0-9]+)/(.*)\.png$ wp-content/uploads/$1/$2/$3\.png [NC]
======================================================================
Redirect /blog/year/month/post-name to /blog/post-name
======================================================================
RewriteRule ^blog/([0-9]+)/([0-9]+)/(.*)$ blog/$3 [R=301,NC,L]
======================================================================
Force no SSL
======================================================================
RewriteCond %{SERVER_PORT} ^443$
RewriteRule (.*) http://www.golddustdental.com/$1 [R=301,L]
======================================================================
rewrite urls in a folder to another folder (case insensitive)
======================================================================
RewriteRule ^pics/(.*) photos/$1 [NC]
======================================================================
Query Strings
======================================================================
#Redirect page id 4 to a simple URL
RewriteCond %{QUERY_STRING} ^p=4$
RewriteRule ^(.*)$ /blog/about-dr-burr-bakke/? [L,R=301]
# category rewrite
# wp/index.php/category/the-topic > blog/category/the-topic
RewriteRule ^wp/index.php/category/(.*)$ blog/category/$1 [R=301,NC]
#archive by date permalink fix
RewriteRule ^wp/index.php/([0-9]+)/([0-9]+)/$ blog/$1/$2/ [R=301,NC]
# date-based permalink structure rewrite
RewriteRule ^wp/index.php/([0-9]+)/([0-9]+)/(.*)$ blog/$3 [R=301,L,NC]
#Catch all for remaining blog urls
RewriteRule ^wp/index.php/(.*)$ blog/$1 [R=301,L,NC]
RewriteRule ^wp/(.*)$ blog/$1 [R=301,L,NC]
#httpd.conf
# Protect the htaccess file
<Files .htaccess>
Order Allow,Deny
Deny from all
</Files>
# Protect the htaccess file
<Files wp-config.php>
Order Allow,Deny
Deny from all
</Files>
<ifModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/css text/javascript application/javascript application/x-javascript
</ifModule>
#.htaccess stuff
Header unset ETag
FileETag None
# BEGIN Expire headers
<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 5 seconds"
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
ExpiresByType text/css "access plus 2592000 seconds"
<filesMatch "(style.css)$">
ExpiresByType text/css "access plus 60 seconds"
</filesMatch>
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>
# END Expire headers
# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
<filesMatch "\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(css)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(js)$">
Header set Cache-Control "private"
</filesMatch>
<filesMatch "\.(x?html?|php)$">
Header set Cache-Control "private, must-revalidate"
</filesMatch>
</ifModule>
# END Cache-Control Headers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment