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.
Related
I have this simple redirect rules:
RewriteEngine On
RewriteRule ^tags/([^\.]+)/?$ home.php?search=$1 [L]
So if a url looks like
http://example.org/path/to/folder/tags/exampleTag
It is redirected to
http://example.org.com/path/to/folder/home.php?search=exampleTag
It works, it redirects to home.php, but the problem is things and images on home.php. Apparently, the path when loading the images turns to
http://example.org/path/to/folder/tags/imgs/img.jpg
Instead of:
http://example.org/path/to/folder/imgs/img.jpg
I.e, during redirection, the home.php loads things like it is in the tags folder and because they are not, the images do not get loaded.
How can I solve this?
On home.php I am using html to load images like this:
Both the .htaccess, home.php and the imgs folder are on the same folder
Add this to the header of your pages:
<base href="/path/to/folder/" />
so that all of your relative paths use the correct URI base.
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.
I'll try to make this brief. This is my first attempt using HTACCESS to create "user-friendly URLs". I've set up my .htaccess file so that when a user clicks a link that appears to be in a subfolder, it loads a file within the root.
ex:
RewriteEngine On
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^current-communities/(.*)$ $1 [L]
So if I click a link like "/current-communities/my-community it will load the file "my-community.php". No problem there.
However, now I'm trying to make another rule that when I click a "floor-plans" link, the floor-plans.php file will load data from a database based on its querystring:
RewriteRule ^floor-plans/([^/]+)-([^/]+).php floor-plans.php?communityname=$1&modelname=$2 [NC]
So if a user clicks on a link like "/floor-plans/my-community-myhouse", it will load the "floor-plans.php" file, with "my-community" as the first querystring variable, and "myhouse" as the second.
The issue I'm having here is that the "floor-plans.php" file is not showing the pictures. It is looking into a folder called "images" for graphics, but because "images" is actually within the root directory, I only get a very empty page, but for some reason this is not the issue with the virtual directory of "current-communities".
Can you help?
Hello try using the next code in the head of your document, basically what it does is to set path for all scripts and css files to the actual root of the php document and not what the address in the browser is set up to your using.
so what it actually does is once set in the head before importing anything else php will automatically set the path if you give on css/style.css, php will set it before it goes back to the browser to http://www.myweb.com/css/style.css
it works fine for my wbesites hope it helps you.
<BASE href="http://<?php echo $_SERVER['SERVER_NAME']."/".$_SERVER['PHP_SELF'] ?>">
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.
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.