Simple hiding of PHP and custom Routing

Source: Internet
Author: User
Configure HTTP. conf in Apache

# Make PHP look like other programming languages, use PHP to parse files with the following extensions
Addtype application/X-httpd-PHP. asp. py. pl. html. htm

Or set php as the default file.
HTTP. conf-Apache
# Set default file type to PhP
Defaulttype application/X-httpd-PHP
Then configure in PHP. ini-PHP to hide the PHP suffix.
Expose_php = off
And the PHP file is also written as a non-suffix file.
Your URL is www.domain.com/test? Key = Value

The URL configured with addtype above is long as www.domain.com/test.html? Key = Value

Another method is to use index. php In the code to hide the PHP suffix and route it to the corresponding module. The principle is to intercept $ _ server [quert_string] and parse it,
Then include the corresponding module
The result is as follows:
Www.domain.com /? Article/ID/54/type/News & cat = 2
The original URL should be like this.
Www.domain.com/modul_directory/article.php? Id = 54 & type = News & cat = 2


Index. php
--------------
<? PHP

// Parsing query string
$ Qs = explode ("&", $ _ server ['query _ string']);
$ Qs = explode ('/', $ QS [0]);

// If modul is undefined set it to index
If (! $ QS [0]) $ modul = 'index ';
Else $ modul = strtolower ($ QS [0]);

// We can make a variable $ _ Query
// For alternative _ Get
For ($ I = 1; $ I <count ($ QS); $ I + = 2)
{
$ _ Query [$ nvar] = $ nvar = $ QS [$ I];
$ Nvar = $ QS [$ I + 1];
}

// Check the modul is exists?
If (! File_exists ("modul_directory/{$ modul}. php "))
$ Modul = "Index ";

#### This is example
Implementation The Script
// Load the Template
Include ("template. php ");
// Load the module
Include ("modul_directory/{$ modul}. php ");
// Load
Footer
Include ("footer. php ");
?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.