skip to Main Content

I have a website example.com and I am passing two GET parameters in the url.

example.com/page.php?page=5&section=10

Now I want it to show

example.com/5/10

I’ve already got it to work for the first part but cannot seem to figure out the second part (section part)

My current .htaccess file is

RewriteEngine on

RewriteCond %{THE_REQUEST} s/+page.php?page=(d+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]

RewriteRule ^(d+)/?$ page.php?page=$1 [L,QSA,NC]

All I need to get is the second part working (&section=10)

2

Answers


  1. You can use this

    RewriteEngine on
    
    #redirect and rewrite the single get perm /?page
    RewriteCond %{THE_REQUEST} s/+page.php?page=(d+) [NC]
    RewriteRule ^ /%1? [R=301,L,NE]
    
    RewriteRule ^(d+)/?$ page.php?page=$1 [L,QSA,NC]
    
    #redirect and rewrite urls with multiple perms   
    RewriteCond %{THE_REQUEST} s/+page.php?page=(d+)&section=(d+) [NC]
    RewriteRule ^ /%1/%2? [R=301,L,NE]
    
    RewriteRule ^(d+)/(d+)/?$ page.php?page=$1&section=$2 [L,QSA,NC]
    
    Login or Signup to reply.
  2. Add this to your .htaccess:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} page=(.+)&section=(.+)$ [NC]
    RewriteRule ^ /%1/%2? [R=301,L,NE]
    

    This grabs the variables using {QUERY_STRING} and rewrites your URL to http://example.com/5/10. The use of the ? on the end of the rewrite is to stop the query from appending onto the end after the rewrite.

    Make sure you clear your cache before testing this.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search