phpwordpressmod-rewriteurl-rewritingcustom-pages

Hide get parameters names from the URL


I know this can be done using Rewrite engine but I am unable to do this This is my .htaccess file

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

I have this url www.mysite.com/report?page=my-report-name

Now what I want to acheive is this : www.mysite.com/report/my-report-name

The file where I am accessing this get variable name page is reports.php and its not in my root directory Path to my file is : root/themes/fount/intel/reports.php

Can anyone help?


Solution

  • Updated answer

    To add a routing which points to an external handler PHP file, the following snippet should be used:

    function wprre_add_rewrite_rules() {
        global $wp_rewrite;
    
        // pattern with regexps
      $wp_rewrite->add_external_rule( '^wp_report/([\w\d-]+)/?', PATH_TO_THE_EXTERNAL_HANDLER.'report.php?report_name=$1' );
    }
    add_action('init', 'wprre_add_rewrite_rules');
    

    You can spot one difference in the parameter handling between add_external_rule() and the add_rewrite_rule. You must use the match selector as the Apache uses it in this case.

    This snippet must placed in a file which is always loaded by your plugin or theme. If you write a plugin it can be the main plugin file. In case of theme development it can be the main functions.php file.

    The custom GET parameter registration is working as it was mentioned in the Original answer.

    IMPORTANT

    After you edited the rewrite rules via code (external or internal both) you must go to the Permalink settings page in the admin panel and click to the Save button without any changes. This is necessary, because this will flush the rewrite rules and the WP will write into the .htaccess file the rules.

    This is the reason why I recommend you to hook on the plugin activation event and register the rewrite rules then and immediately run a flush_rewrite_rules() command.

    NOTES

    The problem with the original answer was that, the add_rewrite_rule() function only works if you route to the default basic index.php. You can only modify the parameters, but you can not route to an external file.

    Original answer

    I think you should use the WordPress API to achieve this. You will need to add a rewrite rule and tag in you theme or plugin with this syntax:

    !! Disclaimer this is only working to route to the basic index.php !!

    For the routing, add a rewrite rule which points to your PHP file.

    function custom_rewrite_basic() {
      add_rewrite_rule('^report/([\w-]+)/?', 'index.php?page=$matches[1]', 'top');
    }
    add_action('init', 'custom_rewrite_basic');
    

    If you want to use a query parameter which is not in the standard WP parameter list, you need to add that custom parameter name.

    function custom_rewrite_tag() {
      add_rewrite_tag('%page%', '([^&]+)');
    }
    add_action('init', 'custom_rewrite_tag', 10, 0);
    

    Be aware to you use built in parameters if you do not use as the WP API.

    In this Codex article you find more details about the topic: https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

    For troubleshooting and deeper dive understanding you might to check this Codex article too, which describes the proper rewrite rule usage. Because some in circumstances you need to reset the rewrite rules (plugin activation / deactivation). https://codex.wordpress.org/Function_Reference/flush_rewrite_rules