I am running into an odd pathing problem in CodeIgniter/MAMP Pro. I enabled nice URLs in CodeIgniter (hid index.php from URL) by setting $config['index_page'] = ''; in config.php and by adding the following to .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|js|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
My app was working fine, however I am not able to hit my CSS and JS files for some reason. If i type in http://mysite:8888/js/jquery.js I get a CodeIgniter 404 page. Any idea on why this might be the case?
I use this .htaccess for CodeIgniter projects on MAMP. It supports subfolders too:
<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]
#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]
</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.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
.htaccess files are read dynamically and should not need a MAMP restart, unless you adjusted apache's setting files in it's essence.
The robots.txt is not really secret file, so it's not really of importance to mask it out.
The scripts/images/etc. folders you should just add an index.html file, like CodeIgniter does in it's systems folder too. This way users can't browse those folders. Don't forget to copy index.html files into the subfolders too tho. This may seem dirty, but will be a lot cleaner than cluttering your htaccess with even more rules.
I think the above answer is good, but since CI is dynamically writing URLs, I prefer this method. Also, I think this helps with writing better template code. (I tested this specifically on MAMP.)
For one, set the .htaccess file to the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Create a new helper in application/helpers. I call mine assets_helper.php but call it whatever is useful. Put this code in that helper file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('asset_url()')) {
function asset_url() {
echo base_url().'assets/';
}
}
Add the helper to your autoload file in config/autoload.php a la
$autoload['helper'] = array('url', 'assets');
(You may not already have the url helper activated, but it's common.)
And also add a route for the assets folder (in config/routes.php):
$route['assets/(:any)'] = 'assets/$1';
Now when you want to add css, or js or images, you just need to put <? assets_url(); ?> in the template code.
<img src="<? asset_url(); ?>images/logo.png" width="100" height="100" />
or
<link rel="stylesheet" href="<? asset_url(); ?>css/house.css">
Related
I was wondering, how i could give access to the public to files placed within a folder called 'Sample' which is placed inside /applications/views/ .
I tried to to do it using .htaccess file.
This is the .htaccess file that im using right now.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /0.9.1
#Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#Enable access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|application\views\Sample\/public)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Any help would be appreciated. Thank you.
Are you placing your images, css, js etc in your application folder? If you are stop right there. The application and system folder don't allow direct script access and it should be kept that way. Anything you want to make public you should put into a folder called something like public and place that in root directory alongside your application and system directories.
I started working on CodeIgniter recently and I'm having some issues with the .htaccess and the base_url() function causing issues. I hope this is just a noob error and has a quick fix!
Pretty much what happens is that now with the .htaccess the index.php is gone from the url which is amazing but now I am having issues linking stylesheets and js files with my templates.
In all the tutorials I have read/watched, they have included the .htaccess and then still set their base_url() and then using "link_tag(href);" to link their .css but now I am getting a double up on the url. For instance when I tried to add the stylesheet, the path that it looked for was:
http://www.mysite.com/site/www.mysite.com/site/css/stylesheet.css
So my first thought was just to remove my base_url() so that the .htaccess does everything but it doesn't work. With the .htaccess I can have a site without the index.php being there but without stylesheets or JavaScript files linked or I can have a site with some styling, but deal with the index.php. There must be a way that both will work simultaneously?
Also, I did a fix by instead of using the link_tag() I just echoed out a normal link tag which works perfectly in the beginning when its still:
http://www.mysite.com/
But if the login is incorrect and I redirect to load the login view again, the styling disappears because its now using the relative path from:
http://www.mysite.com/user/login
Thanks in advance! Any help would be appreciated.
Try
RewriteEngine on
RewriteCond $1 !^(css|js)
RewriteRule ^(.*)$ /index.php/$1 [L]
So www.mysite.com/css/mycss.css is retrieved normally where as www.mysite.com/something is passed to codeigniter.
To get the css file with base_url() you would do base_url("css/mycss.css"), which should result in "http://www.mysite.com/css/mycss.css"
A better .htaccess ruleset would be:
#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]
#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]
This disallows access to your system and applications folder, but allows access to all other files, for example: /js/*, /css/*, /img/*, etc.
I'm building my website in codeigniter, and here an example of a routing rule that's giving me some trouble:
$route['updategamepage/(:num)'] = 'maincontroller/main/updategamepage/$1';
So is there anything wrong with the format/structure of the rule? Here is an example url that uses that rule:
http://www.mydomain.com/ci_website/updategamepage/6
and when that page gets loaded, the css/js don't get loaded with the page...any idea on whats going wrong?
Your routing rule should only apply to things that get routed through CodeIgniter's index.php file. That routing is dictated by the application's .htaccess file. It's possible that your htaccess is redirecting requests to your .css file to CodeIgniter when you don't want it to.
Ultimately you may way to check your web server logs, including potentially enabling mod_rewrite logging, to see what's actually going on.
Here is an example .htaccess that I use for my CodeIgniter apps:
(Notice you will have to change the RewriteBase directive near the top)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /app/
#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]
#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]
</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.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
Here's a short .htaccess snippet I always use in my projects, it's much easier to understand.
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|shared|uploads|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
what it does is that it redirects every url that doesn't contain index.php, assets, shared, uploads and robots.txt right after the URL to your public folder to your index.php file allowing you to set your $config['index_page'] value to '', that will make your URL's much more pleasant for the eye and search engines. Without this you had URL's like http://www.mysite.com/index.php/pages/about, with it you will have http://www.mysite.com/pages/about.
Personally I keep my JS and CSS files in assets folder, but if you like to keep them in the root folder, simply add their folder names to the second line of the .htaccess file I provided separated with a | sign
RewriteCond $1 !^(index\.php|js|css|shared|uploads|robots\.txt)
As I suggested in my answer to your other question a few minutes ago, you better erase the contents of your $config['base_url'] value and let CodeIgniter do the job for you - he does it really well.
I am new to CI and don't know much about rewrite rules. I was able to find htaccess rules from stackoverflow post which allows me to remove index.php from the URL. The ht access rules are as following
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /siebeluigen/
#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]
#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]
</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.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
But this doesn't work if I move my controllers to subfolder within the controller directly. for example
If I put my Application controller in following path
application/controllers/application.php
Then I can access it from URL
http://localhost/siebeluigen/application
But if my directory structure is
application/controllers/app/application.php
Then I get error with following url
http://localhost/siebeluigen/app/application
but it works like this
http://localhost/siebeluigen/index.php/app/application
So, I am pretty sure there is something in htaccess that I should be able to change to make it work.
Please help!!!
Update
My htaccess file was in the application folder instead of root. Moving the file to the folder were index.php is located solved the issue. I have marked the answer that is working.
Thanks...
This is my htaccess file that I use on ALL my projects:
Options -Indexes
Options +FollowSymLinks
# Set the default file for indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
# activate URL rewriting
RewriteEngine on
# do not rewrite links to the documentation, assets and public files
RewriteCond $1 !^(images|assets|uploads|captcha)
# do not rewrite for php files in the document root, robots.txt or the maintenance page
RewriteCond $1 !^([^\..]+\.php|robots\.txt)
# but rewrite everything else
RewriteRule ^(.*)$ index.php/$1 [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>
Then - also make sure that inside your config.php file
$config['index_page'] = '';
edit: you NEVER want to point someone to your application folder - you ALWAYS point them to your index.php - EVERYTHING goes through Index.php - and it does the re-routing to where it is needed.
This is the condition that is usually meant to redirect to application.php
RewriteCond %{REQUEST_URI} ^application.*
app/application doesn't match this condition. Try changing it to this:
RewriteCond %{REQUEST_URI} ^app/application.*
I have made a codeIgniter application. Its working oh so cool on my local, but when I deploy it to the main, all my pages are getting redirected to the home page.
I have the following directory structure:
---www
-----.htaccess
------codeIgniter (the application lives here).
My htaccess reads as follows
RewriteEngine on
RewriteCond $1 ^media
RewriteRule ^(.*)$ codeIgnite/$1
RewriteCond $1 !^(index\.php|codeIgnite|images|robots\.txt)
RewriteRule ^(.*)$ codeIgnite/index.php/$1
And this is my config.php
$config['base_url'] = "http://localhost/codeIgnite/"; // or should it just be http://localhost/ ?
Is there some other config settings I need to change ? Please help.
My home page shows up just fine. Problem is with other pages. No matter what URL I give, I see my home page UI.
Setting the $config['uri_protocol'] = "ORIG_PATH_INFO", fixed the issue.
I've used the provided .htaccess from CodeIgniter and just set the RewriteBase to my subdirectory to get it to work properly.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /codeIgnite/
#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]
#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]
</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.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
You should have to put the .htaccess in the CodeIgniter folder and also you have to modify the last line of your file to this
RewriteRule ^(.)$ /codeIgniter/index.php/$1 or
RewriteRule ^(.)$ /codeIgnite/index.php/$1
You dont have to do nothing in apache config only turn on the extension rewrite_module