.htaccess enables us to easily clean up 404 errors and other misdirected HTTP traffic for better SEO. Here are some canonical snippets for WordPress and beyond.
Canonical .htaccess snippets
Each of the .htaccess directives below include a brief description as an inline comment (
# like this). Not all of these techniques will apply to every site, and most of them are specifically for WordPress. Recommended to read the description, grab what is useful, and include in the root .htaccess file of your site. Note: the last three snippets are useful for any website, not just WP.
# CANONICALIZATION SNIPPETS <IfModule mod_alias.c> # redirect empty tag requests to homepage RedirectMatch 301 ^/tag/?$ http://example.com/ # redirect empty page requests to homepage RedirectMatch 301 ^/page/?$ http://example.com/ # redirect empty search requests to homepage RedirectMatch 301 ^/search/?$ http://example.com/ # redirect empty category requests to homepage RedirectMatch 301 ^/category/?$ http://example.com/ # redirect subdirectory to homepage RedirectMatch 301 /wp/?$ http://example.com/ # redirect favicon requests to the actual favicon RedirectMatch 301 ^/favicon\.(?!ico) http://example.com/favicon.ico RedirectMatch 301 (?<!(^/))favicon\. http://example.com/favicon.ico # redirect apple-icon requests to the actual icon RedirectMatch 301 /apple-touch-icon(.*)?.png http://example.com/apple.png # redirect requests for humans.txt and robots.txt to the actual file RedirectMatch 301 (?<!(^/))(humans|robots)\.txt http://example.com/$2.txt # redirect requests for xmlrpc.php to the actual file RedirectMatch 301 (?<!(^(/wp/)))(xmlrpc)\.php http://example.com/wp/xmlrpc.php # redirect requests for the wp-login.php file to the login page RedirectMatch 301 (?<!(^(/wp/)))(wp-login)\.php http://example.com/wp/wp-login.php </IfModule>
Remember to change all instances of
example.com/wp with the URL of your site and WordPress installation directory. For example, if you have WordPress installed in the root directory instead of a subdirectory
/wp/, you would change all instances of
example.com/wp to simply
example.com. And so on and so forth.
Good day to you!