separating php and html, stuck in loading images and css - php

I'm new with php. I have a question about separating php and html. This is the root directory of my website
Root
template
html
image
script
css
main.html
main.php
template.php
when I load the main.php by using template.php and main.html, images are not displayed. I know the reason is wrong directory(the current directory is root, not template/html). So how can I fix this problem? How can I import to main.php?

If you are using Apache and can use .htaccess to add rewrite rules, you can use this:
RewriteEngine On
RewriteBase /
RewriteRule image/(.*) template/html/image/$1
RewriteRule script/(.*) template/html/script/$1
RewriteRule css/(.*) template/html/css/$1
Or another solution is to add template/html/ prefix to all image/script/css URLs manually.

When you include a PHP file from a HTML file you need to use the script tag. In your case you need to do something like as follows:
<script src="template/main.php"></script>
and when you import a PHP file from another PHP file you need to use the include thing.

Related

Codeigniter Page Content Unsuccessfully Loaded

Since I am newbie in Codeigniter, I've got problem for my URI format.
I have url : myweb/admin/
If I write the URL :
myweb/admin/
myweb/admin/index
My page cotent is perfectly loaded.
But when I write like this:
myweb/admin
myweb/admin/index/
My page content is mess and when I open browser console, I got message about I failed to load resurces (CSS, JS , etc.)
Can you tell me what I've missed?
In your .htaccess have some like this
RewriteCond $1 !^(index\.php|assets)
?
to overriden the rewrite conf to this folders? (where assets is a folder with css, jquery, images ... )
For including all your files (css, img, etc.), you should use:
<?php echo base_url('path/to/file.css'); ?>
Relative to the defined base_url() in config.php, and then it will work even if it's moved to another folder.

How to code HTML with URL Rewriting as directory

I have used .htaccess in the past to rewrite my URLs.
I used things like this:
RewriteRule event-([0-9]+)-([^.]+)\.html$ /event.php?id=$1 [L]
BUT really prefers directory style:
RewriteRule ^events/([a-z]+) events.php?cat=$1 [NC]
My question is that it has always bothered me in the HTML with the directory style to add this kind of code to link images, includes(); and other links when the directory gets long when with the first style I don't have to change anything.
../../../images.jpg
../../../../../script.php
Is there an other way to code this because it's really bad like thisnd gets on my nerves each time.
Assuming that you have the folder structure like :
css [folder]
images [folder]
event.php
events.php
index.php
etc.
You could use in your html : base and at that point you can just link your css and images like normal.

Using mod-rewrite to pass route to index.php

I'm kind of new to mod_rewrite so I need some help from you guys.
My folder structure is:
assets/fonts/ Contains all fonts
assets/images/ Contains all images
assets/scripts/ Contains all JavaScript files
assets/styles/ Contains all CSS files
pages/ Contains parts of the page as PHP file or directory containing PHP files
header.php Header of site
footer.php Footer of site
index.php index of site, now always loads pages/home.php.
The pages directory contains all the body parts of the pages like home.php, about.php etc. It also contains directories like 'portfolio' which in turn contains more .php files.
This is how I'd like my urls to be re-written.
http://domain.com/about => http://domain.com/index.php?route=about
http://domain.com/about/kittens => http://domain.com/index.php?route=about/kittens
This should however exclude requests directly to the assests or pages folder.
The reason why the pages folder should be excludes on direct requests like http://domain.com/pages/about is because some parts of the site are loaded with AJAX.
Preferable it would exclude any directory in the root.
Could you guys help me out?
You could use an approach along these lines:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|pages|robots\.txt)
RewriteRule ^(.*)$ /index.php?route=$1 [L]
Save this as .htaccess in your root folder.

Creating a template class: losing images and style by changing directory of template files

I'm writing a template engine for a already waited template.
Now before you say about how redounded this is. I get payed to do it and I don't know why they insist to have one of their own (probably because template is already been done and they have marked it).
They've already marked the template like this:
index.html
<html>
<head><title> [title] </title></head>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<body>
<img class="header" src="images/header.jpg" />
[contents]
</body>
</html>
I'm using a very simple find&replace approach and it works good enough:
index.php
$e = new tengine ;
$e->load_template('index.html');// basically file_get_contents
$e->replace('title' , 'zzzzzzz');
$e->replace( 'contents' , 'xxxx');
$e->show();
It works fine while they are in the same directory.
Now I want to move my assets to another directory called templates.
So I have to call my template like this:
index.php
$e->load_template('template/index.html');
Now the page that renders the template (index.php) is not in the same directory as template file (index.html).
It's still works but I loss all the style and images and .js that are in the template page because they are in the template directory and I render the page one directory above them.
Are there any workarounds? Have in mind that template is already done and creating some kind of GLOBAL base_url like and changing all images and .js/.css links is out of question.
I also suggest creating a .htaccess file rewriting every request to the "templates/" subdirectory except if the requested file exists (so index.php or other files in the webroot still get served properly):
Enable "mod_rewrite" for apache2. At the shell type:
a2enmod rewrite
Put a file named ".htaccess" in the root directory of your web-application with the following contents:
RewriteEngine On
# If the requested file does not exist redirect it to the "template/" subdirectory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ template/$1 [L]
Now if you reqest "css/style.css" the "RewriteCond" statements will notice that there is no "css/style.css" and rewrite the request to "template/css/style.css". But if you request "index.php" the rewrite conditions will notice that there is a file with such a name and serve it like usually.
Just use absolute paths to your media files instead of relative. So, rather than using something like
<img src='../assets/img/temp.png' />
or
use
<img src='/assets/img/temp.png' />
and
Then it no longer matters at all where your processing occurs, where your templates sit, from what server-dir the file is called, etc. You can shift them around to your heart's content, it won't make any difference.
You can use rewrite engine using .htaccess
For example create a rewrite rule for css folder and address it to http://www.mysite.com/css/.
With rewrite module i'm sure you can somehow handle it.
what about
dirname(FILE).'/example.php';
with dirname file you are at exactly that place where the file is
It's because the 'css' directory is in the template direcotory, and the browser can not access it.
put the 'css' directory in the view directory or use ×Ļore accurate address.
If it is an option to modify the html head of the template(s) you can use the base tag to change the base url of all relative URLs in the document:
<head>
<base href="http://yourdomain/template/">
...
</head>
Just remember to insert it right at the top before any relative path is used.
In HTML there is a simple hack available to do the things you needed. The hack is through base url, you have to add extra tag link as:
<head>
<base href="domin.com/directory">
</head>
Kindly Note everything has its pro and cons using the base html tag
Pro:
No use of .htaccess
No use of copying files
Static representation of the document
cons:
It will link all the href tags to the base href provided including Anchor, link or any thing using href
I suggest, that asset inclusion should be done by your templating engine, too.
So instead of writing
<link href="css/style.css" rel="stylesheet" type="text/css" />
in you template, do something like this
[stylesheet css/style.css]
and in your engine then create the appropriate HTML with your prepended asset path.
OR you copy the css files in a relative directory to the root directory.
I don't think the links of the template is the problem since it does not access the .css/.js from the template directory.
What matters here is the location of the file rendering them, since the links would then be read after it is rendered.
So as long as the index.php is on the same directory as before, the links in the template should still work fine.

.htaccess redirect subdirectory as query string in php

This is hard to explain, so hopefully I'm understood in my question.
(1) I want to create "SEO friendly" links that remove the query string from a web site. There is only one variable, let's call it "page". Here is the following code for my .htaccess file.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php?page=$1
This works in providing the proper redirect. So /applications/ will send to index.php?page=applications.
(2) My index.php will include a view page based on the value of $_GET['page']. Here is some sample code below:
switch ($_REQUEST['page']) {
default:
include ("home.php");
break;
case "apps":
include ("apps.php");
break;
}
There seems to be no problems so far.
(3) Let's make apps.php an exact copy of home.php. home.php loads just fine, but apps.php will not load linked CSS and JScript pages. When apps.php is loaded, it thinks it is in the /apps/ directory. To load the linked pages, I would need to insert a "../" in front of the file name. Then it displays correctly.
So my question is -- How can I properly write the .htaccess file so the home.php and apps.php page can be identical files and produce identical results, instead of the apps.php file being treated as if it were in the /apps/ directory?
First, I should apologize as I don't have a solution which involves making changes in the htaccess. My solutions are of a different nature.
I think the problem can be solved if you have a config variable,preferably in a config file, which will hold the root folder for images, js etc. Most of the time its public_html, the document root, where the url of your website points to. so your config variable could look like:
$base_url = 'http://www.mywebsite.com/';
The config file should be included in index.php unconditionally.
So, when you include any js or images, you do it like this:
<script type="text/javascript" src="<?php echo $base_url;?>js/global.js" />
<img src="<?php echo $base_url;?>images/gradient_green.jpg" />
If you include the config file in index.php, all the files you include based on switch-case conditions, will be able to use the $base_url variable.
Another possible solution is to use the base tag. Look it up here:
http://www.w3schools.com/TAGS/tag_base.asp
I hope this helps.
use absolute urls for js, css and images on your pages (starting with a slash).
/js/main.js instead of js/main.js
You can't do that with .htaccess unless you do an external redirect (by adding the [R] flag to your RewriteRule). But then you expose the query string, which is what you wanted to avoid in the first place.
The reason it can't be done: It is not apps.php which "thinks it is in the /apps/ directory" - it's the browser which "thinks" that. In the page source generated by apps.php, you send relative URLs back to the browser, and now the browser will request these resources relative to the location of the page it asked for. For the browser, the page it got is in /apps/, no matter what rewriting you applied internally on the server side.
So the options you have are:
Do an external redirect with your .htaccess (and defeat your original purpose ;-)
Change the URLs dynamically with PHP while processing apps.php etc, as you said (prefixing ../ to the URLs)
Use absolute URLs, just as #nobody has suggested in his answer.
The last one is the only real option IMHO.

Categories