Search code examples
phpapache.htaccessmod-rewriteroutes

Remove query string parameter to create a more user-friendly URL


I have this .htaccess file and I have no knowledge with mod_rewrite,

RewriteEngine On
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php  [L]

What I want to achieve is to have localhost/viewticket/${id} instead of localhost/viewticket.php?id=123

I have tried many .htaccess rules but none worked except this one that hides .php in my URL.


Solution

  • I created this one for you, just erase yours and copy this one enjoy^^

    IndexIgnore */*
    
    Options FollowSymLinks
    AddDefaultCharset utf-8
    
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteRule ^viewticket/([^/]+)/?([^/]*)/?([^/]*)$ viewticket.php?id=$1&param2=$2&param3=$3 [L]
        RewriteCond %{REQUEST_FILENAME}.php -f
        RewriteRule ^(.*)/?$ /$1.php  [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-l
        RewriteRule . index.php [L]
    </IfModule>```