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

htaccess - pretty url with multiple variables


<a href="sky.php?c=something&id=9&s=anything">sky</a>

something, 9, anything are variables
in address bar I want to see this:
...host/something/9/anything so - without sky.php at all

reverse rule - from pretty to ugly - is solved - but if you have any notice - pls do:

RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /sky.php?c=$1&id=$2&s=$3 [L]

Solution

  • With your shown samples, please try following. This considers that you want to hit URL like: http://localhost:80/sky.php?c=something&id=9&s=anything in browser which should be redirected to http://localhost:80/something/9/anything here.

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

    RewriteEngine ON
    ##This rule handles to change from non-friendly URL to friendly URL in browser.
    RewriteCond %{THE_REQUEST} \s/(?:[^.]*)\.php\?c=([^&]*)&id=([^&]*)&s=([^\s&]*) [NC]
    RewriteRule ^ /%1/%2/%3? [R=301,L]
    
    ##This rule internally rewrites friendly url to non-friendly url.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ sky.php?c=$1&id=$2&s=$3  [L]