Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8337935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:26:17+00:00 2026-06-09T04:26:17+00:00

I have a problem with my Codeigniter routing and hope someone can help. The

  • 0

I have a problem with my Codeigniter routing and hope someone can help. The routing works fine for most part but seems to break when subdomains are involved. I think its to do with the htaccess but im really not sure at all and hoping someone can help.

If I go to mysite.co.uk/a-page/ this works as expected.

If I go to subdomain.mysite.co.uk this also works (I have a separate default_controller loaded for a subdomain).

If someone goes to subdomain.mysite.co.uk/anything this 404’s as expected.

However if by mistake someone goes to subdomain.mysite.co.uk/a-page/ this should also 404 but it does not. Instead it changes to the following URL:

http://www.subdomain.mysite.co.uk/index.php?/a-page/

This then loads the “a-page” function. I want it to 404. The weird thing is the profiler doesnt pick up the URI above. Instead it just think it is “a-page”.

In my config I have the following:

$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$config['url_suffix'] = '/';
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';

I know its not ideal but for the subdomain check I am doing the following at the bottom of my routes.php

if(isset($_SERVER['HTTP_HOST'])){
    $currentURL = parse_url('http://' . $_SERVER['HTTP_HOST']);
    $urlHostPieces = explode('.', $currentURL['host']);
    $subdomainValue = $urlHostPieces[1]; // will either be a genuine value or the domain name
}else{
    $subdomainValue = DOMAIN;
}

if($subdomainValue !== DOMAIN){
    //is subdomain
    $route['default_controller'] = "controller/function/$subdomainValue";
} else{
    //everything else
    $route['default_controller'] = "controller/home";
}

I am using the htaccess by Fabdrol which seems to be quite common when googling. However I added code for a trailing slash and force www.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    # this adds trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ $1/ [R=301,L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]

    # Forces WWW
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]  


</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.

    ErrorDocument 404 /index.php
</IfModule>  

Any ideas how I can 404 the weird variations? Also read that the uri_protocol should be chnaged to REQUEST_URI but that didnt affect it either.

Also I thought about checking uri segment. If it exists then just 404 but even with that the segment doesnt exist. Its as if the extra string doesnt even exist as far are CI is concerned.

Hope I haven’t missed anything. If you need more example code feel free to say.

Thanks for reading and hope someone can help and explain why this is happening.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:26:19+00:00Added an answer on June 9, 2026 at 4:26 am

    The .htaccess file is forcing the url change because of the #Forces WWW section, which is causing your sub-domain routing to break. (I’m assuming)

    Try changing the lines:

    # Forces WWW
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]  
    

    To:

    # Forces WWW
    RewriteCond %{HTTP_HOST} !^(www|subdomain)\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]  
    

    Also, because you are using 301 Redirects (you’ll want to change those to R=302 for Dev Environments), you might still be redirected because your browser has cached the 301. Test on another browser that you haven’t used for a few days, or clear your current browsers cache.

    **Just took a second glance, and the trailing slash section might also cause you issues with Chrome browser given how many redirects you are already going to be forcing. I have experienced this before with CI, where adding/removing trailing slash causes Chrome to mark page as possibly malicious

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with CodeIgniter .htaccess file and hope that somebody can help
I am new with codeigniter. Hope can help to solve problem on codeigniter pagination.
I have enabled the inbuilt logging within Codeigniter. This works fine. However I'm running
maybe you can help me out here. I have an issue with my codeigniter
Im newbie here.I have a problem with codeigniter segment() method.I referred 6th segment of
I'm new in codeigniter I have problem I use my OS X Lion, and
I have a problem right now with CodeIgniter : I use the REST Controller
Here is my problem in codeigniter MVC. Suppose I have 3 models: (users, email
Im using jquerymobile together with codeigniter framework and I'm having a problem I have
I have a strange problem with codeigniter upload class. I use uploadify (http://www.uploadify.com) to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.