0

I've integrated a custom PHP script within my wordpress instalation (with theme page-template). The page is /islands/island/ and I get the islandId with GET paramater. So the URL looks like https://website.com/islands/island/?iid=1

But now I want to rewrite the url to be like this: https://website.com/islands/island/1

I already tried edditing the .htaccess but with no luck.

After some research I found editing .htaccess is not the right way to do this. So I used the followingn code and added it to my theme's function.php.

function add_directory_rewrite() {
    add_rewrite_tag("%iid%", '([^/]*)');
    add_rewrite_rule('^islands/island/([^/]*)', 'index.php?pagename=islands/island&iid=$matches[1]', 'top');
}
add_action( 'init', 'add_directory_rewrite' );

But unfortunately it is not working. When I browse to the page http://website.com/islands/island/1 it redirects to http://website.com/islands/island/. Am I missing something?

JesseH
  • 53
  • 8
  • You should showcase some of the no luck editing. Else take a look at the linked tutorials first. – mario Mar 31 '19 at 10:38
  • The duplicate you set is based on normal PHP websites, not Wordpress. I've added some of the code. – JesseH Mar 31 '19 at 11:06
  • 1
    The proper placeholder would be `\d+`. Reopened, if you feel the unreliable WP dispatching is preferrable over a classic RewriteRule. – mario Mar 31 '19 at 11:19
  • With integrating in plugins and themes you have to use WP function if you do not want to manually change htaccess for each install right? – JesseH Mar 31 '19 at 12:28

1 Answers1

0

Got it working! Working code:

function add_directory_rewrite() {
    add_rewrite_tag("%iid%", '[\d+]');
    add_rewrite_rule('islands/island/([^/]*)', 'index.php?pagename=islands/island&iid=$matches[1]', 'top');
}
add_action( 'init', 'add_directory_rewrite' );

Changed the placeholder tags as @Mario suggested.

JesseH
  • 53
  • 8