Skip to content

Instantly share code, notes, and snippets.

@jonesch
Created November 1, 2018 14:54
Show Gist options
  • Save jonesch/5b2e68e3eefc08c4fc655e3037f8c99c to your computer and use it in GitHub Desktop.
Save jonesch/5b2e68e3eefc08c4fc655e3037f8c99c to your computer and use it in GitHub Desktop.
htaccess for SEO
# Syntax highlights
# ===========================
# Flags for a Rule, by default R=302, L equals Last line to run & stop the file
[R=301,L]
# This is an Apache setting that is normally turned on by default at the server level
RewriteEngine on
# Single vs Plural
Redirect vs RedirectRule
# 301 Single Redirects
# ===========================
# Redirect a single page:
Redirect 301 /pagename.php http://www.domain.com/pagename.html
Redirect 301 /page-url/ http://www.domain.com/random-new-url/
# Redirect an entire site:
Redirect 301 / http://www.domain.com/
# Redirect an entire site to a subfolder
Redirect 301 / http://www.domain.com/subfolder/
# Redirect a subfolder to another site
Redirect 301 /subfolder http://www.domain.com/
# 301 Plural redirects
# ===========================================
# This will redirect any file with the .html extension to use the same filename but use the .php extension instead.
RedirectMatch 301 (.*)\.html$ http://www.domain.com$1.php
# Redirect from old domain to new domain
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
# Forcing https
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://www.newdomain.com/$1 [R=301,L]
# Forcing http
RewriteCond %{HTTPS} =on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
# Redirect to www location
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Redirect to non-www location
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
# Redirect to www location with subdirectory
RewriteCond %{HTTP_HOST} domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/directory/index.html [R=301,NC]
# Redirect from old domain to new domain with full path and query string:
RewriteRule ^(.*) http://www.newdomain.com%{REQUEST_URI} [R=302,NC]
# Redirect from old domain with subdirectory to new domain w/o subdirectory but include full path and query string:
RewriteCond %{REQUEST_URI} ^/subdirname/(.*)$
RewriteRule ^(.*) http://www.katcode.com/%1 [R=302,NC]
# Rewrite and redirect URLs with query parameters (files placed in root directory)
# Original URL:
# http://www.example.com/index.php?id=1
#
# Desired destination URL:
# http://www.example.com/path-to-new-location/
RewriteCond %{QUERY_STRING} id=1
RewriteRule ^index\.php$ /path-to-new-location/? [R=301,L]
# Redirect URLs with query parameters (files placed in subdirectory)
# Original URL:
# http://www.example.com/sub-dir/index.php?id=1
#
# Desired destination URL:
# http://www.example.com/path-to-new-location/
RewriteCond %{QUERY_STRING} id=1
RewriteRule ^sub-dir/index\.php$ /path-to-new-location/? [R=301,L]
# Redirect one clean URL to a new clean URL
# Original URL:
# http://www.example.com/old-page/
#
# Desired destination URL:
# http://www.example.com/new-page/
RewriteRule ^old-page/?$ $1/new-page$2 [R=301,L]
# Rewrite and redirect URLs with query parameter to directory based structure, retaining query string in URL root level
# Original URL:
# http://www.example.com/index.php?id=100
#
# Desired destination URL:
# http://www.example.com/100/
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [QSA] QSA Definition - (https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa)
# Rewrite URLs with query parameter to directory based structure, retaining query string parameter in URL subdirectory
# Original URL:
# http://www.example.com/index.php?category=fish
#
# Desired destination URL:
# http://www.example.com/category/fish/
RewriteEngine On
RewriteRule ^/?category/([^/d]+)/?$ index.php?category=$1 [L,QSA]
# Domain change – redirect all incoming request from old to new domain (retain path)
RewriteCond %{HTTP_HOST} ^example-old\.com$ [NC]
RewriteRule ^(.*)$ http://www.example-new.com/$1 [R=301,L]
# If you do not want to pass the path in the request to the new domain, change the last row to:
RewriteRule ^(.*)$ http://www.example-new.com/ [R=301,L]
# From blog.oldsite.com -> www.somewhere.com/blog/
# retains path and query, and eliminates xtra blog path if domain is blog.oldsite.com/blog/
RewriteCond %{REQUEST_URI}/ blog
RewriteRule ^(.*) http://www.somewhere.com/%{REQUEST_URI} [R=302,NC]
RewriteRule ^(.*) http://www.somewhere.com/blog/%{REQUEST_URI} [R=302,NC]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment