This article describes how to implement wildcard domain name resolution in CodeIgniter. For more information, see the CI framework.
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. In this example, the fake domain name www.mysite.com is used.
Step 1:
First, create virtualhost in httpd. conf.
ServerAdmin admin@163.com DocumentRoot "D:/www/cms" ServerName www.mysite.com ServerAlias * .mysite.com # Here ErrorLog "logs/mysite.com-error. log "CustomLog" logs/mysite.com. log "common
Step 2:
I want to achieve this effect:
Http://www.mysite.com/category/news/1.html ====> http://category.mysite.com/news/1.html
To ensure normal access to this domainModify the hosts file
127.0.0.1 www.mysite.com127.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 2013-1-25 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: goCreate 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.