I want to serve a specific WordPress page for multiple urls. Basically I want to point all the urls in a specific format to a known page:

/some-prefix-* => /target-page

And accessing /some-prefix-and-here-something it should load /target-page.

How to do this?

Note I do not want to redirect the user but to be able to serve an existing page to multiple urls.

The .htaccess looks like this:

# BEGIN s2Member GZIP exclusions
RewriteEngine On RewriteRule ^prefix-*$ some/path/to/directory-$1 [NC,L]
<IfModule rewrite_module>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{QUERY_STRING} (^|\?|&)s2member_file_download\=.+ [OR]
    RewriteCond %{QUERY_STRING} (^|\?|&)no-gzip\=1
    RewriteRule .* - [E=no-gzip:1]
</IfModule>
# END s2Member GZIP exclusions

# 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

DirectoryIndex index.php index.html

I thought by adding RewriteEngine On RewriteRule ^prefix-*$ some/path/to/directory-$1 [NC,L] will serve /prefix-* from some/path/to/directory-$1. For example: when accessing example.com/prefix-foo should be the same with example.com/some/path/to/directory-foo (which resolves to example.com/some/path/to/directory-foo/index.html).

Also, if there is already a WordPress page with the name prefix-bar, /prefix-bar should not load example.com/some/path/to/directory-bar/index.html.

3 s
3

You can use template_include, but before you hook to this filter you must do the following steps:

  1. Create page template. e.g: page-target.php

    <?php
    /**
     * Template Name: Page Target
     */
    ...
    
  2. Manually query the contents of target-page on page-target.php template, because the global $post will be referencing to your some-prefix-* page.

  3. (Optional): Edit and apply page template Page Target to your /target-page from the Page Attributes

Then add the following to your functions.php file

add_filter('template_include', function($template) {
    global $post;

    $prefix = 'some-prefix-'; // Your prefix
    $prefixExist = strpos($post->post_name, $prefix) !== false;

    if ($prefixExist) {
        return locate_template('page-target.php'); //Your template
    }

    return $template;
});

Above filter will override the template with page-target.php if condition met, otherwise default

Edit:

I think I have found a better way to resolve this using add_rewrite_rule

function custom_rewrite_basic() {
  $page_id = 123; //Your serve page id
  add_rewrite_rule('^some-prefix-.*', 'index.php?page_id=' . $page_id, 'top');
}
add_action('init', 'custom_rewrite_basic');

IMPORTANT: Do not forget to flush and regenerate the rewrite rules database after modifying rules. From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *