Implement wildcard domain name resolution in CodeIgniter

Source: Internet
Author: User
Tags codeigniter
This article describes how to implement wildcard domain name resolution in CodeIgniter. For more information, see

This article describes how to implement wildcard domain name resolution in CodeIgniter. For more information, see

Recently, a project requires second-level domain names to facilitate SEO. Because CodeIgniter framework is used, although this framework provides flexible routing functions, it cannot implement second-level domain names. After querying a large amount of data, the solution was obtained through several tests. This example uses this fake domain name.

Step 1:

First, create virtualhost in httpd. conf.

ServerAdmin admin@163.com DocumentRoot "D:/www/cms" ServerName ServerAlias * .mysite.com # Here ErrorLog "logs/mysite.com-error. log "CustomLog" logs/mysite.com. log "common

Step 2:

I want to achieve this effect:
====>
To ensure normal access to this domain, you must modify the hosts file.

127.0.0.1 127.0.0.1 category.mysite.com

Step 3:

Modify: system/core/URI. php's _ set_uri_string Method

/*** Set the URI String ** @ access public * @ param string * @ return string */function _ set_uri_string ($ str) {// Filter out control characters $ str = remove_invisible_characters ($ str, FALSE ); // If the URI contains only a slash we'll kill it $ this-> uri_string = ($ str = '/')? '': $ Str; // Add by fengyun for url rewrite at 1:02:27 @ include (APPPATH. 'config/domain '. EXT); $ arrServerName = explode ('. ', $ _ SERVER ['server _ name']); if (in_array ($ arrServerName [0], $ domain) {$ this-> uri_string = '/'. $ arrServerName [0]. "/". $ this-> uri_string ;}}

This is mainly to make the URL correctly understood by CI.

Step 4: Create a domain. php file under application/config. The content is as follows:

<? Phpif (! Defined ('basepath') exit ('no direct script access allowed'); $ domain = array ('category ', "detail", "info", "archive ");

This is basically done. However, when using site_url (), if you want to use a second-level domain name, you have to handle it separately.

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.