Search code examples
regexapache.htaccessmod-rewriteurl-rewriting

Modify code htaccess to accept parameters via URl


Modify code to accept parameters via URl. Rules to redirect URLs to www.site.com/acessorios.php tried are as follows, where .htaccess rules file is present along side with app folder.

RewriteEngine ON
RewriteBase /app/views/

##External redirect rules from here.
RewriteCond %{THE_REQUEST} \s/app/views/([^.]*\.php)\s [NC]
RewriteRule ^ /%1? [R=301,L]

##Internal rewrite rules from here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]*\.php)/?$ /app/views/$1 [QSA,L]

Samples:

www.site.com/accessories.php?id=1
www.site.com/accessories.php?id=1&name=Tiago
www.site.com/accessories.php?id=1&name=Tiago&image=foto.png

Samples:

www.site.com/accessories/1
www.site.com/accessories/1/Tiago
www.site.com/accessories/1/Tiago/foto.png

This order may be different


Solution

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

    RewriteEngine ON
    RewriteBase /app/views/
    
    ##External redirect rules from here....
    RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\d+)\s [NC]
    RewriteRule ^ /%1/%2? [R=301,L]
    
    RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\d+)&name=(\S+)\s [NC]
    RewriteRUle ^  /%1/%2/%3? [R=301,L]
    
    RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\d+)&name=(\S+)&image=(\S+)\s [NC]
    RewriteRule ^  /%1/%2/%3/%4? [R=301,L]
    
    
    ##Internal rules from here onwards....
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /app/views/$1.php?id=$2&name=$3&image=$4 [QSA,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /app/views/$1.php?id=$2&name=$3 [QSA,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)/([^/]*)/?$ /app/views/$1.php?id=$2 [QSA,L]