Search code examples
php.htaccesssocial-networking

ReWriting URL for Linking to the page


Almost everyone has gone to Facebook. On Facebook they have a url like www.facebook.com/username but on a site that has their user structure like this: site.com/profile.php?user=username how can you rewrite the link to look like Facebook (ie: site.com/username) so that if you go to site.com/username in your browser, it will actually link to the site.com/profile.php?user=username without only looking like it is rewriting the URL while you are on the page?


Solution

  • This should work:

    RewriteEngine On
    RewriteCond %{THE_REQUEST} ^(GET|POST)\ /profile\.php\?username=(.*)\ HTTP
    RewriteRule ^ /%2\? [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /profile.php?user=$1 [L]