CodeIgniter CSS File Not Included - mod-rewrite

I have a bit of an odd problem with my CodeIgniter installation, I am using modrewrite in order to shorten my URLs, for example I have an api page:
http://www.mydomain.com/api
And it works really well, my .htaccess file looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
But whats really strange is if I type a trailing slash:
http://www.mydomain.com/api/
The API page loads ok but it doesn't include the Base CSS file which is included on all the pages, it would be fine if it didn't load anything at all, but I need to stop it from loading the actual api page. Any advice would be helpful,
Thanks!
UPDATE: I have found that when I view source my CSS file comes from the following when the CSS loads correctly:
http://www.mydomain.com/css/all.css
But when it fails to load it comes from
http://www.mydomain.com/api/css/all.css

Make sure that you have a trailing slash in your base_url in the config.php file:
$config['base_url'] = "http://www.mydomain.com/";
And always call the base_url() when dealing with links...etc
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/all.css">
EDIT:
Please note that I'm using the mod_rewrite from this wiki page BUT since my CI installation in not on the root (inside /ci173/) the RewriteBase is not / but /ci173/.

When using relative URL path like css/all.css or ./css/all.css (both are equivalent), you need to be aware that relative URL paths (like any relative URLs) are resolved using a base URL that is the URL of the current document if not specified otherwise.
So in your case /api or /api/ is the base URL path and the relative URL path css/all.css is resolved differently depending on the base URL path:
/api + css/all.css → /css/all.css
/api/ + css/all.css → /api/css/all.css
To conquer this you either need to
use absolute URL paths like /css/all.css that are independent from the base URL path
specify a different base URL in your HTML document (e.g. /); but note that this will affect all relative URLs and not just relative URL paths
adjust your relative URL paths to suite your base URL paths:
for /api use css/all.css
for /api/ use ../css/all.css
for /api/foo/bar/ use ../../../css/all.css, etc.
I guess the first solution is the easiest.

Related

Image relative and absolute path and .htaccess

Hy all,
I'm playing around in Codeigneter and trying to make some kind of photo album just for fun ( to understand codeigneter better before i start for real ).
Now did i set up a test website here:
http://foal.scriptsenprogs.nl/
Now the thing is that users can upload an image in the future. And i want that image to be some kind of save...
So if somebody use this link:
http://foal.scriptsenprogs.nl/media/img/albums/1.png
It should be blocked (like it is now).
I've got this line of code in an .htacces file that is placed in /media/img/albums folder
<Directory "http://foal.scriptsenprogs.nl/media/img/albums" >
Options Includes
AllowOverride All
Order allow,deny
Deny from All
</Directory>
I must confess that i have little know-how about .htaccess files, but this seems to work ( the direct url is blocked ).
In my codeIgneter code i use this to add an image to an webpage:
$str .= '<div style="margin-top:20px;"><img width="200px" src="./media/img/albums/' . $imageStuff[$row['album_id']] -> img_id . '.' . $imageStuff[$row['album_id']] -> img_type . '" /></div>';
And before the .htaccess file, it worked. But now it isn't working.
If i check chromes error console i see this error:
GET http://foal.scriptsenprogs.nl/media/img/albums/1.png 404 (Not Found) foal.scriptsenprogs.nl:48
So an 404 Not Found error it is then. But if you check the URL you see that it is conferted to an Absolute URL? But i inserted an Relative URL right?
Any thoughts of you guys on how to fix this?
EDIT 1
In de webroot of the subdomain, i've got the following .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|school|media|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
EDIT 2
To clarify the question something more...
If your on the website ( http://foal.scriptsenprogs.nl/ ) the image must be showable done by using an relative path. ( like this: <img width="200px" src="./media/img/albums/1.png" /> )
But if your trying to acces the image through an absolute path, it must be blocked ( the link will be something like this then: http://foal.scriptsenprogs.nl/media/img/albums/1.png )
Sometimes CI and relative urls don't play nice together, try using the base_url function
'src="'.base_url().'media/img/albums/'
You could add a new rule to your existing webroot/.htaccess file.
Place the following after the RewriteEngine On line towards the top of your file.
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?yoursite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png|jpg)$ /404.html [L]
This restricts access to any jpg,gif, etc to the domain http://yoursite.com, other requests are redirected to a 404 page.
NOTE: If you are testing this on a local server, you will be on the same domain as your site so it will most likely seem like nothing is happening.

mod_rewrite to parent directory

I would like to create a .htaccess file that would do this logic:
If the requested file is not found try to find it in the directory
above.
I don't want to redirect the browser I would just like to internally rewrite the request.
I tried and searched for this a lot but always got stuck because (as I gather from the log) the where I could do the rewrite the path was always already without its per directory prefix. In the example below the .htaccess file is in the lang folder. If the lang specific file is not found it should just take the file from the parent folder. I understand that it is possible to do it by hardcoding the parent directory or by placing the .htaccess higher, but now that I suffered for so long in trying I would be very interested to learn if it was possible at all this way.
strip per-dir prefix: X:/localhost/htdocs/peopletest/public/img/root_cli/lang/en/loginhead.gif -> en/loginhead.gif
applying pattern 'somePattern' to uri 'en/loginhead.gif'
Thanks for the help.
SWK
Like this?
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-l
RewriteRule /[^/+]/([^/]+)$ $1

Problem with mod_rewrite and relative paths of included css, js, image,... files and links

First of all - I have searched quite a bit for an answer in stackoverflow and via google but haven´t been successfull so far to find a possible solution any advice would be greatly appreciated.
problem is:
I have a page
www.mypage.com
and controll the language displayed via a GET parameter lang
-> www.mypage.com/index.php?target=1&lang=en
Now a client has registered a domain at united-domains (www.mypage.it) and wants me display the italian version of the page whenever the domain www.mypage.it is requested.
The provider united-domains offers a solution called URL-HIDING which basically seems to pass the the request to a URL+folder structure I provide (like www.mypage.com/lang/it - according to the specifications of united-domains it has to be a folder structure and may not be a file)
solution so far:
Calls to the domain: www.mypage.it will be maped to www.mypage.com/lang/it/ via the URL-HIDING option.
there an .htaccess file rewrites the REQUESTS to the actual target:
RewriteEngine on
## check if query string contains NOT 'lang='
## (lang might be changed by user after initial request)
RewriteCond %{QUERY_STRING} !lang=
## redirect to page
RewriteRule ^(.*)$ http://www.mypage.com/index.php?target=1&lang=it
## check if query string contains 'lang='
RewriteCond %{QUERY_STRING} lang=
## Keep the existing query string using the Query String Append flag
RewriteRule ^(.*)$ http://www.mypage.com/index.php? [QSA]
Problem is of course: the CSS, javascript, image,... files are included with an relative path
<link href="scripts/style.css" rel="stylesheet" type="text/css">
<script src="scripts/media.js" type="text/javascript">
<img src="images/logo.jpg">
PDF
therfore the relative request scripts/style.css is mapped to http://www.mypage.com/lang/it/scripts/style.css
I am sadly stuck the the URL-HIDING mechanism of united-domains and changing every relative path the /scripts/media.js is not an option as I´d have to change quite a bit of code.
Does anyone have a solution for this (my .htaccess knowledge is not the best I am afraid)
Couldn´t I just remove lang/it/ from EVERY request and additionally check if the QUERY_STRING contains the string lang= and if it does simple add ?lang=it
thanks to all that have taken the time to read so far - if anyone has a suggestion I´d be more than grateful !
stay well,
matthias
This seems to do the trick !
in folder:
www.mypage.com/lang/it/
I put:
RewriteEngine on
## redirect if just folder is requested
RewriteCond %{REQUEST_URI} ^/lang/it/$
RewriteRule ^(.*)$ http://www.mypage.com/index.php?target=1&lang=it
## otherwise redirects if file does not exist
RewriteCond %{DOCUMENT_ROOT}/lang/it/$1 !-f
RewriteRule ^(.*)$ http://www.mypage.com/$1 [QSA]

RewriteRule for Javascript/css versioning

I wrote a utility to handle the versioning of my CSS and JS files for caching purposes, however, I'm struggling to get the RewriteRule setup correctly to load the original file.
The way the versioning utility writes the new URL is as below:
Local
<script src="20110125/contact.js" type="text/javascript"></script>
Global
<script src="../Scripts/js/20110125/core.js" type="text/javascript"></script>
My RewriteRules strip out the timestamp and load just the path and filename. They are as follows:
#rewrite core js
RewriteRule ^(\/Scripts\/[a-z]*\/)[0-9]*\/(.*) $1$2 [NC]
#rewrite directory level js
RewriteRule .+\/(.+\.js) $1 [NC]
However, when I make a page request I get a 404 on the pages. Any help is appreciated.
You have a rather bizarre path to your JS files; I'd recommend just using /js or /javascript, and don't use capital letters in file or path names in a URL. What are you trying to do with your second RewriteRule?
# /js/123456/jquery.js to /js/jquery.js
RewriteRule ^/js/[0-9]+/(.+).js$ /js/$1.js [L]

Ko3 - URL Rewriting problem - Removing index.php

I'm developing a website using Kohana 3 (1rst time I use a framework). Locally, everything works perfectly. At the moment, I have a default template controller, a multi-language support and my 'index.php' is correctly removed. So before going further, I tested if it worked on my server and I got an endless loop.
I followed the tutorial from the unofficial wiki for the multi-language implementation: http://www.kerkness.ca/wiki/doku.php?id=example_of_a_multi-language_website
A redirection to the default language occurs if the language is not specified in the uri so I figured the problem might have come from there even though it worked locally, so I removed it to see what happens without the redirection. Now, I can see my home page, but whatever the uri is in the web browser, the home page will always be called. I inserted the following line in my home view to check what the uri was:
request::instance()->uri() and effectively, the uri is always: /en/home/
I put the index.php back (in the bootstrap) and everything worked fine again, even with the redirection to the default language.
My first guess was that the uri isn't rewritten correctly, so I tried to change the .htaccess but no success...
Here's my .htaccess:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /dev/
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(application|modules|system)/ - [F,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
(btw I also tried the other RewriteRule in the unofficial wiki, doesn't work either)
Additional info:
Host: WebHostingPad
Apache: v2.2.11
PHP: 5.2.9
Rewrite_Module is activated
Thank you, I would really appreciate your help because I've been trying to fix this for days now and it's really starting to annoy me ;)
The only thing you have to change in order to get rid of index.php in URL is to set the 'index_file' param in Kohana::init ( bootstrap.php ) to FALSE ( everything else can cause an error ).
So the Kohana::init looks like this;
Kohana::init(array(
'base_url' => '/',
'index_file' => FALSE,
));
If it worked with the original .htaccess, there's no need to change it at all.
The problem came from $_SERVER['PATH_INFO'] which returned no value...
This issue can be solved by adding the following line to the php.ini:
cgi.fix_pathinfo=0

Resources