Search code examples
.htaccesshttp-redirecthttp-status-code-302

Wordpress 302 Temporary redirect via htaccess


I need to temporarily move the directory of my site so I can drop in a landing page on the domain root. The landing page will live at a subdomain and point to the root domain (via DNS).

I am trying to do a simple 302 redirect via htaccess to change the directory of the site from https://example.com/ to https://example.com/home/, but I get stuck in a loop. When I load the site I get this forever...

https://example.com/home/home/home/home/home/home/

Any ideas how to get this to work?

Redirect 302 / https://example.com/home/


# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Solution

  • The mod_alias Redirect directive is prefix-matching, and everything after the match is copied onto the end of the target URL. So, the same directive will redirect /home/ (which matches /) to /home/home/ (home/ is appended on the target), etc. etc.

    Since you are already using mod_rewrite in the WordPress code block, you should also use mod_rewrite here also to avoid potential conflicts. mod_rewrite always executes before mod_alias, regardless of the order of directives in .htaccess.

    For example, to 302 redirect the root only then use the following instead:

    RewriteRule ^$ /home/ [R,L]
    

    Obviously, this redirect will need to be removed when you implement the alternative landing page in the root.

    The landing page will live at a subdomain and point to the root domain (via DNS).

    However, if you are implementing this "landing page" via a "subdomain" (as suggested in comments) then I'm not sure why you need to move the existing homepage since it exists on a different hostname. (?)

    But implementing the new landing page on a subdomain (that presumably points to the main domains document root) is going to create a very disjointed site. The subdomain and the main domain are two different sites.

    If you are wanting to create a new landing page (replacing the old homepage) outside of WordPress (on the same domain) then I don't see how you can implement a "redirect" from the old homepage to /home/ since this will naturally redirect the new landing page - unless this is only "temporary" as you say, until the new landing page is implemented?

    As far I can see, you would need to:

    1. Create a new WP page for /home/ with the existing content of the homepage. (The existing WP homepage will no longer be accessible.)

    2. Create your new landing page in a separate subdirectory (which won't be visible in the URL) - or perhaps just a separate file (eg. /new-landing-page.php) if this is a relatively simple page.

    3. Internally rewrite requests from the document root (that would otherwise display the WP home page) to the new-landing-page, and prevent the request being routed through WordPress.

      For example:

      # Rewrite requests for the homepage to the new landing page.
      RewriteRule ^$ new-site/new-landing-page.php [L]
      

      When example.com/ is requested example.com/new-site/new-landing-page.php is served (no redirect). WP is bypassed. But /<something> is routed through WP in the usual way.