1

I would like my website to point any URL

website.com/about/
website.com/about/more/

to the homepage. However, I would also like to maintain the URL in the browser URL bar.

How can I achieve this?

A redirect can be achieved with

RewriteRule .* / [L,R=301]

I have tried with

RewriteRule .* / [L,NC]

but then wordpress displays the page /about/ rather than ignoring the URL and just showing the homepage.

7
  • Doesn't WP do that by default more or less? You'd just have to display the frontpage instead of the 404 page, no?
    – kero
    Commented Aug 13, 2021 at 9:58
  • To clarify, these URLs don't exist in WordPress?
    – MrWhite
    Commented Aug 13, 2021 at 10:04
  • @MrWhite not exactly, but for some of them wordpress will redirect to a page which does exist. but that I think can be fixed by just removing the template_direct filter
    – Cameron
    Commented Aug 13, 2021 at 10:07
  • @kero ahhh, yes perhaps. how can i make the 404 page display the homepage then?
    – Cameron
    Commented Aug 13, 2021 at 10:27
  • Do you need this for these 2 URLs only?
    – mukto90
    Commented Aug 13, 2021 at 11:14

2 Answers 2

1

I assume you plan to replace the native mechanisms Wordpress uses to serve the queried objects and correct templates at each URL. I must say I don't see the point in doing this as you would lose many of the things Wordpress does to make our lives easier. You would have to write your own code to handle each scenario, like 404's for example. Maybe Wordpress is not the most ideal tool for what you intend.

Regardless, if I wanted to do that, I would probably just add a rewrite rule (through a plugin, not on the .htaccess) pointing any URL in the domain to the index:

function prefix_rewrite_rules(){

    add_rewrite_rule(
        '.*',
        'index.php',
        'top' //Places it as the prioritary rewrite rule
      );
  
}
  
add_action( 'init', 'prefix_rewrite_rules' );

Just as the other answer pointed out, you are adding a rewrite rule, so you must visit the Settings > Permalinks page in the admin, so that Wordpress may flush the rewrite rules and include this one. (Contrary to what was said in the other answer's comments, you only need to visit the page. Clicking "Save Changes" is not necessary).

0

Add this code to the functions.php file of your (child) theme or as a plugin-

add_action( 'init', 'wpse_393237_add_any_endpoint' );

function wpse_393237_add_any_endpoint() {
    add_rewrite_endpoint( '.*', EP_ALL );
}

Please note, you may need to flush the rewrite rules if you see a 404 error. (simply saving the permalink does the trick)

Ref: https://developer.wordpress.org/reference/functions/add_rewrite_endpoint/

4
  • what do you mean by saving the permalink?
    – Cameron
    Commented Aug 13, 2021 at 11:40
  • Just go to the Settings > Permalinks screen from your wp-admin dashboard. Scroll down and click the "Save Changes" button
    – mukto90
    Commented Aug 13, 2021 at 11:44
  • This doesn't seem to work. Instead it does a full redirect to the homepage. I would just like to point the website to the homepage, but not redirect the browser
    – Cameron
    Commented Aug 13, 2021 at 13:12
  • Do you have a static page set as homepage?
    – mukto90
    Commented Aug 13, 2021 at 16:02

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.