Linking resources in php - php

I have following file structure for my app
/App
/html
404.html
another.html
other.html
/css
main.css
.htaccess
index.php
I am sending other.html file from index.html with readfile().
All html files shares main.css.
index.html looks like this
<html>
<head>
<link rel="stylesheet" href="../css/main.css">
</head>
<body>
link
</body>
</html>
I am running this on XAMPP. With
http://localhost/App
I'm able to retrieve index.html, but when I click on the link I'm getting 404.html, as I wrote another.html and ../css/main.css the server is looking for http://localhost/App/another.html and http://localhost/css/main.css. This leads to messed up CSS in 404.html and index.html
I attempt to solve this by replacing ../css/main.css with css/main.css, but this leads to messed up CSS in 404.html whenever the error is encountered. Another attempt I tried is /css/main.css, but again no luck server is looking for http://localhost/css/main.css.
So, How do I link those file? Is there any configuration available in .htacccess that can say server to look / (root) as /App, so if there is any configuration then I can get everything works perfectly by saying /css/main.css, /html/another.html, and so on? Or any other suggestion?
Here is the video of the problem: https://youtu.be/Qg7K8gOdyW0

Put inside you .htaccess file the following code:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/App/
RewriteRule ^(.*)$ /App/$1 [L,R=301]
in the index.html use next links:
/html/another.html
/css/main.css
Check this out:
https://www.youtube.com/watch?v=oO1AHIOec3c

Related

Redirect to subdirectory path for the page links having absolute path

For an instance, primary URL is http://example.com running on apache server.
I want to put my project in subdirectories as root>projects>thekingfisher resulting into
http://example.com/projects/thekingfisher/
With htaccess, I could access the project
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /projects/thekingfisher/
</Ifmodule>
But, some of the files have absolute paths, which are not accessible as in below file
/root/projects/thekingfisher/index.php
<html>
<head>
<link href="/css/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Test Page</h1>
About
</body>
</html>
As I run my project http://example.com/projects/thekingfisher/
Expected Behaviour:
I want to load the file "css/styles.css" from root/projects/thekingfisher/
Also, "About" link should navigate to "http://example.com/projects/thekingfisher/about.php"
Actual Scenario:
style.css file is being searched at root/css/ to be loaded.
"About" link navigates to "http://example.com/about.php"
Actually, project runs smooth on root. But not properly loading in the subdirectory.
There are many hyperlinks in such pattern, and gets 404 error when tried.
Since this is active project, I don't want to make edits in the source files for modifying the path like prepending baseurl, etc.
You just need a rule in site root to rewrite all requests to /projects/thekingfisher/:
RewriteEngine On
# if file exists in /projects/thekingfisher/ then rewrite
RewriteCond %{DOCUMENT_ROOT}/projects/thekingfisher/$0 -f
RewriteBase .* projects/thekingfisher/$0 [L]

Codeigniter loads html code when trying to load style.css or a img file

This is my first time using a php framework and I haven't found an effective solution.
I've made my page load style tag correctly using base_url() in an asset_helper file, and it seems to work fine.
generated code:
<link rel="stylesheet" type="text/css" href="http://localhost/p_maricarmen/assets/css/style.css" media="screen" />
but it doesn't load the css file.
I have the same problem with others files like loading images with img tag.
I also have a .htaccess file in root directory with code:
RewriteEngine on
RewriteCond $1 !^(index.php|public|robots.txt)
RewriteRule ^(.*)$ /p_maricarmen/index.php/$1 [L]
for the friendly url.
For more information, if put the style url in my navigator, it generate an html page with 404 error, page not founded.
Modify your .htaccess file with the below code.
RewriteEngine on
RewriteCond $1 !^(index.php|public|assets|robots.txt)
RewriteRule ^(.*)$ /p_maricarmen/index.php/$1 [L]
The reason why your css file is not loading is that you've to specify the Rewrite Condition for the folder where your css and js files have been placed.
You can do the same thing for your images folder.
Simply specify the images parent folder name as i've specified for the assets, and it should work as expected.
Hope this helps you...

How to link to files and change base url in CodeIgniter?

Hello I've been running into a few issues with CI lately. When I want to link a css file to pretty much anything, I need to put it directly into the root folder. If I move the file somewhere further in the hierarchy it won't load it. Here's an example of when I am trying to link this core_css.css. It loads the one in the htdocs folder, but not the one in the css folder.
Works:
<link href="<?php echo base_url(); ?>core_css.css" type="text/css" rel="stylesheet" />
Does NOT work:
<link href="<?php echo base_url(); ?>application/OBS/css/core_css.css" type="text/css" rel="stylesheet" />
I've tried putting the css filed in all the other subfolders and linking those, but it only loads the one in the root.
My second problem is that every time I try to test a controller, I need to access it through index.php like this:
http://localhost:8888/index.php/test_controller
Is there a way to get rid of the need to put the index.php in the URL?
For your first problem:
Add .htaccess file in your root web directory and write allow from all it means, in this folder your all files and all folder will not give error Access forbidden! use it like this:
<link href="<?php echo base_url(); ?>application/public/css/style.css" rel="stylesheet" type="text/css" />
N.B:
I usually put all my files like that into an "assets" folder in the application root, and then I make sure to use an Asset_Helper to point to those files for me. This is what CodeIgniter suggests.
For your second problem:
If you are using Apache place a .htaccess file in your root web directory containing the following:
RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
for better understand you can see codeigniter URL section through this link
http://ellislab.com/codeigniter%20/user-guide/general/urls.html
and another one is http://snipplr.com/view/5966/codeigniter-htaccess/
For the second question: put the following in an .htaccess file inside your CodeIgniter root folder (htdocs in your case):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
Make sure to set $config['index_page'] to an empty string, and also set $config['base_url'] accordingly (these two options are inside /application/config/config.php)
For the first question: try to make a folder called assets, or something similar and put it in the root of the site (again inside htdocs). Move your css file there, and try to access it by visiting http://localhost:8888/assets/core_css.css.
Also, in case you are using Linux box, make sure the folders you want to access have read permissions.

htaccess rewrite absolute link folder

I have used htaccess to rewrite my links on my page
RewriteEngine On
RewriteRule ^search/([^&]+)$ search.php?q=$1 [L]
RewriteRule ^login/?$ login.php [L]
# etc..
On my PHP pages I have made all my links absolute like this
<?php require("/incl_head.php"); ?>
<link href="/css/css_file.css" rel="stylesheet" type="text/css">
<img src="/images/image.jpg">
This works fine locally, but now I want to test it online, and I have to put it into a subfolder, since my old page is still active.
My subfolder will be test so that my domain will be www.mydomain.dk/test/
But then my pages dosent work, since all my links are absolute, and dont have /test/ in front
Is it somehow possible to make all links have /test/ in front for example using htaccess file (without interrupt my other rules)?
Or what should I do?
Have seen some html base possibilities or some where they save root in vaiable and include in front of all links. But if I add that into a file and tries to include it, I would have the same problem with those includes on every page.
Hope someone can help me
Option 1:
Try this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteRule !^test/ /test%{REQUEST_URI} [L,NC,R]
Option 2: You can try adding this in your page's header:
<base href="/test/" />

external js & css not loading in codeigniter

To load external Javascript file in codeignitor, I'm using
<script src="<?php echo base_url("application/js/contentslider.js"); ?>" type="text/javascript"></script>
I can see in the page's source as
<script src="http://localhost/cinifb_ci/application/js/contentslider.js" type="text/javascript"></script>
which means to my knowledge that the file is loading perfectly similarly for my CSS file, I've used
<link rel="stylesheet" href="<?php echo base_url(); ?>application/css/default.css" type="text/css" />
and in the page's source code its showing as
<link rel="stylesheet" href="http://localhost/cinifb_ci/application/css/default.css" type="text/css" />
which means even its loading perfectly. (on clicking the external js/css link in the source its showing 403 Forbidden). But neither of their effects are applied on the page. No error its showing.
I've loaded URL helper in controller as $this->load->helper('url');
and placed $autoload['helper'] = array('url'); in autoload.php
can you please explain me where I went wrong.
I learn that to load external JS, we need to install jquery-codeigniter library. I've downloaded them and included the respective files into respective similar folders of my application folder.
Now when I try to include $this->load->library('jquery'); in my controller, its showing
An Error Was Encountered
Unable to load the requested class: jquery
Hence I request you to please help me to understand why 403 Forbidden is showing when I can see its pointing to the exact location of the JS/CSS file and secondly if the installation of the jquery-codeigniter library is wrong please guide me.
Note: http://ellislab.com/codeigniter/user-guide/libraries/javascript.html didn't help me
Edit:
jquery-codeigniter library i've placed in system/libraries/javascript/
In systems .htaccess file, I've written as
RewriteEngine on
RewriteRule ^$ /index.php [L]
RewriteCond $1 !^(index\.php|images|css|js|video_files|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /cinifb_ci/index.php/$1 [L]
In the view I'm writing as
$this->load->helper('url');
<script src="<?php echo base_url("application/js/jquery-1.8.2.js"); ?>" type="text/javascript"></script>
In controller as
$this->load->helper('url');
$this->load->library('javascript');
$this->load->library('javascript/jquery');
$this->load->library('Jquery_ext');
again if I look at source its pointing to correct source but still if i click external js file link, its showing 403 Forbidden error
Sorry for lengthy post :(
Suggestions please ..!
A 403 hidden error means that the Web Server is rejecting the request to that particular resource.
I am going to assume you are using the provided .htaccess file to get pretty urls.
In the line that
RewriteCond $1 !^(index\.php|images|robots\.txt)
Make it read
RewriteCond $1 !^(index\.php|images|robots\.txt|application/js|application/css)
Actually, the above could be wrong, regardless, the idea is there that you need to add your css and js folders to the RewriteCond exception list
The best way is to create folder assets, and then you need to put css, images, js etc.
In this folder save the js/contentslider.js and finally in your header type:
<script type="text/javascript" src="<?php echo base_url();?>assets/css/js/contentslider.js"></script>
Don't forget, you need to set base_url() in config.php.
Thanks
you should include js folder into your .htaccess
if your .htaccess file is inside your application folder then copy it and paste it outside application folder mean root then open it and past code that i have mention and u just need to change name sample in line 3 with your website name or your main folder
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
UPDATE

Categories