Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

How I learned to stop worrying and love the .htaccess file

2,077 views

Published on

An introduction to .htaccess and what this file can do to help with SEO.
Redirects:
- Mod_alias and mod_rewrite
- Most common redirect types (domain migrations, subdomain to folder and folder renaming and how to deal with duplicate content).
Indexing & Crawling:
- Set HTTP headers for canonicals and meta robots for non-HTML files.
Website speed:
- Gzip and Deflate
- Cache control

Published in: Internet

How I learned to stop worrying and love the .htaccess file

  1. 1. @RoxanaStingu #BrightonSEO How I learned to stop worrying and love the .htaccess file Roxana Stingu // Alamy SLIDESHARE.NET/RoxanaStingu @roxanastingu
  2. 2. @RoxanaStingu #BrightonSEO > .htaccess and SEO Redirects Page speed Crawling and Indexing
  3. 3. @RoxanaStingu #BrightonSEO .htaccess is very powerful – even a missing space can result in server malfunction. DON’T make .htaccess changes without a proper back-up! > Disclaimer
  4. 4. @RoxanaStingu #BrightonSEO THE BASICS
  5. 5. @RoxanaStingu #BrightonSEO > .htaccess speed dating Full name Hyper Text Access Job Affects the folder it’s placed in Orientation Execution order is top to bottom (mostly) Languages Directives
  6. 6. @RoxanaStingu #BrightonSEO > Regular expressions
  7. 7. @RoxanaStingu #BrightonSEO Apache Server Global configuration httpd.conf Resources Documents .htaccess Scripts .htaccess
  8. 8. @RoxanaStingu #BrightonSEO httpd.conf .htaccess
  9. 9. @RoxanaStingu #BrightonSEO > httpd.conf >.htaccess
  10. 10. @RoxanaStingu #BrightonSEO TTFB
  11. 11. @RoxanaStingu #BrightonSEO > Performance 4.6 4.8 5 5.2 5.4 5.6 5.8 6 0 100 200 300 400 500 600 700 800 PageLoadTime(s) TTFB(ms) .htaccess no. of redirects Impact of .htaccess rules on page load time TTFB in milliseconds Page Load Time in seconds Thanks to SEOMike
  12. 12. @RoxanaStingu #BrightonSEO REDIRECTS
  13. 13. @RoxanaStingu #BrightonSEO > Main redirect modules mod_alias mod_rewrite
  14. 14. @RoxanaStingu #BrightonSEO > mod_alias # Redirect [status] [URL-path] URL ### Example Redirect 301 "/old-url.html" "/new-url.html“ # RedirectMatch [status] regex URL ### Example RedirectMatch 301 "(.*).pdf$" "$1.html" .htaccess
  15. 15. @RoxanaStingu #BrightonSEO > mod_rewrite ### Example RewriteEngine on RewriteCond %{HTTP_HOST} ^example.com [NC] RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301] RewriteEngine off .htaccess
  16. 16. @RoxanaStingu #BrightonSEO > Rewrite flags
  17. 17. @RoxanaStingu #BrightonSEO L|last N|next NC|nocase QSA|qsappend QSD|qsdiscard R|redirect > Most common flags
  18. 18. @RoxanaStingu #BrightonSEO > Common redirects
  19. 19. @RoxanaStingu #BrightonSEO > Domain migration ### Domain change – redirect all incoming request from old to new domain (retain path) RewriteEngine on RewriteCond %{HTTP_HOST} ^example-old.com$ [NC] RewriteRule ^(.*)$ https://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 ^(.*)$ https://www.example-new.com/ [R=301,L] .htaccess
  20. 20. @RoxanaStingu #BrightonSEO > Subdomain to folder migration ### From blog.mywebsite.com to www.mywebsite.com/blog/ RewriteEngine on RewriteCond %{HTTP_HOST} ^blog.mywebsite.com RewriteRule ^(.*)$ https://www.mywebsite.com/blog/$1 [L,NC,QSA] .htaccess
  21. 21. @RoxanaStingu #BrightonSEO > Folder redirect ### From https://www.example.com/old-folder/any-page to https://www.example.com/new-folder/any-page RewriteEngine on RewriteRule ^old-folder/(.*)$ /new-folder/$1 [R=301,NC,L] .htaccess
  22. 22. @RoxanaStingu #BrightonSEO > Duplicate content 1. http://example.com/blog 2. http://example.com/blog/ 3. http://www.example.com/blog 4. http://www.example.com/blog/ 5. https://example.com/blog 6. https://example.com/blog/ 7. https://www.example.com/blog 8. https://www.example.com/blog/
  23. 23. @RoxanaStingu #BrightonSEO > Duplicate content ### Turn on rewrite engine RewriteEngine on ### Force WWW RewriteCond %{HTTP_HOST} ^example.com [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC] ### Remove trailing slash RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ http://www.example.com/$1 [L,R=301] ### Force HTTPS RewriteCond %{HTTPS} !on RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] .htaccess
  24. 24. @RoxanaStingu #BrightonSEO > Duplicate content Request http://example.com/folder/ Step 1 http://www.example.com/folder/ Step 2 https://www.example.com/folder/ Step 3 http://www.example.com/folder Step 4 https://www.example.com/folder
  25. 25. @RoxanaStingu #BrightonSEO > Duplicate content
  26. 26. @RoxanaStingu #BrightonSEO > Duplicate content Indexing Google stops after 3 to 5 redirects Crawling wastes crawl budget Speed each step slows down the time it takes for a page to load
  27. 27. @RoxanaStingu #BrightonSEO #### Force HTTPS://WWW and remove trailing / from files #### ## Turn on rewrite engine RewriteEngine on # Force HTTPS and WWW RewriteCond %{HTTP_HOST} !^www.(.*)$ [OR,NC] RewriteCond %{https} off RewriteRule ^(.*)$ https://www.example.com/$1/ [R=301,L] # Remove trailing slash from non-filepath urls RewriteCond %{REQUEST_URI} /(.+)/$ RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^https://www.example.com/%1 [R=301,L] # Include trailing slash on directory RewriteCond %{REQUEST_URI} !(.+)/$ RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^(.+)$ https://www.example.com/$1/ [R=301,L] .htaccess
  28. 28. @RoxanaStingu #BrightonSEO > Duplicate content Request http://example.com/folder Step 1 https://www.example.com/folder/ Request https://www.example.com/folder Step 1 https://www.example.com/folder/
  29. 29. @RoxanaStingu #BrightonSEO CRAWLING & INDEXING
  30. 30. @RoxanaStingu #BrightonSEO > Canonical tags ### Add a canonical tag to a non-HTML resource <Files white-paper.pdf> Header add Link '<https://www.example.com/white-paper-download.html>; rel="canonical"' </Files> .htaccess
  31. 31. @RoxanaStingu #BrightonSEO > Indexing directives ### Add a meta robots tag to a non-HTML resource <Files white-paper.pdf> Header add X-robots-tag "noindex, noarchive, nosnippet" </Files> ### Add meta robots tags to non-HTML resources by type <Files ".(docx|pdf)$"> Header add X-robots-tag "noindex, noarchive, nosnippet" </Files> .htaccess
  32. 32. @RoxanaStingu #BrightonSEO PAGE SPEED
  33. 33. @RoxanaStingu #BrightonSEO > Leverage Browser caching Expires headers Cache control
  34. 34. @RoxanaStingu #BrightonSEO ### Set Expires Headers <FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> Header set Expires "Thu, 15 Jan 2015 20:00:00 GMT" </FilesMatch> # Set the cache-control max-age # 1 year <FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> Header set Cache-Control "max-age=31449600, public" </FilesMatch> # 2 DAYS <FilesMatch ".(xml|txt)$"> Header set Cache-Control "max-age=172800, public, must-revalidate" </FilesMatch> .htaccess
  35. 35. @RoxanaStingu #BrightonSEO experimental non-standard
  36. 36. @RoxanaStingu #BrightonSEO > Compression modules mod_gzip mod_deflate
  37. 37. @RoxanaStingu #BrightonSEO ### Enable gzip compression for resources <ifModule mod_gzip.c> mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .(html?|txt|css|js|php)$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/.* mod_gzip_item_include mime ^application/x-javascript.* mod_gzip_item_exclude mime ^image/.* mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* </ifModule> ### Use DEFLATE to compress resources <FilesMatch *.(html|css|jpg|jpeg|png|gif|js|ico)> SetOutputFilter DEFLATE </FilesMatch> .htaccess
  38. 38. @RoxanaStingu #BrightonSEO TOOLS & RESOURCES
  39. 39. @RoxanaStingu #BrightonSEO > Get to grips with .htaccess danielmorell.com/guides/htaccess-seo .htaccess for SEO
  40. 40. @RoxanaStingu #BrightonSEO > .htaccess generators/testers aleydasolis.com/htaccess-redirects-generator/ danielmorell.com/tools/htaccess/redirect-generator htaccesscheck.com webconfs.com/seo-tools/htaccess-301-redirect-tool/ htaccesstools.com
  41. 41. @RoxanaStingu #BrightonSEO > Thank you!

×