I am using Codeigniter on XAMPP server in Windows. I removed index.php from url using .htaccess and Apache mod_rewrite as below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mysite/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|favicon\.ico|robots\.txt|css|js)
RewriteRule ^(.*)$ /mysite/index.php/$1 [L]
</IfModule>
My folder structure is:
htdocs\mysite\index.php
htdocs\mysite\application\...
htdocs\mysite\system\...
htdocs\mysite\js\...
htdocs\mysite\css\...
...
I have a controller named "main" that is default controller and another controller named "news" and two functions named "index" and "show" in each. All controller functions use a single view file named "header.php" that looks for two file:
<link type="text/css" rel="stylesheet" media="screen" href="css/default.css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
But what is the problem? Only main/index could access to .js and .css files. main/show, news/index and news/show look for a url like:
http://localhost/mysite/news/js/jquery.min.js
http://localhost/mysite/news/css/default.css
If I use "../js/jquery.min.js" and "../css/default.css" in header.php the problem will be solved except for main/index. I don't want to use separate header files for main/index and other controllers and functions, because the header is the same in all of them. Is there a solution using Apache mod_rewrite or Codeigniter routing config to solve this problem?
Thank you in advance
Option 1: use full path relative to site
<script type="text/javascript" src="/mysite/js/jquery.min.js"></script>
Option 2: use base_url() (must be set in config.php)
$config['base_url'] = 'http://www.example.com/mysite/';
-
<script type="text/javascript" src="<?php echo base_url() . "js/jquery.min.js"; ?>"></script>
This isn't an issue with routing or your .htaccess.
If you don't mind do this
<script src="<?php echo site_url('js/jquery.min.js'); ?>"></script>
Instead of this :
<script type="text/javascript" src="<?php echo base_url() . "js/jquery.min.js"; ?>"></script>
And remove the type="text/javascript" it's not required
Related
My website is working fine.
All controllers works fine unless I put a slash at the end the pages broke.
Here is oringal URL: Saaf.Pk
But when I put a slash at the end, style & images broked: Here
I know the problem is that URL path got changed. But what's the general solution for this situation?
My .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
are you using base_url to define every image, js, css etc?
example img
<img src="<?=base_url('assets/uploads/cms/') . $cms->logo_2?>" alt="Logo">
example js
<script src="<?=base_url('assets/plugins/jquery-3.3.1/jquery.min.js')?>"></script>
example css
<link href="<?=base_url('assets/guide/css/hopscotch.css')?>" rel="stylesheet"/>
First of all, set the base_url in config folder config.php location in application->cofig->config.php and use as $config['base_url'] ='https://saaf.pk/';
Than give the link of images, css and js as ">
Don't forget to use the helper URL in autoload.php file.
i am having trouble about to link my css and js files on code igniter. i am new on codeigniter and need some help. i look for it but couldnt find a really working solution. my folder structure is like:
-> system
-> application
-> js
-> jquery.js
-> css
-> style.css
-> images
-> .htaccess
-> index.php
my base url is like http://subdomain.mydomain.com/ working on a sub domain
here is my .htaccess file for pass out /index.php/controller i m giving this because i dont know if it effect the issue.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|scripts|robots\.txt)
RewriteCond {REQUEST_FILENAME} !-f
RewriteCond {REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
and here is my header.php - i use a simple template structure, header content footer loading
<?php
$css = array(
'href' => 'css/style.css',
'rel' => 'stylesheet',
'type' => 'text/css',
'media' => 'screen'
);
echo link_tag($css);
?>
<link rel="stylesheet" href="<?php echo base_url();?>css/style.css" />
<script src="<?php echo base_url(); ?>js/jquery.js" type="text/javascript"></script>
here i tried html helper for css and also i tried base_url()/css but didnt work both. and also for jquery lib didnt work. it returns on page source like
src="http://subdomain.mydomain.com/js/jquery.js"
when i click it, it must show the codes which inside of my file but it shows 404 not found. thanks for your help.
Remove this line and try once:
RewriteCond $1 !^(index\.php|images|stylesheets|scripts|robots\.txt)
OR Use this instead:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Lets say I'm using Apache and I have a sample web folder like:
http://myserver.com/test/
In this folder I have the following files:
index.html
img.php
sample.jpg
.htaccess
The index.html have the following code:
<html>
<body>
<img src="sample.jpg" />
</body>
</html>
The img.php have the following code:
<?php
if(!isset($_GET["path"])){
exit()
}
?>
<html>
<body>
<img src="<?php echo $_GET["path"] ?>" />
</body>
</html>
I like http://myserver.com/test/sample.jpg to show http://myserver.com/test/img.php?path=sample.jpg content
(with no URL change)
After a lot of search on the net I wrote the following code but it does not work :/
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(png|gif|jpe?g)$ [NC]
RewriteCond %{SCRIPT_FILENAME} \.(php|html)$ [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+)$ /test/img.php?path=$1 [L]
There is probably an ! to put on the third line but I do not know exactly where
Change your .htaccess code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /test/
# block direct access to images
RewriteCond %{HTTP_REFERER} !^http://(www\.)?myserver\.com/ [NC]
RewriteRule ^(.+?\.(?:png|gif|jpe?g))$ img.php?path=$1 [L,NC]
To output an image as base64, you would use something like this:
<img src="data:image/jpg;base64,<?php echo base64_encode(file_get_contents('/path/to/image.jpg'));?>"/>
Remember that's server path, not document root, so you may need to add $_SERVER['DOCUMENT_ROOT'] in front of your normal image file path
I’m new using codeigniter. I using codeigniter 2.X. I have some question regarding basic course of codeigniter.
What the best suggestion for folder location of our external css/ js /image/another ? For now, i put js/css/or another file on the root, i.e site url/css
How to describe path of image on css and javascript file ? On php its easily that just use <?php echo base_url(); ?> but how in css and javascript?
Both question, I need for the best and experience answer because the answer will be my knowledge base for using codeigniter.
Thank you so much.
Store your css/js/image in assets folder or as u want
/www
/code_igniter
/application
/assets
+img
+css
+js
/controllers
/system
then use it in your view as including it
<link type="text/css" rel="stylesheet" href='<?php echo base_url()?>assets/css/mystyle.css'>
<script type="text/javascript" src='<?php echo base_url()?>assets/js/myjs.js'></script>
into your .htacess file add this code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
put .htaccess file outside the application folder
I have a favicon file that won't show up on my site.
At first I thought it might be the htaccess that's to blame, but I'm not sure if that's the right direction.
My htaccess is set to send the URL to index.php for parsing:
#Pass to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(css|gif|jpe?g|png|txt|xml|js|pdf|html)$ /home/username/public_html/domain.com/index.php [NC,L]
#Hotlinking
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.com(/)?.*$ [NC]
RewriteRule .*\.(gif|jpe?g|bmp|png|ico|css|js|pdf)$ http://domain.com [R,NC]
And I have the following HTML:
<link rel="icon" href="http://domain.com/favicon.ico" />
<link rel="shortcut icon" href="http://domain.com/favicon.ico" />
When I try to access the favicon.ico file directly, the image doesn't load at all. When I try opening the image file locally (from my hdd, using chrome or firefox) it opens and shows up fine.
Please see comments (above) for solution/workaround.