1

I am stuck at redirecting one url to another url.

I want to redirect

https://samedomain.com/page-check-927GSJAYS639AL/highlights

to

https://samedomain.com/page-highlights/

(927GSJAYS639AL this is id it can be anything)

the http://samedomain.com/page-highlights/ is the wordpress page

The rewrite rule I tied in .htaccess file (added this in the first line it-self) is:

RewriteEngine On
RewriteRule ^page-check-([A-Z0-9_-]+)/highlights$ page-highlights?id=$1 [L]

but I see a 404 error page

It worked for

RewriteEngine On
RewriteRule ^page-check-([A-Z0-9_-]+)/highlights$ info.php?id=$1 [L]

I also tried WP rewrite API php code

UPDATE 1

add_action('init', 'ao_add_rewrite_rule');
function ao_add_rewrite_rule() {
   add_rewrite_rule('^page-check-([A-Z0-9_-]+)/highlights/?$', 'index.php?pagename=page-highlights&id=$matches[1]', 'top');
}

UPDATE 2

add_action('init', 'ao_add_rewrite_rule');
function ao_add_rewrite_rule() {
   add_rewrite_rule('^page-check-([A-Z0-9_-]+)/highlights/?$', 'index.php?pagename=page-highlights&id=$matches[1]', 'top');
}
add_filter( 'query_vars', 'wpa5413_query_vars' );
function wpa5413_query_vars( $query_vars ) {
   $query_vars[] = 'id';
   return $query_vars;
}

Adding this line RewriteRule ^page-check-([A-Z0-9_-]+)/highlights$ index.php?pagename=page-highlights&id=$1 [L] in .htaccess file works, but it will do 301 redirect, so the URL also changes back to https://samedomain.com/page-highlights/

No Luck :( in finding solutions

Any help is highly appreciated. Thanks in advance.

6
  • 2
    Possibly a silly question, but are you sure page-highlights is the actual slug of a page, and not just the name of a template?
    – vancoder
    May 6, 2022 at 18:30
  • rewrites are not what you want, redirects are what you want, there are functions such as wp_redirect and wp_safe_redirect for that. HTAccess could do it but not using rewrites, and the WP rewrite rules are for mapping not for redirecting
    – Tom J Nowell
    May 6, 2022 at 21:14
  • @vancoder yes, you are right. i have been using slug of a page. I am a new bee to this Wordpress. Could you please guide me how can I achieve this? May 6, 2022 at 21:39
  • @TomJNowell if i use wp_redirect the URL also changes , i am not looking for redirect rule here. i just want to show https://samedomain.com/page-highlights/ contents on https://samedomain.com/page-check-927GSJAYS639AL/highlights May 6, 2022 at 21:41
  • @chandanaGowda that would lead to duplicate content, with both the new and old URLs serving the same data, and SEO penalties as a result. WP Rewrite rules always take the form of "pretty URL" -> "index.php?foo=bar" they can't be used to make "synonyms" or edit other rewrite rules, only add new pretty URLs that map to ugly permalinks. WP will always try to redirect to the canonical URL to avoid SEO hits. What is it that you're trying to redirect? Is it a page listed in the pages admin area? A custom post type? Where does the number/letters you're trying to remove come from?
    – Tom J Nowell
    May 8, 2022 at 13:40

1 Answer 1

1

I'm honestly struggling to comprehend this failure state - I believe I've reproduced the issue locally, and in my testing the RewriteRule directive hits but then WordPress processes the original input, resulting in a fairly catch-all attachment rewrite producing a query for a non-existent attachment. I may continue playing with it to try and figure out what's going on.

In the meantime, I think the root of the issue is complexity introduced by using a URI as the target of the RewriteRule. Using a file path seems to alleviate the problem:

RewriteRule ^page-check-([A-Z0-9_-]+)/highlights/?$ index.php?pagename=page-highlights&id=$1 [L]

Regarding the use of add_rewrite_rule(), I have a few notes:

  • When you use a target which does not start with index.php, WordPress will write the rule to the .htaccess file and implicitly begin the pattern with the ^ character - thus, starting a pattern with ^ while using a URI target can result in malformed rule beginning with ^^.
  • Instead of positional references to matches such as $1, WordPress uses the syntax $matches[1].
  • Flushing rewrite rules is a fairly expensive operation - I'd strongly recommend against flushing them in routine hook!

The above in mind, an add_rewrite_rule() with using a filepath target akin to the above RewriteRule directive would look as such:

add_action('init', 'ao_add_rewrite_rule');

function ao_add_rewrite_rule() {
   add_rewrite_rule('^page-check-([A-Z0-9_-]+)/highlights/?$', 'index.php?pagename=page-highlights&id=$matches[1]', 'top');
}
5
  • it solved my problem. Thank you May 8, 2022 at 20:35
  • 1
    "the RewriteRule directive hits but then WordPress processes the original input" - The issue is internally rewriting the request to another "pretty" URL, which is then rewritten again to index.php (the WP front-controller). This will likely fail on any CMS/framework that uses a front-controller pattern, like WP. The intermediary rewrite is internal to Apache, occurring before WP gets to handle the request. WP looks at $_SERVER['REQUEST_URI'] to retrieve the originally requested URL. (In this instance, you could look at $_SERVER['REDIRECT_URL'] instead but at other times this would fail.)
    – MrWhite
    May 9, 2022 at 23:23
  • @bosco: I thought it was working, but it is redirected to samedomain.com/page-highlights in the inspect i can see 301 Moved Permanently this also failed :( May 10, 2022 at 17:53
  • @bosco: i also tried this solution Need help with add_rewrite_rule, didn't work for me May 10, 2022 at 18:15
  • Pardon the late reply - are you still experiencing the issue?
    – bosco
    May 14, 2022 at 17:28

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.