Welcome Guest, Not a member yet? Register   Sign In
Using :any route breaks css/images
#1

[eluser]samwilliamh[/eluser]
Hi,

I'm developing a CMS based on CodeIgniter for a client and the one thing they specifically wanted was the ability to add pages themselves, something which I have not done with CI before. I read through a bunch of topics and the user guide and added a route to routes.php:

Code:
$route['home/:any'] = "home/index";

The index function in the home controller then loads the layout and the page content. However, the css and images (along with css images) do not work on most pages. For example, they work on:

localhost/fhs/home

but not on:

localhost/fhs/home/test

and all pages like the above. The css and images are both located here:

localhost/fhs/css
localhost/fhs/images

In the /home page they load fine with the above URLs but with any page with a segment in the URL after /home/ it tried to load the css and images from:

localhost/fhs/home/css
localhost/fhs/home/images

Is there another route I can add to fix this or a htaccess rule? I've had a go at it but can't seem to fix it. Any help is greatly appreciated.
#2

[eluser]Dam1an[/eluser]
Whats the code you use to include css/images?
You'd want to include the base_url via the URL function, like so
Code:
<link rel="stylesheet" href="<?php echo base_url()?>css/reset.css" media="screen" type="text/css" />
#3

[eluser]samwilliamh[/eluser]
That really isn't an option unless it is the only option because I would have to do the same for images as well and since it is a CMS the client would be uploading images.
#4

[eluser]mattthehoople[/eluser]
You could define a base href for the page so all your urls are absolute...

<head>
<base href="http://localhost/fhs/" />
</head>

<body>
<img src="images/an_image.gif" />

</body>
#5

[eluser]samwilliamh[/eluser]
It works, thanks so much mattthehoople!
#6

[eluser]Aken[/eluser]
This could also be resolved by making sure your .htaccess doesn't not redirect any existing files to the CodeIgniter index.php.
#7

[eluser]NateL[/eluser]
Yes, I use .htaccess to fix this issue:

Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

That code allows for images, css, and a js directory. You can add anything else to that Smile
#8

[eluser]Phil Sturgeon[/eluser]
You are having exactly the same problem as this post that I answered about 5 minutes ago.

Code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Also, anyone who doesn't use that .htaccess rule is making their own life difficult. This means a HTTP request only goes through to CodeIgniter if it is not a file or directory. Why bother manually adding every single possible file or folder in?
#9

[eluser]Dam1an[/eluser]
[quote author="Phil Sturgeon" date="1252686216"]
Code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Also, anyone who doesn't use that .htaccess rule is making their own life difficult. This means a HTTP request only goes through to CodeIgniter if it is not a file or directory. Why bother manually adding every single possible file or folder in?[/quote]

Just to be awkward, I'd like to argue against that Smile
If you integarte something else with a CI site, such as WP in my case, then I don't have a file or directory called my-category, but I don't want CI to touch it Wink
#10

[eluser]Phil Sturgeon[/eluser]
You're WordPress installation should have its own .htaccess file that will handle the files accordingly. If it is really an issue add WordPress as an exception and keep the rules in for everything else:

Code:
RewriteEngine on
RewriteCond $1 !^wordpress_install
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]




Theme © iAndrew 2016 - Forum software by © MyBB