Search code examples
phphttp-redirectheaderpathinfo

Return PHP page based on directory base-name without redirect


I want the server to return a specific PHP page based on the directory name without a redirect. For example:

http://www.example.com/home

should return the same result as:

http://www.example.com/index.php?page=home

but it shouldn't redirect the address


Solution

  • you need to create a special file called '.htaccess' and put it in your web application root directory. File content could be like this:

    Options +FollowSymLinks
    RewriteEngine on
    
    RewriteRule ^/home$ /index.php?page=home [L]
    

    Then configure Apache (httpd.conf, httpd-vhosts.conf) to allow your web application to use that .htaccess config file

    <VirtualHost 127.0.0.1:80>
        DocumentRoot "/path/to/your/webapp"
        ServerName localhost
        <Directory "/path/to/your/webapp">
            AllowOverride All
                Order allow,deny
                Allow from all
        </Directory>
    </VirtualHost>
    

    This is a good read.

    http://httpd.apache.org/docs/current/misc/rewriteguide.html