Connecting Tech Pros Worldwide Help | Site Map

.htaccess path problem

Newbie
 
Join Date: Jan 2007
Posts: 28
#1: Mar 19 '09
dear all,

I would like to ask about url rewriting in php. One of my project I got a problem
with image path.

.htacess
Quote:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^home/?$ index.php
RewriteRule ^product/([0-9]+)/?$ product.php?prd_id=$1 [NC,L]
I am using header.html as a common file. But the images from header.html only display in index.php. In product.htm none of image can't display. I think I have a problem in .htacess file. Please let me know about this.
Newbie
 
Join Date: Apr 2009
Posts: 1
#2: Apr 8 '09

re: .htaccess path problem


i think you are using relative link for images. Try using absolute path for images, hope u wil solve ur problem
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,747
#3: Apr 18 '09

re: .htaccess path problem


Hi.

You need to be very careful with images, and other resource paths, when using mod_rewrite.

What you need to keep in mind is, the images are requested separately.
So, if a user requests: http://example.com/home/
And you have a rule: RewriteRule ^home/?$ index.php
The request will be routed to: http://example.com/index.php

However, any additional resources, like images, in that page will not be rerouted by that rule.
Meaning, if that page includes an <img> at: images/header.jpg
The request will be: http://example.com/home/images/header.jpg
Which will not be rerouted by your original rewrite rule.

If you want the image path to be re-written as well, you need to add a rewrite rule for that to. (Or come up with one that covers both)

A simpler way, as rayckins suggested, would be to use absolute URLs for all resource links.
Meaning: /images/header.jpg
Rather than: images/header.jpg

The added / to the link simply means that it should start at the web-root, rather than the current location.
Member
 
Join Date: Feb 2007
Posts: 75
#4: Apr 19 '09

re: .htaccess path problem


Quote:

Originally Posted by Atli View Post

Hi.

You need to be very careful with images, and other resource paths, when using mod_rewrite.

What you need to keep in mind is, the images are requested separately.
So, if a user requests: http://example.com/home/
And you have a rule: RewriteRule ^home/?$ index.php
The request will be routed to: http://example.com/index.php

However, any additional resources, like images, in that page will not be rerouted by that rule.
Meaning, if that page includes an <img> at: images/header.jpg
The request will be: http://example.com/home/images/header.jpg
Which will not be rerouted by your original rewrite rule.

If you want the image path to be re-written as well, you need to add a rewrite rule for that to. (Or come up with one that covers both)

A simpler way, as rayckins suggested, would be to use absolute URLs for all resource links.
Meaning: /images/header.jpg
Rather than: images/header.jpg

The added / to the link simply means that it should start at the web-root, rather than the current location.

I am also with the same problem and has been wondering how to make it work. Well i am not sure about how to rewrite the image,css & js path in htaccess but i can say little about the absolute path you are referring to. Well, adding a slash in the infront doesnt solve the problem. Only if you use the entire long absolute path then it works something like href="http://localhost/mysite/images/image1.gif". And if we start using absolute path in all our hrefs i must say its not manageable for big sites.

So, i feel there should be some way to use relative paths only. Or it can be done via the path rewriting method in htaccess. I tried this way but couldnt make it work href="<?php echo $_SERVERDOCUMENT_ROOT."images/image1.gif"; ?>".

Anyone can please tell us how to rewrite the path of these files aswell in htaccess.
Member
 
Join Date: Feb 2007
Posts: 75
#5: Apr 19 '09

re: .htaccess path problem


I found a solution for the path problem. I think you were using relative path for refering to image, css and js files.

ok define a constant variable in your page something like
Expand|Select|Wrap|Line Numbers
  1. define("ROOT_DIRECTORY", "\your root directory\"); 
to store the path of your root directory. Then whenever you want some image or css or js files to be used in your page you can do it in this way
Expand|Select|Wrap|Line Numbers
  1. href="<?php echo ROOT_DIRECTORY; ?>your image directory/image file"
.

But remember here that the constant ROOT_DIRECTORY should have a value of absolute path. If i am to give you an example, I am using WAMP, so i will give you the wamp example. My files are stored under the www directory of wamp i.e C:\wamp\www\mysite folder. So now when i use absolute path i have to use \mysite\ as the root. Now, i will write my root_directory constant as
Expand|Select|Wrap|Line Numbers
  1. define("ROOT_DIRECTORY", "\mysite\");
I have a image folder inside mysite folder. Now if i want to use a image i will reference it in this way
Expand|Select|Wrap|Line Numbers
  1. href="<?php echo ROOT_DIRECTORY; ?>image/image.gif"
This will make sure that correct path is specified in any page whose url is rewritten.

Hope this helps you. I also had the same problem as yours but i was able to solve it today following the above method.
Reply