Search code examples
.htaccessurlurl-rewritingget

URL change with .htaccess RewriteRule


I want to change URL in .htaccess but I don't know how to do anyone can help me please.

RewriteRule profile/(.*)$ profile.php?name=$1

My URL : http://localhost/profile/swatalk

Want to make : http://localhost/swatalk

If this is possible, Thank you all :)


Solution

  • RewriteRule ^profile/(.*) /$1 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*) /profile.php?name=$1 [L]
    

    Line 1: (.*) matches any characters. it makes /profile/(.*) redirect to (.*) permanently.

    Line 2: check if the request uri not matches any existing file

    Line 3: check if the request uri not matches any existing directory

    Line 4: then rewrite the request uri to /profile.php?name=$1, $1 is the backreference to (.*)

    The above rule makes http://localhost/profile/swatalk redirect to http://localhost/swatalk, which is handled by /profile.php?name=swatalk