Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9218337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:50:22+00:00 2026-06-18T02:50:22+00:00

I need add a rewrite rule in my plugin, and distribute it with my

  • 0

I need add a rewrite rule in my plugin, and distribute it with my code. All works fine if I put the rule in the .htaccess in the WordPress root folder, but I need distribute the plugin with my rule.

I try to put a .htaccess inside the plugin folder and try to use the add_rewrite_rule function but doesn’t works either.

Here the .htaccess code that works correctly in WordPress root folder but doesn’t works in my plugin folder:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteRule my-plugin/pages/tp(.*)\.php$ wp-content/plugins/my-plugin/pages/request.php?pid=$1

</IfModule>

I try the follow code in my plugin but doesn’t works either:

add_filter( 'query_vars', 'add_query_vars' );
function add_query_vars( $query_vars )
{
    $query_vars[] = 'pid';
    return $query_vars;
}
add_action( 'init', 'add_init' );
function add_init()
{
    $plugin_url = 'the-path-to-my-plugin-folder';
    add_rewrite_rule('my-plugin/pages/tp(.*)\.php'
                  , $plugin_url . 'pages/request.php?pid=$matches[1]','top');

    global $wp_rewrite;
    $wp_rewrite->flush_rewrite_rules(); // I know this should be called only one time, but I put it here just to keep simple the sample code
}

But I always get the error that the URL wasn’t found.
What I’m doing wrong? How can I do what I need? I searched for similar questions but none solve my problem.

My plugin folder structure is:

Main folder: /wp-content/plugins/my-plugin
—— /pages (sub folder)
————-/request.php (script that should receive the request)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:50:23+00:00Added an answer on June 18, 2026 at 2:50 am

    NOTE: WordPress Rewrite API is not the same as Apache Rewrite module.
    WP Rewrite API doesn’t redirect a request to another URL, it used to
    parse current URL and fill query_vars array.

    The issue is in the second parameter of you add_rewrite_rule function call. It has to start from index.php? and then there should be your arguments, like pid, for example:

    "index.php?pid=$matches[1]...."
    

    So your add_init function should be like this:

    add_action( 'init', 'wpse8170_add_init' );
    function wpse8170_add_init()
    {
        add_rewrite_rule('my-plugin/pages/tp(.*)\.php', 'index.php?pid=$matches[1]', 'top');
    }
    

    Don’t forget to flush rewrite rules by visiting Settings » Permalinks page.

    Further reading:

    • The Rewrite API: The Basics
    • The Rewrite API: Post Types & Taxonomies
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using a .htaccess rewrite rule, I need to add ?q= before the path on
I've got this Rewrite Rule. RewriteRule ^news/([0-9]+))/? /index.php?show=news&id=$1 Now I need to add some
I have all my rewrite rules setup and working in .htaccess but I need
I need to add a rule to my .htaccess file to do this: Go
I have written a basic htaccess rewrite rule to point all images inside a
I have a rewrite rule in .htaccess RewriteRule (.+?\.fid)/ /$1 [L] with a request
The following sample plugin adds the custom mod rewrite rules to .htaccess when the
I have the following .htaccess rule: RewriteEngine on RewriteRule ^(.*) http://www.server2.com/index.php?action=$1 I need to
I have a standard Drupal site and need to add a redirection rule for
I'm having trouble with some URL rewriting. All of the stuff below works fine,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.