My php controller can't include css files - php

The following issue is driving me crazy, perhaps i am thinking to difficult.
Here is the thing, i developed a small MVC framework that works fine. Atleast without url rewriting.
The problem is that as soon as i use url rewrite, things like the css or images which are included in the templates are directed to the wrong directory.
If i type for example: http://www.domain.com/home then everything is fine and the css file get loaded from the http://www.domain.com/css/ directory.
But when i type: http://www.domain.com/home/ the css file won't be loaded, since its looking for the css files in http://www.domain.com/home/css/ Which is obliviously the wrong directory. It seems it sees home/ as a directory where it looks for the the included files.
If i don't use any url rewriting at all, just by typing: http://www.domain.com/index.php?slugs=home/ then there are no problems at all. So i don't think the problem is caused by my script so thats why i think the problems should be looked for in the .htaccess file.
Here is my htacces file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|css|robots\.txt)
RewriteRule ^(.*)$ /astrostrategy/index.php?slugs=$1 [L]
Is there a way, that home/test/whatever(The segments) are not seen as a directory?
Hope my post made sense, i often think to complicated :P
Already thanx for any help! :)
Gr,
Hermes.

If Death's answer doesn't work you can always set the base tag to the url to use in relative paths

it seems you're using relative to currect directory path for css. when you use http://www.domain.com/home/ browser takes home as directory and css file will be located in http://www.domain.com/home/css/. my suggestion is use relative to root path. Now you're using something like :
<link rel="StyleSheet" type="text/css" href="css/style.main.css" />
it's better to use absolute path or relative to root path.
<link rel="StyleSheet" type="text/css" href="/css/style.main.css" />

Related

A way to avoid Apache Alias path to include HTML resources like CSS

Here comes a question because my current solution is not really satisfying me for the following problem:
I have an own PHP Framework with MVC pattern in development. And my Router works perfect and all but I have one question I could not find a solution for.
Well I route every incoming request to index.php file which is located in the base-path of my framework. Ofcourse it is no problem to work with relative paths when including css such as:
<link rel="stylesheet" type="text/css" href="style/include/css/style.css" />
This works perfect in the browser. Ofcourse it does not matter what I enter in the URL because every request gets redirected as stated above to the index to make a reasonable routing possible.
However when my url contains multiple slashes which look like subfolders, for example: "/manual/details/1_2" then I get a normal routing process but the browser cannot find the css file unless I add "../" for each "/" in my url to map backwards to my base-path.
For example for the above URL this would work instead:
<link rel="stylesheet" type="text/css" href="../../style/include/css/style.css" />
My curent solution:
I wrote a PHP function in my Routing class that determines the required amount of "../" pattern and I always cast the function before implementing a resource to build the exact
path at any time.
Example:
<link rel="stylesheet" type="text/css" href="<?=Router::getInstance()->getSubdirectoryPrefix()?>style/include/css/style.css" />
Needless to say that this is very unhandy and also sucks if you forget to place that function. If your route ever changes or you forget that you are in a subdirectory you will wonder why your resource could not be found.
I also know about putting an alias such as Alias /public style/ in my Virtual-Host configuration of Apache but I want to find another - project and PHP internal way without having an unhandy crap such as pasting the alias function all the time and without setting up a virtual-host option so the framework can stay lightweight and does not require any nasty external options like modifying Virtual-Host.
I'd love to hear your solutions, best would be .htacces - oh and by talking over it I leave my .htaccess code here aswell:
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]
Thanks in advance for your help~
You must use absolute paths instead of relative paths for your html links (css, javascript, images, etc).
For example:
<link rel="stylesheet" type="text/css" href="/style/include/css/style.css" />
The leading slash (before style) means to begin from document root folder, then go into style folder, and so on... (you may have another prefixed directory if it's not in root folder)
You had some problems because some of your rules can create virtual directories (e.g: http://domain.com/some/directory/subdirectory/etc/).
Also, in your htaccess, it wouldn't hurt to use a leading slash (or a RewriteBase)
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /index.php [L]
or (both are the same)
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]

Klein system. CSS isn't working

I'm testing Klein routing system, https://github.com/chriso/klein.php
it's awesome but i can't get my css ant images to run
This is my directory structure:
index.php
.htaccess
vendor
views
includes
assets
css
images
And here's one of my includes line of code where i try to access my assets/css:
<link href="../assets/css/main.css" rel="stylesheet">
I tried everything. Every possible hint would be great, thanks.
EDIT:
now i think it can be problem in my htaccess. how to make that url won't rewrite if it's css or img?
my .htaccess :
RewriteEngine on
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
Your link is most likely wrong (unless the code you pulled it from is in the include or assets folder). the ".." literally means "up one level". So if you code is in the .htaccess folder (which it usually is I believe) or any folder on that level the correct link would be:
href='../views/assets/css/main.css'
If your code is in css or images (not sure why it would be) the correct link would be:
href='../css/main.css'
Good lcuk!

Codeigniter loads html code when trying to load style.css or a img file

This is my first time using a php framework and I haven't found an effective solution.
I've made my page load style tag correctly using base_url() in an asset_helper file, and it seems to work fine.
generated code:
<link rel="stylesheet" type="text/css" href="http://localhost/p_maricarmen/assets/css/style.css" media="screen" />
but it doesn't load the css file.
I have the same problem with others files like loading images with img tag.
I also have a .htaccess file in root directory with code:
RewriteEngine on
RewriteCond $1 !^(index.php|public|robots.txt)
RewriteRule ^(.*)$ /p_maricarmen/index.php/$1 [L]
for the friendly url.
For more information, if put the style url in my navigator, it generate an html page with 404 error, page not founded.
Modify your .htaccess file with the below code.
RewriteEngine on
RewriteCond $1 !^(index.php|public|assets|robots.txt)
RewriteRule ^(.*)$ /p_maricarmen/index.php/$1 [L]
The reason why your css file is not loading is that you've to specify the Rewrite Condition for the folder where your css and js files have been placed.
You can do the same thing for your images folder.
Simply specify the images parent folder name as i've specified for the assets, and it should work as expected.
Hope this helps you...

How to link to files and change base url in CodeIgniter?

Hello I've been running into a few issues with CI lately. When I want to link a css file to pretty much anything, I need to put it directly into the root folder. If I move the file somewhere further in the hierarchy it won't load it. Here's an example of when I am trying to link this core_css.css. It loads the one in the htdocs folder, but not the one in the css folder.
Works:
<link href="<?php echo base_url(); ?>core_css.css" type="text/css" rel="stylesheet" />
Does NOT work:
<link href="<?php echo base_url(); ?>application/OBS/css/core_css.css" type="text/css" rel="stylesheet" />
I've tried putting the css filed in all the other subfolders and linking those, but it only loads the one in the root.
My second problem is that every time I try to test a controller, I need to access it through index.php like this:
http://localhost:8888/index.php/test_controller
Is there a way to get rid of the need to put the index.php in the URL?
For your first problem:
Add .htaccess file in your root web directory and write allow from all it means, in this folder your all files and all folder will not give error Access forbidden! use it like this:
<link href="<?php echo base_url(); ?>application/public/css/style.css" rel="stylesheet" type="text/css" />
N.B:
I usually put all my files like that into an "assets" folder in the application root, and then I make sure to use an Asset_Helper to point to those files for me. This is what CodeIgniter suggests.
For your second problem:
If you are using Apache place a .htaccess file in your root web directory containing the following:
RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
for better understand you can see codeigniter URL section through this link
http://ellislab.com/codeigniter%20/user-guide/general/urls.html
and another one is http://snipplr.com/view/5966/codeigniter-htaccess/
For the second question: put the following in an .htaccess file inside your CodeIgniter root folder (htdocs in your case):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
Make sure to set $config['index_page'] to an empty string, and also set $config['base_url'] accordingly (these two options are inside /application/config/config.php)
For the first question: try to make a folder called assets, or something similar and put it in the root of the site (again inside htdocs). Move your css file there, and try to access it by visiting http://localhost:8888/assets/core_css.css.
Also, in case you are using Linux box, make sure the folders you want to access have read permissions.

Request for resources (CSS, JS) being incorrectly redirected by htaccess rewrite rules

I had a look at a few possible duplicates for this question but rewrite rules are fairly specific to projects so I couldn't find a suitable answer.
Crux of issue? Requests for certain files (CSS, JS & images) are being rewritten (I think) by the htaccess file.
I have an htaccess file set up to direct as follows:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
It takes a URL like localhost/framework/booking/dashboard and rewrites to localhost/framework/index.php?controller=booking&action=dashboard
The relevant file structure inside my local www folder looks something like:
framework
-index.php
-htaccess
-views
-Booking
-dashboard.php
-wuxia-blue.css
The CSS link in dashboard.php is as follows:
<link rel='stylesheet' type='text/css' href="wuxia-blue.css">
The HTTP request being made seems to be:
/framework/booking/wuxia-blue.css
It should be:
/framework/views/booking/wuxia-blue.css
Ideally, I'd like to use something similar to
href="<?php echo(CSS . "wuxia-blue.css"); ?>".
Where CSS is a defined constant in the application and where wuxia-blue.css (and all other css files) are contained in a predefined folder of stylesheets. Obviously this would apply to other assets like .js files and images.
Any ideas?
The htaccess rule is correct - the problem is with your folder structure.
When you create a relative link, it's relative to the current URL as displayed in the address bar - the location of the file you're in isn't relevant.
Your view file is at /framework/views/booking/dashboard.php, but the browser just sees the url /framework/booking/dashboard. That means the browser interprets the location of the resource to be /framework/booking/dashboard/wuxia-blue.css
Personally, I'd recommend just keeping all your css and images outside of the framework in /images and /css. Otherwise, may have to add some code in your framework to serve up image files, since all the image requests will be running through your framework.php file.

Categories