Search code examples
.htaccesshttp-redirectmod-rewriteurl-rewritingfriendly-url

Htaccess how to add static text to url


I want to make example.com /productName to example.com /product/productName
I just want to add static text /product/ in mid to url how can i add this with .htaccess file to url structure? I already tried this code but it did not worked.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#hiding parameter name and product.php
RewriteRule ^([^\.]+)$ /product.php?productName=$1 [NC,L]

Solution

  • With your shown attempts, please try following htaccess Rules. Please make sure to keep your htaccess along with folder product(not inside product folder).

    Make sure to clear your browser cache before testing your URLs.

    ##Making rewrite engine on
    RewriteEngine ON
    ##rules for external redirect
    RewriteCond %{THE_REQUEST} \s/(product)\.php\?code=(\S+)\s [NC]
    RewriteRule ^ /%1/%2? [R=301,L]
    ##Rules for internal rewrite here.
    RewriteRule ^(product)/(.*)/?$ $1.php?code=$2 [QSA,L,NC]