I have a WordPress website and I’m trying to redirect all pages to a landing page on the same domain except for the home page.
I tried many solutions found around internet but none of them worked.
What I need is:
- Home page and wp-admin page should always be reachable (example.com and examplecom/wp-admin)
- All the other pages should be redirected with a 302 code to example.com/redirect-page
Thanks in advance
3
Answers
You can use filter that would allow you to create an exception with code which handles website redirect based on current configuration.
https://plugins.trac.wordpress.org/browser/simple-website-redirect/trunk/SimpleWebsiteRedirect.php#L113
Something like this :
Htaccess : https://stackoverflow.com/a/38594600/19168006
Edit : You need to add that code to your theme
functions.php
This is the second solution: Perhaps not as elegant as a theoretical, functional .htaccess solution (if it exists).
// Redirect entire site except one page ID
If you don’t want to redirect the admin section, remove !
is_admin()
&& Replace http://example.com with the destination you wish to redirect traffic to By default this performs a 301 (permanent) redirect; if you intent for this to be temporary, change 301 to 302 Obviously swap out 9999 for the page ID you wish to exclude.Note, however, that if you aren’t using a child theme and you have auto-updates on, it will likely be overwritten at some point when your theme forces an update and you’ll have to update it again.
More explanition : here
There might be a better solution, but you could try this:
you could use
$_SERVER['REQUEST_URI']
to get part of the url:URL: http://www.example.com/abc/de
output
$_SERVER['REQUEST_URI']
: /abc/deThen you can use
<meta http-equiv="refresh" content="0; url=http://example.com/redirect-page" />
to redirect.
So something like this:
Try the following at the top of the root
.htaccess
file, before the# BEGIN WordPress
comment marker (you do not need to repeat theRewriteEngine
directive):I’ve also included "static assets" (images, CSS, JS – anything with a file extension) in the exception, since I imagine
/redirect-page
will need to access these.And naturally, we need to make an exception for
/redirect-page
itself, otherwise it would result in an endless redirect loop.The
!
prefix on the regex negates the expression, so it is successful when it does not match.Note that the URL-path matched by the
RewriteRule
pattern does not start with a slash, unlike theREQUEST_URI
server variable which does.The above can be reduced to a one-liner if you prefer (at the expense of readability):