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
How I learned to stop worrying and love the .htaccess file
1. @RoxanaStingu #BrightonSEO
How I learned to stop
worrying and love the
.htaccess file
Roxana Stingu // Alamy
SLIDESHARE.NET/RoxanaStingu
@roxanastingu
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
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
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. @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. @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
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. @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
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. @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