skip to Main Content

So i have a htaccess rewrite that makes a query string to a path like so..

RewriteRule ^page/(.*)$ page.php?query=$1

This works fine and using the above i can access the page via both page.php?query= and /page/query/ but how can i make it so that if the page is accessed by the page.php?query= method, it automatically redirects to the path version?

I’ve tried the following but i don’t think i wrote it correctly…

RewriteCond %{HTTP_HOST} ^www.webaddress.com/page.php?query=$
RewriteRule ^(.*)$ http://www.webaddress.com/page/$1 [R=301,L] 

Any help to fix this would be appreciated. Thanks.

2

Answers


  1. Try it before your internal rewrite rule.

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^(.+).php
    RewriteCond %{QUERY_STRING} ^(.+)=(.+)
    RewriteRule ^ http://%{HTTP_HOST}/%1/%2/%3? [R=301]
    
    Login or Signup to reply.
  2. You can use the following

    RewriteEngine On
    #url redirection from old url to the new one
    #redirect /page.php?query=foo to /page/foo
    RewriteCond %{THE_REQUEST} /page.php?query=([^s]+) [NC]
    RewriteRule ^.+$ /page/%1? [L,R]
    #url rewiring from new url to the old one
    # rewrite /page/foo to /page.php?query=foo
    RewriteRule ^page/(.*)$ page.php?query=$1 [L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search