Search code examples
.htaccessurl-rewritinglitespeed

htaccess - hide file extension and keep only value of a variable


original url - example.com/art.php?a=lorem
want to be visible as - example.com/art/lorem
litespeed server - htaccess is enabled

RewriteEngine ON
RewriteRule ^art/(.*)$ /art.php?a=$1 [L]

doesn't work
url is still - example.com/art.php?a=lorem
pls help


Solution

  • You just have a rewrite rule to rewrite example.com/art/lorem but you are missing a redirect rule to redirect example.com/art.php?a=lorem to example.com/art/lorem.

    You may use:

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} \s/+art\.php\?a=([^\s&]+) [NC]
    RewriteRule ^ /art/%1? [R=301,L]
    
    RewriteRule ^art/([\w-]+)/?$ art.php?a=$1 [L,QSA]
    

    THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1

    References: