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
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]
I am a newbie.. My htaccess now has
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# /viewgallery.php?cname=Colorado-Fall&pcaption=Poked to /photos/Colorado-Fall/Poked.jpg
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+viewgallery\.php\?cname=([^&]+)&pcaption=([^&\s]+) [NC]
RewriteRule ^ /photos/%1/%2.jpg? [R=302,L,NE]
RewriteRule ^photos/([^/]+)/([^.]+)\.jpg$ /viewgallery.php?cname=$1&pcaption=$2 [QSA,L,NC,NE]
And redirect works but the images will not show up on my pages if they are not in the root folder. If I have a sub directory created with images(for organizing them), they will create an error as 'images not found'.
Is there a solution for this? Thank you for any suggestions....
This is probably a relative/absolute URL thing. Try adding a relative URI base to the header of your page:
<base href="/" />
Or whatever the correct base should be. Either that, or change all your links to absolute urls:
From:
<img src="subfolder/banner.jpg">
to:
<img src="/subfolder/banner.jpg">
I'm trying to use htaccess redirect to a file, but can't get the image to display.
I want to redirect ANY request to mydomain.com or www.mydomain.com to www.mydomain.com/test.html
This is the content of my htaccess file:
# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/test.html
RewriteCond %{REQUEST_URI} !=*\.png$ [NC]
RewriteRule .* /test.html
I have this line of code in my test.html file:
<img src="image.png" alt="My Image">
But the image doesn't display at all, I just get a broken image. I also tried using absolute path for image, same thing.
Try replacing your .htaccess file with the following
# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#if the request does not end with test.html or is not a .png
RewriteCond %{REQUEST_URI} !(test\.html|\.png)$ [NC]
# Rewrite it to test.html
RewriteRule .* /test.html [L]
This will exclude various types of images
#if the request does not end with .gif,.png or jpg
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
RewriteRule ^index.html$ index.php [NC]
Try:
RewriteCond %{REQUEST_URI} !\.png$ [NC]
Not familiar with !. prefix. Check the RewriteCond Docs
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.