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.
Related
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.
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 have problem with this rule:
RewriteRule ^([^/]+)/(\d+)$ more.php?books=$1&tags=$2 [L]
After this rule everything works but page is displayed incorrectly. Like missing css.
Thanks in advance
This might happen, when you have a relative URL to your CSS files. If you access the URL /book-name/2 and have a link to css/style.css, for example, the browser will resolve this to /book-name/css/style.css.
Change your CSS links to absolute URLs, like /css/style.css, and you should be fine.
I have some problems with mod_rewrited at .httacess.
We have created a website, the website links have this sctructure.
www.myweb.com/page.php?title=this-is-the-title&filmID=454122
AND
www.myweb.com/video.php?title=this-is-the-title&filmID=2567971&player=veevr
AND
www.myweb.com/gallery/index.php?galeriID=11
is possible to change this structure to:
www.myweb.com/page/this-is-the-title/454122.html
www.myweb.com/video/this-is-the-title/454122/veevr.html
www.myweb.com/gallery/index/11.html
Any help will be really great
Thank you for reading the post!
Best Regards
JoinOG
In your .htaccess file in your web root folder put the following code.
RewriteEngine On
RewriteRule ^page/([^/.]+)/([0-9]+).html/?$ page.php?title=$1&filmID=$2 [L]
RewriteRule ^video/([^/.]+)/([0-9]+)/([^/.]+).html/?$ video.php?title=$1&filmID=$2&player=$3 [L]
RewriteRule ^gallery/index/([0-9]+).html/?$ gallery/index.php?galeriID=$1 [L]
What this does is mask the url /page/something/12345.html to page.php?title=something&filmID=12345. It masks it, so when you go to the first URL it still looks like the first URL in the address bar but is really at the second URL. Simple tutorial on how this works is here: http://corz.org/serv/tricks/htaccess2.php
The server will think you are in the folder /page/something/ so if your CSS, images and hyperlinks are locally relative links they will not work, e.g. it will look in /page/something/yourimage.png for an image linked to like this <img src='yourimage.png'/>. To get it to work as you'd like it to, you'll need to put a forward slash before all your links to make it relative to your website's root folder like this <img src='/yourimage.png'/>.
If I have a link, for example: dashboard/xyz/fff/ and I use modrewrite to change it to dashboard/?loc=xyz&action=fff when the page loads are loc and action available as variables to use?
If so, then here's a specific example I can't seem to get to work. My rule as it sits:
RewriteRule ^getclients/([a-z\-]+)$ /dashboard/?action=getclients&module=$1
And the link that is sending them to that url:
<li>SEO Analysis</li>
I want now to be at .com/dashboard/?action=getclients&module=$1 and use those variables to load the page content that's needed.
However: Now the page redirects to what I believe is the "right page" but the CSS is all broken. I only have plain text. Feel free to suggest another way to achieve the same effect as well perhaps using jQuery and Ajax or something to load up sections of the site.
Thanks!
If you don't want to use absolute paths, you can try rewriting the requests for images, javascript, and css. Maybe something like this:
RewriteCond %{REQUEST_FILENAME} \.(js|css|png|jpe?g|gif)$ [NC]
RewriteRule ^getclients/(.+)$ /dashboard/$1 [L]
Make sure that the file locations are exact, or the server might send the data from the wrong relative directory.
For loading css, images, js files properly from a different relative path you should specify a base URL for all relative URLs on a page like this:
<base href="http://www.example.com/dashboard/" />