1

Variations of this question have been asked, however the scenarios do not match mine and despite my efforts I have not been able to adopt the previous answers to a working solution in my use case.

I have a page template that has a javascript application built into it. This application uses the last parameter in a URL as a parameter for an ajax request, which it runs and then produces results for.

Due to SEO concerns and client requirement, I cannot change or alter the way this request syntax functions, so I must make it work in Wordpress (page is being converted from a Ruby application)

When the client makes a request to:

https://www.wordpresssite.com/service/testing-tool/domain.com

I need to respond with the page that is built using the template at:

https://www.wordpresssite.com/service/results

This part I have working with the following rewrite rule in .htaccess

RewriteRule ^service/testing-tool/([a-z0-9.]+)$ /index.php?pagename=service/results [NC,L]

The rewrite works, however WordPress changes the URL in the browser to

https://www.wordpresssite.com/service/results

This causes the JS to fail as the domain parameter is missing, and it also doesn't meet the requirements previously mentioned.

I though after doing some research here that the issue is the redirect_canonical() action, so I tried removing it from within the template:

// Disable redirect_canonical()
add_action( 'init', 'results_tweaks' );
function results_tweaks()
{
    remove_action( 'template_redirect', 'redirect_canonical' );
}

However this doesn't seem to do it. Reading the WP documentation, I suspect this is because the action is already processed by the time the page loads.

Is there another way I can override this action specifically for this template, and no others? Perhaps another way aside from removing the action?

1

You can use an internal rewrite instead of .htaccess:

function wpd_service_rewrite() {
    add_rewrite_rule(
        '^service/testing-tool/([a-z0-9.]+)$',
        'index.php?pagename=service/results',
        'top'
    );
}
add_action( 'init', 'wpd_service_rewrite' );

Don't forget to flush rewrite rules after adding / changing them.

  • This did it. Not sure why an internal rewrite behaves differently than one in .htaccess, however it does the trick without changing the URL in the browser, just like I require. Thanks! – Routhinator Mar 1 '17 at 15:27
0

You might be able to solve your problem just by changing your rewrite rule to:

RewriteRule ^service/testing-tool/([a-z0-9.]+)/?$ /index.php?pagename=service/results&domain=$1 [NC,L]

Now, from within https://www.wordpresssite.com/service/results you can get the domain via PHP.

Note that I added '/?' to your rule, otherwise it shouldn't work.

Using php you'd get the domain (missing param) using $_GET['domain'].

Hope this helps.

  • This doesn't solve the issue . The issue is the URL cannot be changed. This this solves the lost var. I had thought of this however it doesn't maintain the URL itself in the browser. I don't want the user to be redirected. Just served the template at the same URL they entered. – Routhinator Mar 1 '17 at 3:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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