How to Add 301 Catchall to Wordpress .htaccess

I recently bought a domain with a PR. I am setting up a Wordpress installation on this domain and would like to set up to 301 catchall. (The previous site using this domain also was using Wordpress.)

Here are the contents to my .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

How do I add a 301 catch all to the above code?



Two points:

  1. The way that WP writes their code, it’s as strong as a Redirect 301. In other words, WHY do you want to repeat what has already been done for you? In fact, using a 301 “catchall” will also redirect .css, .js, .gif, .jpg, etc files which mod_rewrite’s conditional deftly avoids.

  2. I have a phobia about webmasters who use code they do not understand. To leave <IfModule> conditionals in an .htaccess file makes me go into my

[standard rant #4][indent]The definition of an idiot is someone who repeatedly does the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> … </IfModule> wrapper is the same thing in the webmaster world. DON’T BE AN IDIOT! If you don’t know whether a module is enabled, run the test ONCE then REMOVE the wrapper as it is EXTREMELY wasteful of Apache’s resources (and should NEVER be allowed on a shared server).[/indent][/standard rant #4]

  1. I’m also not fond of WP’s RewriteBase directive as that can upset the mod_rewrite redirections when not / and not in the DocumentRoot. Because the RewriteBase directive was created to “undo a mod_alias redirection,” you don’t need this line either.



Thanks for the response, David.

The code I had shown was copied as-is from what WP supplied. It seems you’re suggesting I remove both the IF conditional and rewritebase, which I’m fine with.

The reason I’d like to get a 301 redirect is to preserve the PR acquired with my domain. There are a lot of inbound links that are now linking to pages that no longer exist. I would rather have a 301 redirect to the homepage than return a 404 status.

Perhaps “catchall” isn’t the correct time. I only want to “catch” web pages that are being called that would otherwise return a 404. Hope that clarifies my question.

Is there a way to do what I’m looking for?



Yes, that’s WP’s standard code and I know of no valid reason to copy (and use) code without knowing it’s purpose (the <IfModule> wrapper is to prevent 500 errors on servers without mod_rewrite enabled) and the RewriteBase is to counter any unexpected Redirect statements which may have been legacy code - remove ALL if you have no reason to keep them).

WP redirections do not need a 301 as your links will be served by index.php … and it’s better to allow index.php to do its job by parsing the original links without displaying the redirection. If you use a 301, you will display index.php and not retain your PR. In other words, WP has done the job for you … and WP does not return a 404 as the index.php script handles ALL requests (which do not exist as files or directories).