Search code examples
phpapache.htaccessurl-rewriting

Apache URL rewrite: remove index.php and query string


I have a link, for example:

https://test.com/index.php?code=myko

I would like to turn it into:

https://test.com/myko

My best attempt so far is:

RewriteEngine On
RewriteRule ^(.*)$ index.php?code=$1 [L]

All answers I could see so far deals with either removing index.php only, or handling the query string only in some fashion, and the above is the closest I could see so far, but is not working properly.

In the case above, it adds index.php as the code. I tried with $0, $1, etc.

Please assist in changing this via .htaccess.


Solution

  • I have found the answer. I needed to add QSA in the square brackets at the end. So instead of:

    RewriteRule ^(.*)$ index.php?code=$1 [L]
    

    I had to do:

    RewriteRule ^(.*)$ index.php?code=$1 [QSA,L]