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 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.

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.