Search code examples
apache.htaccessurlmod-rewriteurl-rewriting

.htaccess rewrite query string as path url


I searched for this question but only came across very specific answers that I couldn't tailor to my requirements.

My URL now looks like this: https://example.eu/?action=changepassword and I want it to look like this: https://example.eu/changepassword so text ?action= gets deleted.

I tried to adapt this but it didn't work.


Solution

  • With your shown samples, please try following htaccess rules file. Please also make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    ##External redirect rules here....
    RewriteCond %{THE_REQUEST} \s/?\?action=(\S+)\s [NC]
    RewriteRule ^ %1? [R=301,L]
    
    ##Internal rewrite rules to handle query string in backend.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /?action=$1  [L]