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.