Search code examples
phpwordpress.htaccesshttp-redirecturl-rewriting

Looking to implement redirects for entire website and a specific page


I am looking to implement the following. if someone visits the site oldsite.com or any of the pages under it they will be redirected to newsite.com

BUT i need a specific page on oldsite.com to redirect to something else.

oldsite.com/page should go to newsite2.com

To summarize. oldsite.com and any pages under that -> newsite.com oldsite.com/page -> newsite2.com

I have tried using the Cpanel redirects but it doesnt seem to work for the specific page.

Adding

RewriteEngine On 
RewriteRule ^(.*)$ http://www.newdomain.com [R=301,L]

to htaccess file as well. I cannot seem to figure out how to do the last part of redirecting the specific url to another website while keeping the redirects to everything else.


Solution

  • You can use the more "simple" mod_alias directives:

    # Redirect specific page
    RedirectMatch 301 ^/page$ https://newsite2.example/
    
    # Redirect everything else
    RedirectMatch 301 ^/ https://newsite1.example/
    

    Note the order of the directives. The most specific redirect is first.

    This assumes the old-domain is not pointing to the same place as newsite2.example and/or newsite2.example.

    You should test first with 302 (temporary) redirects to avoid potential caching issues.

    NB: Never mix redirects from both mod_alias (Redirect and RedirectMatch) and mod_rewrite (RewriteRule) since you can get unexpected conflicts. (mod_rewrite is always processed before mod_alias despite the apparent order of the directives in the config file.)

    And, as mentioned in comments, if SEO is a concern then many-to-one redirects to the homepage like this are likely to be seen as soft-404s by search engines (and generally gives a bad experience for users).