Search code examples
regexapache.htaccessurlrewriting

Htaccess rewrite URL with virtual directory and 2 variables


its my URL...

result.php?team=arsenal&player=ospina

I want to like this

mysite.com/virtualdirectory/arsenal/ospina.html

I tried this code.. cant work.. Not Found

RewriteRule ^(.*)/(.*)/(.*).html$ result.php?team=$2&player=$3 [L]

The requested URL /subfolder/arsenal/ospina.html was not found on this server.

Apache/2.2.8 (Win32) PHP/5.2.6 Server at localhost Port 80

Thanks for helps, Best regards!!


Solution

  • ok , I assume you want to change the URI from

     http://www.example.com/result.php?team=arsenal&player=ospina
    

    to

     http://www.example/subdirectory/arsenal/ospina.html
    

    so this is the .htaccess that will do that for you

    RewriteEngine on
    
    RewriteCond %{QUERY_STRING} ^team=(.*)\&player=(.*)$ 
    RewriteRule ^(.*)$ http://www.example.com/subdirectory/%1/%2.html [R=301]
    

    you can test it with htaccess tester here http://htaccess.madewithlove.be/


    and some useful links for documentation and learning:

    http://httpd.apache.org/docs/2.2/rewrite/intro.html

    http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708

    Great Video Tutorials - Understanding Apache and htaccess

    hope that helps