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

issue with url parameters in htaccess


RewriteRule ^/pages/topic\.php?tag=$1 /topics/([^/]+) [R=301,L]

RewriteRule ^topics/([^/]+)/?$  /pages/topic.php?tag=$1 [END]

My problem is with the 301 redirect, I believe the problem is with the tag=$1 and /([^/]+) I'm not entirely sure I've done this correctly, My desired result is that when the user navigates to /pages/topic.php?tag=cryptocom that the user is then redirected to /topics/cryptocom/ Thanks for your help.


Solution

  • With your shown attempts/samples, please try following htaccess rules file.

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

    RewriteEngine ON
    ##External redirect rules here.
    RewriteCond %{THE_REQUEST} \s/pages/([^.]*)\.php\?tag=(\S+)\s
    RewriteRule ^ /%1/%2? [R=301,L]
    
    ##Internal rewrite rules here...
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^topics/([^/]+)/?$  /pages/topic.php?tag=$1 [NC,QSA,L]