开发者

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


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


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜