2

I am trying to remove the query string from my URLs when I redirect them but it is preserving them. I can't get the regex right.

Also, I have multiple templates that redirect to different urls so I can't just use .* after howto.php

.htaccess is not an option in this case so I must figure out how to do this in the Redirection plugin

I want /templates/howto.php?page=template-business-plan

to go to

/business-plan

but i keep getting /business-plan?page=template-business-plan

2
  • Could you please elaborate how exactly redirect happens and at which point you want to customize the behavior? If this is too specific to the plugin it might be better aimed at its developers/support.
    – Rarst
    Nov 3, 2016 at 15:56
  • I tried there but no luck so far. I think it is more regex related as opposed to being specific to the plugin, but I could be wrong.
    – Nate Beers
    Nov 3, 2016 at 16:15

2 Answers 2

0

I also use this plugin, so I figured I'd dig into the code to see if I can find the answer.

There are two actions you could use for this that may work redirection_first and redirection_last

Both take two arguments: $url and $this (which is the wordpress module for the redirection plugin)

Here's the snippet of code from the module init() in \modules\wordpress.php

public function init() {
    $url = $_SERVER['REQUEST_URI'];

    // Make sure we don't try and redirect something essential
    if ( ! $this->protected_url( $url ) && $this->matched === false ) {
        do_action( 'redirection_first', $url, $this );

        $redirects = Red_Item::get_for_url( $url, 'wp' );

        foreach ( (array) $redirects as $item ) {
            if ( $item->matches( $url ) ) {
                $this->matched = $item;
                break;
            }
        }

        do_action( 'redirection_last', $url, $this );
    }
}

So, $url is your requested URL, and in redirection_last, $this->matched has your redirection url in it. I would start with redirection_start and you could run something like:

function redirection_strip_query_string( $url = '', $this ) {

    if ( strpos( $url, '?page=' ) !== false ) {

        $url = preg_replace('/\?.*/', '', $url);

    }

}

add_action( 'redirection_first', 'redirection_strip_query_string' );

Two notes:

  1. I have not tested this code.
  2. I wanted to give credit to the simple URL preg_replace
-1

I've solved it by slight change in plugin code, by adding two filter hooks. Hoping that author will incorporate those hooks - 2 non-destructive lines, as they allows us much more customization of plugin behaviour. Explained here in detail: https://github.com/johngodley/redirection/issues/180

UPDATE: This "suggestion" of mine is incorporated upstream, in plugin itself, so there is no need to modify anything: we now have redirection_url_target and redirection_url_source filters so custom code can manipulate the source and target redirect for special purposes.now there

4
  • 1
    you should never change a plugin's code unless you are ready to take full responsibility for it Feb 22, 2017 at 7:23
  • "... that author will probably incorporate those hooks". So my answer was more of suggestion to wait for plugin update and not how to do it. That's why there is a link to plugin issue, to monitor what's happening, because currently there is no way to do it differently.
    – Vladan
    Feb 25, 2017 at 4:42
  • from my personal POV it is more important what you have written than what you would have actually done. Your have written "I've solved it by slight change in plugin code, by adding two filter hooks" which is cool if you know what you are doing, but might give the wrong idea to unexperianced people that will do something like that and lose the changes when the plugin updates Feb 25, 2017 at 4:50
  • This "suggestion" of mine is incorporated upstream, in plugin itself, so there is no need to modify anything: we now have redirection_url_target and redirection_url_source filters so custom code can manipulate the source and target redirect for special purposes.now there
    – Vladan
    Nov 22, 2019 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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