0

How can I redirect non existent pages/posts in WordPress using only .htaccess file?

Here is my code:

<IfModule mod_rewrite.c>

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^products/cool-tshirt$ /tshirts [R=301,L]

</IfModule>

Thank you in advance.

2
  • The RewriteRule directive above should already do what you require. If it's not working then you've probably put in in the wrong place - this redirect needs to go at the top of the .htaccess file, before the WordPress front-controller. If you are not using WP's permalinks (in which case you may not have WP front-controller in .htaccess) then make sure that mod_rewrite is enabled on your server.
    – MrWhite
    Commented May 24, 2018 at 11:34
  • You can just use one of redirection plug-ins. I use and like this one wordpress.org/plugins/nomore404-404-redirection-and-firewall It not only does redirects, but can help you block bad guys too Commented Oct 31, 2020 at 11:15

1 Answer 1

1

This should work:

RedirectMatch 301 ^/products/cool-tshirt$ http://yourdomain.com/tshirts$1

You can also see a bunch of other options and solutions here

Hope this helps.

5
  • if you review my code you can see that I want to redirect products/cool-tshirt to tshirts when I use the above code it open 404 page.
    – Noroti
    Commented May 7, 2014 at 8:38
  • Sorry I had misread the question. Edited my answer. Commented May 7, 2014 at 8:51
  • redirect alone doesn't work. It should be RewriteRule
    – Noroti
    Commented May 7, 2014 at 8:58
  • Also see this question Commented May 7, 2014 at 9:12
  • The $1 at the end of the target URL is superfluous and should be removed. However, if you are already using WordPress permalinks (ie. you have existing mod_rewrite directives in .htaccess) then you should ideally use mod_rewrite (ie. RewriteRule) instead, in order to avoid potential conflicts. (Or, even better, use WordPress and avoid .htaccess altogether.)
    – MrWhite
    Commented May 24, 2018 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.