How do I redirect Pages from the main site with 404 Status to the Subdomain with same URL Structure?

This for example:


this returns a 404 Error and must be redirected to:


Is this possible on WordPress? If it is how can we do it?

  • Firstly, not sure how it is going to help you deal with 404. With custom implementation, it's definitely possible. Logically i would say it's definitely possible.
    – TopQnA
    May 17 '17 at 11:03
  • check : gist.github.com/ScottPhillips/1721489 Hope it helps......and yes its possible
    – smarttechy
    May 17 '17 at 11:38
  • @TopQnA it is possible with none wordpress site. I have seen the code which is added on the .htaccess. I do wonder how RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d can determine if it wordpress or not.
    – Alen
    May 18 '17 at 7:28
  • @TopQnA here is the code but I believe it isn't for wordpress since I have tried it on a wordpress site: stackoverflow.com/questions/12960140/…
    – Alen
    May 18 '17 at 7:35
  • 3
    As mentioned in my comment below, if you are using WordPress (or any other CMS for that matter where the content is controlled by a database and not simply on the filesystem) then you can't do this redirect in .htaccess. You will need to do something in WordPress itself - either a plugin or hijack the 404 response/page somehow.
    – MrWhite
    May 18 '17 at 8:36

What you are looking to accomplish can be done via proxy. It sounds like your server is Apache because there are several mentions of htaccess without correction (Nginx doesn't use it).

I am not an Apache expert (I use Nginx), so I can't give you an exact example solution. I have only implemented reverse proxies with Nginx.

I recommend reviewing Apache's docs on the topic (below) and asking this on ServerFault.

https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html https://httpd.apache.org/docs/current/mod/mod_proxy.html

There are several considerations you should think about:

  • Handling SSL
  • Setting the proper headers
  • Catching 404s

If you have a 404 page you could use that url instead of catching the requests.

For the record, I realize this is not an exact answer, but I don't have enough reputation to comment yet.

  • I can comment now, but I will leave my answer because the method I describe is the proper way to handle this functionality. Aug 9 at 22:26

There are several ways to add redirects for specific pages to a WordPress blog. There are plugins and extensions to WordPress that will allow redirects. http://www.wpbeginner.com/beginners-guide/beginners-guide-to-creating-redirects-in-wordpress/ recommends two of them.

I would take the approach of editing the .htaccess file and just putting the redirect in there:

Redirect permanent /rent http://subdomain.example.com/rent

This is the best plugin for managing 404 errors and redirections in WordPress. Very easy to use. https://wordpress.org/plugins/redirection/

It isn't automatic, so you will need to track 404s and add the redirections. Easy enough.


.htaccess Rule:

ErrorDocument 404 http://www.example.com/notFound.html

Do this in .htaccess file in main subdirectory

RewriteEngine On
RewriteCond %{HTTP_HOST} example.com
RewriteRule ^(.*)$ http://sub.example.com/sub$1 [R=permanent,L]
  • 5
    This simply redirects everything to the subdomain, which is not what the OP was asking at all. (You also have a spurious /sub subdirectory on the substitution.) Unfortunately it's not possible to do this sort of redirection in .htaccess on a WordPress site, since in order to determine if the request results in a 404, WordPress needs to perform some database lookups - this all happens long after .htaccess has handled the request.
    – MrWhite
    May 17 '17 at 14:39
  • we are talking about subdomain here not subdirectory.
    – Alen
    May 18 '17 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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