CodeIgniter 3 : Rewriting URL with assets - php

I'm facing a problem with URL rewriting in CodeIgniter 3.
My files structure is :
site
-assets (.css and .js files)
-application (sites files : controller, ...)
-system (CI files)
-user_guide
To avoid index.php/ in URL, I put this configuration :
.htaccess
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
config.php
$config['index_page'] = '';
This way, my URL is not test.com/index.php/user/function but test.com/user/function.
Actually everything is working very well, but when some .js scripts try to call other scripts with relative URL, it calls wrong URLs.
For example, if one of my script wants to call assets/js/jsfile.js, I'm having a 404 error because it calls http://www.test.com/user/assets/js/jsfile.js, whereas it should be http://www.test.com/assets/js/jsfile.js.
To avoid this, I put <?php echo base_url() ?> in every <link> or <script> I call myself, but some .js libraries require intern callings, and I can not do this every time... I would like the real solution... Any idea ? Thanks a lot !

Change your htaccess file like this;
RewriteCond $1 !^(index.php|resources|robots.txt|assets)

Related

MVC pattern - Show only controller name in URL (hide index.php and action)

I'm using a Hosting Linux service provided by Aruba to run my website.
At this moment, my website URL design looks like this:
mysite.it/index.php?controller=&action=
and I want to hide index.php and the action parameter so my uri looks like:
mysite.it/controller
My basic folder structure is:
www.mysite.it/
config/
controller/
lib/
model/
template/
.htaccess
index.php
Is there a solution to achieve this?
Also, I'd need to keep the original document.URL, because for example in some .js there are functions that search for the action parameter in the URL.
Thanks in advance.
You could include the following text in your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Keep in mind that the rewrite engine must be active in your web server config.

Removing index.php from Code Igniter

I've done a search for my question but have yet seen one similar to mine.
I have a newly installed CI on my server, for security reason I have shifted the "application" and "system" folder outside of public_html and into a folder called "app". I tried a lot of methods that the forums were using but none seem to help.
Below is my folder structure, would like to know what to type for the .htaccess and where does this .htaccess go to?
/
app (system and application folder for CI resides here)
public_html (index.php - the one which defines ENVIRONMENT, styles, other htmls and my current .htaccess resides here)
My current .htaccess looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$1 [PT,L]
Please help! Thank you for your time!
EDIT
Currently my url is something like this
xxx.xxx.xxx.xxx/~app/index.php/user/check/id
(it's on a shared hosting but not linked to domain yet and hence the ip address and partition in the url.)
I want to achieve something like
xxx.xxx.xxx.xxx/~app/user/check/id
and it should work when I link up my domain. (e.g. www.mydomain.com/user/check/id)
Alright, I think I've solved the problem.
This has most probably got to do with the 'unique' url that I have. I assume if it is a normal domain name it shouldn't have so much problems.
My url > xxx.xxx.xxx.xxx/~app/index.php/user/check/id
My directory structure remains the same >
/app (application and system directories goes here)
/public_html (index.php and .htaccess goes here)
.htaccess code as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~app/ << NOTE the addition
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
In config.php, set
$config['base_url'] = 'http://xxx.xxx.xxx.xxx/~app/';
$config['index_page'] = '';
That's all!

Url rewriting HTML and CSS paths

I have the following .htaccess file in the root of my application:
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This basically just convert paths such as index/var=value/var2=values2/ into index?url=index/var=value/var2=values2/ and in index.php they are elaborated to fetch $controller which is the name of the controller called (in this case index) and the $get which is the substitute for $_GET. (In this case it contains: $get['var'] = 'value' e $get['var2'] = 'value2').
Notice: In the root file, where the .htacces file and the index.php are, we have also the application/ folder (which contains controllers, views, and models), the system/ folder which initialize the framework and the public/ folder which should contains every image/javascript/css/video/audio file which can be accessed.
The .htaccess allows me to access directly every existing file both images and css. Which is great. The only problem that occurs is when the path to that image/javascript/css/audio/video file is inside the HTML or CSS files. So that when you call (*1) <img src="public/img/image.jpg"/> or when you call (*2) background-image: url(public/img/image.jpg); it just doesn't work.
That could be "solved" using absolute paths such as http://dominio.com/public/img/image.jpg but it is tricky when it comes to javascript files. And anyway is not a solution at all, but just a way to bypass the problem.
How could I make (*1) and (*2) work?
Have you tried this path?
/public/img/image.jpg
EDIT/ADD
didn't test this, but you can try
RewriteCond %{REQUEST_URI} ^/public [OR]
RewriteCond %{REQUEST_URI} ^/other_folder [OR]
RewriteCond %{REQUEST_URI} ^/more_folders
RewriteRule (.*) /project/$1
OR, have index.php process everything, and the very fist lines are a check to see if the called files exists based on the current path:
//untested code
$url = str_replace('..','',$_GET['url']);
if(file_exists(dirname(__FILE__).'/'.$url)){
//redirect to that file or serve it with PHP
exit(0);
}
background-image: url(public/img/image.jpg);
will look in public/img path relative to the CSS file.
What you should probably be using is ../img/image.jpg
or /public/img/image.jpg

URL rewriting redirecting to a single file

Let's say I have the project folder as follows:
folder/models
folder/view
folder/controls
folder/public
folder/library
Now let's say that the site folder is folder/public/ and inside that folder there's just one file called index.php. This file handle all the site page request via the GET parameter index.php?page=user for example will call the user.php file of the application in another folder. The point is that I'd like that an URL such as:
www.site.com/index.php?page=user&id=1
became
www.site.com/user/id/1
How can I do that?
This was taken from CakePHP .htacess rewrite rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
It will render everything under your host
http://www.site.com/* --> http://www.site.com/index.php?url=*
from here your index.php could parse $_GET['url']
//e.g browser requests www.site.com/user/id/1
$url = $_GET['url']; // user/id/1
$params = explode("/",$url); // array(0=>"user",1=>"id",2=>"1")
RewriteRule ^user/id/([0-9]+)$ index.php?page=user&id=$1
But it sounds to me that you should use so called router, redirect all trafic to index.php...
http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/ (check out this link)
In your case there is no point in using /id/, but here you go:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/id/(.*) index.php?page=$1&id=$2
Or what's a way better approach:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php
Then handle the request in your index.php file by checking $_SERVER['REQUEST_URI'] against the patter you dream up.
Without apache rewrite rule, a micro framework named Slim can makes routing and templating for your php project. You'll define your routes only in index.php file. Like ;
Slim::get('/', function () {
Slim::render('index.template');
});
You will be implementing what is called the Front Controller pattern. If you Google that you will find several php implementations. I thought this series on building your own php framework was good.
http://fuelyourcoding.com/php-frameworks-just-roll-your-own-part-1/
Are you using Apache as web server?
If yes you can use *mod_rewrite* to accomplish that.
I have not done this myself, so I can't give you detailed instructions, but searching with google, using a search string like "mod_rewrite examples" lands you a lot of seemingly good tutorials.

Url rewriting broke link to css

I'm using the following setting for url rewriting:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
In index.php is parse $_GET['url'] so that in the following examples:
ROOT/user/id/1/name/bobby // user is the page, id = 1, name = bobby
ROOT/blog/post/123/title/welcome // blog is the page, post = 123, title = welcome
So that the first parameter(? i don't know how to call it) is the page name then the following couple of parameters are like "keys/value".
Now when i browse ROOT/ the link to stylesheets that are inserted inside the html of the page and the page are shown correctly.
I fi browse ROOT/index (which is the same as ROOT/) it shows the page (with contents and other stuff) correctly but the links (even if in the html structure are correctly written) to stylesheets are not loaded. And i can see that from the fact that my page has no css at all when i load it.
How can I fix this?
EDIT
The css file's path is as follows:
project/view/css/common.css
The file where is it included is in
project/public/index.php // the one with .htaccess and rewrite rules
This brings me to make a link (inside the index.php) such as
../view/css/common.css
But this works different depending on how the url seems. For examples:
# For URL = public/
project/view/css/common.css // good
# For URL = public/index/
project/public/view/css/common.css // broken
# For URL = public/index/key/value
project/public/index/key/view/css/common.css // broken
Comment doesn't let me format the code, can you please try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(css|jpe?g|gif|png|js|ico)$ [NC]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Update
You can try something like this in the <head> section of your pages to support relative path for your image/css/js files referenced on that page:
<head>
<base href="http://www.example.com/static-files/" />
</head>
I think you have two problems (as the given answers didnĀ“t solve your problem yet...):
You are rewriting all file-names so when the browser is requesting css/index.css your rewrite transforms that to index.php?url=css/index.css. #cOle2's answer should solve that.
You are not using absolute paths for your css files. The server is translating the requested page like /user/id/1/name/bobby to /index.php?etc.... but when the browser requests for example the css file that is something like css/index.css in your code, it will actually request /user/id/1/name/bobby/css/index.css and that file does not exist. You can solve that by using only absolute paths to your external files (css, js, images, etc.) like /css/index.css.
Edit: Based on your comments, both your paths are relative and your css is not accessible by the browser, so you need to:
Move the css to the project/public directory (like project/public/css)
Use absolute paths to your css like /css/index.css.
Make sure the urls to your external files are not rewritten by the server.
You can try removing certain file extensions from the rewrite rule, for example the following rule will disregard images, css and js files.
# Do not process images or CSS files further
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
Edit: This is how I would apply it:
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Categories