linked files in htaccess - php

I'm having a problem with the linked files in the page that is affected by the scope of the .htaccess file.
Here's the .htaccess file:
RewriteEngine On
RewriteRule ^tp_update/id/([A-Z0-9]+)/([A-Z]+)/?$ update_taxpayer.php?tp_id=$1&tp_type=$2 [NC,L]
I didn't get any error, and I can access this page:
tp_update/id/1234/ITP
But the problem is that, all the files which are linked to update_taxpayer.php is also being affected.
When I view the page source and click the link css file. It says the file isn't found:
<link href="../../css/style.css" rel="stylesheet" type="text/css" media="screen" />
And I get this:
tp_update/css/style.css
Instead of the link which I declared above.
How do I get around with this?Is there a proper way of linking files when mod_rewrite is enabled.

You can either use absolute links as Dragon suggested, or you can use the <base href="absolut_base_url">
What this does is make all your relative calls start begin at the absolute_base_url. So (using your style example) if you just had css/style.css as the href, the browser will attempt to call
http://yoursite.com/absolute_base_url/css/style.css
instead of
http://yoursite.com/absolute_base_url/tp_update/css/style.css

Related

How to make Dynamic css path?

I'm using not_found.php file for unfound files on server , and it's in Main Directory
it's style is in 'assests/css/main.css' from same Directory ,
so I make href of linking stylesheet
href="assests/css/main.css"
it works perfect if user types 'Main/wrongFile' for example , but if user typed 'Main/ez/wrongFile' , stylesheet is included wrongly due to changing of path
I'm searching for function like
$Path = FindPath('Main/assests/css/main.css');
so it gets that path wherever file is in
then I can simply
href="<?php echo $Path; ?>"
The best way to fix it is using the code below;
<link rel="stylesheet" href="/assests/css/main.css" type="text/css"/>
adding / at beginning of assests/css/main.css simply tells browsers which root directory to look at.
Option 2) Using htaccess. Add the code below into your .htaccess file
RewriteRule ^([^/]*)/assests/css/main.css$ assests/css/main.css [R=301,L,NC]

Codeigniter not displaying stylsheet when clicking on links

I just built a sit in Codeigniter, but when I click on a menu item the stylesheet breaks and it displays all of the content broken out on the page.
http://marciabrownproductions.com/
I uploaded it to a test server and all the links works perfectly.But when I uploaded the finished site ot my clients server the css issue starts. Is there something wrong with my clients server? Or is there a problem with how I coded the site? Why does it work on one server, but not the other.
Thanks!
First, I recommend using absolute paths instead of relative paths when referencing your assets. At the moment, they are being picked up with the index.php included.
So, let's say your link is http://domain.tld/index.php/home/actors, you should be calling your assets from their actual URLs - http://domain.tld/index.php/_/css/style.css would not work, for example. http://domain.tld/_/css/style.css, however, would work. (That's just a sample URL.)
I'd also recommend removing the index.php from your links, simply by using the htaccess RewriteEngine, and removing index.php from your app's config file.
Have a look at how your current source is being generated in a browser (right-click, View Source) to see what I mean.
Update:
Based on your comment, that may be an htaccess issue. Check to see if your hosting provider supports it.
In the meantime, put the index.php back in the app's main config file. However, leave your CSS asset link as it is. Make sure all your asset links look like this:
<link href="http://marciabrownproductions.com/_/css/styles.css" rel="stylesheet" type="text/css" />
<script type='text/javascript' src='http://marciabrownproductions.com/_/js/jquery.validate.js'></script>
<script type='text/javascript' src='http://marciabrownproductions.com/_/js/effects.js'></script>
<link href="http://marciabrownproductions.com/_/css/bjqs.css" rel="stylesheet" type="text/css" media="screen" />
<script src="http://marciabrownproductions.com/_/js/bjqs-1.3.js"></script>
Your images are broken because their links are also including the index.php/home. You need to make sure that that is taken out. (What are you using to generate your links anyway?)

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.

Missing CSS file and images after URL rewrite

I'm trying to make user friendly URL using mode rewrite.
My problem is, that after giving category like 'name' to my URL, when I call the page using new URL, it can't load the CSS file or images.
I have a link like:
localhost/mywebsite/project?id=22
New link is something like
localhost/mywebsite/project/22/myproject.project
htaccess code:
RewriteRule ^project/([0-9]*)/.*\.project$ /project.php?project=$1 [L]
(it might not be 100% right but I don't have access to my code right now so I just wrote this and it works fine on the original source)
My root directory is localhost/mywebsite/
and my CSS file is in css/style.css
localhost/mywebsite/css/style.css
my htaccess
localhost/mywebsite/.htaccess
and my project.php file is in
localhost/mywebsite/project.php
So in the project page I have access to CSS file by using relative path,
<link href="css/style.css" rel="stylesheet" type="text/css" />
but when I use rewritten URL page can't find the CSS file.
I can't use absolute path with domain name because I don't have domain yet! and it can be anything.
one way is to use relative path to domain as suggested on the similar questions
localhost/mywebsite/project.php
and when i run my script localy my root directory is
localhost
so css link should look like
href="mywebsite/css/style.css"
but when i go live i should change all links to probably something like
href="/css/style.css"
this seems like lots of work
For your local version add
<base href="//localhost/mywebsite" />
to the head section
and for your live versions change it to
<base href="//your.domain.here" />
reference at http://www.w3.org/TR/html4/struct/links.html#h-12.4
you have to define the base path or the server view path in the connection.php and whenever u want that path, make that global. then that variable will b called and the css or images will take the whole path.
for example
$SVP="http://www.example.com/"
global $SVP;
echo $SVP;
so
Insert an image into the same file with the same relative path as the css href link, load the page in a browser, right-click the image in internet explorer, click properties and you should see where the relative path actually points to.

RewriteRule changes physical paths for files

Let's say the domain for my website is [http://mywebsite.com][1] and that opens the index.php. That script gets the $page,$section,$language variables from the url. So [http://mywebsite.com/index.php?lang=en&section=home&page=sitemap][2] opens the sitemap page in English which belongs to the "home" section. And I want that same url to be rewritten to [http://mywebsite.com/home/sitemap_en.html][3]. To achieve this, already I've put the following in the .htaccess:
RewriteCond %{REQUEST_URI} .+\/.+
RewriteRule ^(.+)\/(.+)_(mk|en|al)\.html$ index.php?lang=$3&section=$1&page=$2 [L]
But there is a huge problem now. When I visit some url like that, the files are not found because the file style.css is in the root folder and not in [http://mywebsite.com/home/style.css][4] , and there the server is searching for it. "home" is not real folder and it doesn't exists, it's only a section. The same goes for all the jpg, png, js, gif etc. How can I redirect the pages the way I like, and the files to be found with the real paths?
p.s. Some section like [http://mywebsite.com/index.php?lang=en&section=contact][5] don't have pages at all. They should be reached like so: [http://mywebsite.com/contact_en.html][6]
I have this for them, after the previous rule: RewriteRule ^(.+)_(mk|en|al).html$ index.php?lang=$2&section=$1
You can use a base tag in your header so that all relative paths are off of a specific href so:
<head>
<base href="http://mywebsite.com/" />
</head>
would cause all relative (not just css) URL's to be loaded off your root directory
OR
as stated by others just make your paths absolute by putting a leading "/" at the front:
<link rel="stylesheet" href="/css/mycss.css" type="text/css" />
Did you try and make the references to css and js files etc relative to the top level directory (the home directory) by prefixing with a slash? ie if you keep them in a directory called styles in the sites home directory:
<link rel=StyleSheet href="/styles/style.css" type="text/css" />

Categories