.htaccess

How to hide params passed in url using .htaccess


I have a peculiar situation, my website is like

www.example.com/page.php?url=homepage
. I am trying to use .htaccess file to make url look like

www.example.com/page/homepage/

There by hiding the ".php?url=" part in the url. is this possible? please suggest.


Solution

  • Try this:

    <IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    
    # /anything/anything -> anything.php?url=anything
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([A-Za-z0-9_])/([^/]*)$ /$1.php?url=$2 [L]
    
    </IfModule>
    

    If the page.php filename will always be the same, then do it like this:

    <IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    
    # /page/anything -> page.php?url=anything
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^page/([^/]*)$ /page.php?url=$1 [L]
    
    </IfModule>