Search code examples
apache.htaccessmod-rewriteurl-rewritingfriendly-url

redirect issue with rewrite rule $tag


Having some problems with this below; Specifically with the first rule.

^pages/articles/marketArticles\.php?id=31 I think here is the problem , So basically what I'm trying to achieve is that when a user goes to /pages/articles/marketArticles.php?id=31 the user is redirected to /01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022/ this rule has to work with multiple different ids.

RewriteRule ^pages/articles/marketArticles\.php?id=31 /01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022 [R=301,L]
RewriteRule ^01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022/?$  /pages/articles/marketArticles.php?id=31 [END]

With the above example when the user goes to /01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022/ the user can view the page. When the user goes to pages/articles/marketArticles.php?id=31 the user is presented the page but is not redirected to the new url.


Solution

  • With your shown samples/attempts, please try following htaccess rules file. Make sure that your htaccess file is residing along side with 01-02-2022 folder(not inside it, along side it).

    Please make sure to clear browser cache before testing your URLs.

    RewriteEngine ON
    ##Rules when user hits /pages/articles/marketArticles\.php?id=31 to redirect TO 01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022/
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{THE_REQUEST} \s/pages/articles/marketArticles\.php\?id=31\s [NC]
    RewriteRule ^ /01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022? [R=301,L]
    
    ##Rules when user hits 01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022 it rewrites to pages/articles/marketArticles.php?id=31 in backend.
    RewriteRule ^01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022/?$  /pages/articles/marketArticles.php?id=31 [NC,QSA,L]