Redirect all 404 errors with 301 to HomePage in WordPress using .htaccess (including reserved wordpress terms)

I have an old WordPress website with a lot of pages. I changed to a single page website and I want to redirect all 404 errors with 301 redirect to Homepage.

I don’t want to use plugins or change the 404 page. I want to do it in the .htaccess file.

Read More

This is the code I tried:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [L,R=301]

This works perfect with almost all cases, like:

www.site.com/111 -> redirect 301 -> www.site.com/
www.site.com/abcd/222.html -> redirect 301 -> www.site.com/

But it didn’t work in the cases below:

www.site.com/?p=123
www.site.com/abc/?attachment_id=abc

The problem is that this don’t work with URLs that contains WordPress terms:
https://codex.wordpress.org/WordPress_Query_Vars

Does anybody know how to solve this problem?

Related posts

Leave a Reply