yankygo
yankygo

Reputation: 25

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

Upvotes: 2

Views: 646

Answers (4)

Kaiesh
Kaiesh

Reputation: 1052

If you are using Apache you can look into htaccess scripts. There are a ton of options available through a google search

Upvotes: 0

bensiu
bensiu

Reputation: 25584

look at frameworks like (my favorites) CodeIngniter or Zend, but you are not limited to it or try to reinvent the wheel by buid your own router

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191779

PHP can't do this without a redirect, but there are a host of solutions:

Use mod_rewrite with apache (.htaccess)

RewriteEngine on
RewriteRule ^home$ /index.php?page=home

Another would be to add an index.php to /home thats only function is to include the document root index.php.

Upvotes: 1

ngduc
ngduc

Reputation: 1413

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

Upvotes: 2

Related Questions