Search code examples
apache.htaccessmod-rewrite

htaccess rewrite rule: show page without showing filename in url


i have a php script which can show some image based on a QUERY_STRING

example:

example.com/img.php?IMAGE_1

but i want same result to be shown even if user visit

example.com/IMAGE_1

i tried this code from a forum but it is not working

RewriteEngine On  # enables url rewriting

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php  

Solution

  • Have it this way:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([\w-]+)/?$ img.php?$1 [L,QSA]
    

    This rule matches 1+ of any word character or hyphen in an URI that must not match an existing file or directory. If match is successful it converts path to query string with img.php.