CodeIgniter .htaccess and base_url() issues - php

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.

Related

is "root web server" the same as localhost? installing CodeIgniter

I'm trying to install CodeIgniter (I have Mac). I have no experience with Frameworks, but I've read a little about MVC. My goal is to get CodeIgnitor set up so I can use it.
so I download CodeIgniter, then in one tutorial I'm told the following: Copy the file to your server’s web root folder, usually /var/www/ !
In this tutorial: http://www.phpeveryday.com/articles/CodeIgniter-Introduction-to-CodeIgniter-Framework-P147.html
I am told to "Open your root web server" and put the folder inside.
What is the "root web server"? Is it my htdocs? Where am I supposed to put this folder?
I put the folder into my HTDOCS folder because thats where all PHP files go, but I'm not sure if I did it right.
Please, install the easiest thing in the world XAMPP
find folder xampp/htdocs (desired web root folder) and create folder in it (project name), unzip/unrar/un7z CI in there so your folder structure is going to be
xampp/htdocs/project_name/application
/system
/index.php
Open up project_name/application/config and go thru all files see them one by one so you know what CI can and can not do it is well explained inside each file what every setting does.
If you are new to CI follow this video tutorial
if you are looking for good .htaccess file grab this one and change for whatever you need
note: this file should be in /project_name
note2: also in config/config.php set index_page to $config['index_page'] = '';
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /<your project name>
#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>
after changing .htaccess and config/config.php your urls are "nice"
localhost/project_name/controller/method/parameter1/parameter2
finally: it is CodeIgniter not CodeIgnitor

MAMP Pro / CodeIgniter Pathing Issue

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">

codeigniter: why is that everytime i set a specific routing rule that page's css/js don't get loaded?

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.

CodeIgniter + FB PHP SDK + mod rewrite to remove index.php = disaster

I added the following to my CodeIgniter project to my .htaccess in order to remove the index.php:
#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]
Everything worked fine. My URLs no longer need index.php in the.
However, this has created an interesting side effect such that when I try to login using Facebook OAuth, I get the following error:
The webpage at
=">https://www.facebook.com/dialog/oauth?client_id=123&redirect_uri=http%3A%2F%2Fwww.domain.com%2Fsign_in%2Ffacebook%2F&state=8f3d1e5de60e47935460564a41f35af3&scope=email#=
has resulted in too many redirects. Clearing your cookies for this
site or allowing third-party cookies may fix the problem. If not, it
is possibly a server configuration issue and not a problem with your
computer.
NOTE: client id and redirect URI have been changed for privacy.
I'm using the PHP SDK 3.1.1 https://github.com/facebook/php-sdk and followed the sample code on http://developers.facebook.com/blog/post/503/. When I still had index.php in my URLs, everything worked fine. The problem seem to have started after I added the the rewrite rules to remove index.php.
Any ideas what's going on and how to fix this?
Try to following patch:
In system/core/config.php file, in site_url method replace the following line
return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix;
with new code:
return $this->slash_item('base_url').$this->_uri_string($uri).$suffix;
It might get working...

Installing CodeIgniter on root and WordPress in sub-directory

I want to create a website where the main pages will be served from CodeIgniter. I will use Wordpress in the /blog/ sub-directory to host the blog. Thats it! I want nothing else. Just to make sure that:
example.com/somepage/ calls a CI controller where as example.com/blog/some-post/ is handled by Wordpress.
I don't need any kind of integration or interaction between CI and WP.
Is it possible to install in that way? If not, any workarounds so that I can achieve the objectives?
Thanks and Regards,
Masnun
I suspect you could get this to work using an .htaccess file in the root directory of your site. If blog is the name of the subdirectory where WordPress is installed, and you want example.com/blog to be handled by WordPress, try this to see if it helps:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|blog)
RewriteRule ^(.*)$ /index.php/$1 [L]
+1 with Rich's method
Additionnaly, if you're using a .htaccess file like the one suggested on CI doc, it should work by dropping WP directory directly into web root dir.
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]
#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
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|robots\.txt|corporate|assets)
RewriteRule ^(.*)$ index.php/$1 [L]
Because of the RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d any call made directly to a real file on webserver would be served directly by Apache, other uris wil be handled by CI's routing mecanism.
Notice we used a last RewriteCond directive to exclude calls to certains files, including our assets dir (containing images/css/js static files) and 'corporate' dir which contains in this case a blog.
Although this example do not use wordpress, it has its own url-rewriting and routing system.
In a matter of conclusion, you'll have use a specific RewriteCond statement if you want to use WP url-rewriting, if not it could work without it.
It should work just as you described it without any complicated configuration. Just install codeigniter in your root directory and then create a blog directory and put wordpress in there.

Categories