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.