0

What I'm trying to do it's pretty much simple, but it's not working and I don't know if it's a Wordpress rewrite or I'm just doing it wrong.

What I have is this url:

http://www.example.com/newsletter/?ut=token

And I want .htaccess to write it as following:

http://www.example.com/newsletter/confirm/token

Same thing for this:

http://www.example.com/newsletter/?uc=token

Which should be rewritten as:

http://www.example.com/newsletter/delete/token

This is my actual .htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^newsletter/confirm/(.+)/?$ index.php?page_id=4058&ut=$1 [L,NC]
RewriteRule ^newsletter/delete/(.+)/?$ index.php?page_id=4058&del=$1 [L,NC]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

4058 is the id of newsletter page. I've also tried this:

RewriteRule ^newsletter/confirm/(.+)/?$ newsletter/?ut=$1 [L,NC]
RewriteRule ^newsletter/delete/(.+)/?$ newsletter/?del=$1 [L,NC]

But it still not working at all. With the first method it goes to newsletter page without query string ( so it gives an empty page ); with the second one it gives 404 Not found.

I've also tried using Wordpress's built-in function add_rewrite_rule without success, this is what I've tried:

function customRewriteRules(){
    add_rewrite_rule('^newsletter/confirm/(.+)/?', 'index.php?page_id=4058&ut=$matches[1]', 'top');
    add_rewrite_rule('^newsletter/delete/(.+)/?', 'index.php?page_id=4058&del=$matches[1]', 'top');
}
add_action('init', 'customRewriteRules');

Could it be something related to a kind of rewrite priority for Wordpress's base rewrite conditions? Or am I just doing it bad?

4
  • add_rewrite_url() should work but add_rewrite_endpoint() seems more appropiated for your case. Can you show to us the code you have tried?
    – cybmeta
    Commented Nov 4, 2015 at 11:37
  • Sorry, I wrote add_rewrite_url() instead of add_rewrite_rule() Commented Nov 4, 2015 at 11:38
  • Please check the update question Commented Nov 4, 2015 at 11:44
  • Your code seems fine. Have you flushed the rewrite rules after adding the snippet? You can flush rewrite rules by vivisting to the admin area Settings->Permalinks and clicking the save buttong or by using flush_rewrite_rules() (do it only on plugin activation/deactivation hook)
    – cybmeta
    Commented Nov 4, 2015 at 11:57

1 Answer 1

1

WordPress has its own rewriting system in PHP so you can do it both ways

If you want to rewrite these url from htaccess, i would only make it rewrite

http://www.example.com/newsletter/confirm/token to http://www.example.com/newsletter/?ut=token

instead of trying to rewrite http://www.example.com/newsletter/confirm/token to http://www.example.com/index.php?page_id=4058&ut=token

And let WordPress handle the page id rewrite.

So the htaccess rule would be:

RewriteRule ^newsletter/confirm/(.+)/?$ /newsletter/?ut=$1 [L,NC]

Alternatively you can define your rewrite rule in the WordPress rewrite system:

add_rewrite_rule('^newsletter/confirm/(.+)/?$', 'index.php?page_id=4058&ut=$matches[1]', 'top');

Anyway, to access this new query variable you have to declare it in WordPress aswell:

add_filter('query_vars','my_add_ut_query_var');

function my_add_ut_query_var($vars) {
    array_push($vars, 'designer');
    return $vars;
}

Now you can access it from your page template:

$token = get_query_var('ut')

And don"t forget to flush your rules when you add a new one of course :)

(Also be careful you have a typo with your uc or ut parameter)

3
  • Are you saying that my second RewriteRule was ok? Anyway, newsletter/confirm doesn't exist, why did you put in in yours RewriteRule? P.S. I've tried your rewrite rule but it's not working :( Commented Nov 4, 2015 at 11:57
  • Sorry, edited my answer, my RewriteRule was false. The important point for you i think is to add the query_vars filter. Commented Nov 4, 2015 at 12:02
  • Ok, something has started working. Now I need to check if there are those query_vars. I'll accept your answer as soon as I go back to the office. Thank you :) Commented Nov 4, 2015 at 12:03

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.