1

This problem concerns me for a long time. I admit that it is probably because of lack of experience so I hope I could get some ideas.

Here is the code in .htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^view-([0-9]+)$ view.php?id=$1 [NC,QSA]

So when I access http://domain/project/view-44 it works fine, but I would like it to work with a slash in the end like http://domain/project/view-44/

So I changed the last line to

RewriteRule ^view-([0-9]+)/$ view.php?id=$1 [NC,QSA]

And now when I access http://domain/project/view-44/ all the images, javascripts and style is gone.

Now, instead of getting the css from http://domain/project/style.css it tries to get it from http://domain/project/view-44/style.css

I believe the issue is about incorrect way of specifying url for items. In my case it is simply

<link rel="stylesheet" type="text/css" href="style.css" >
<img src="images/web/showreel.png" alt="Image1" />

Does that mean that I have to specify a full url ( http://domain/project/images/web/showreel.png ) in every required element?

4 Answers 4

3

It is always best to specify absolute URL as this way you know the document will always be reached. I know its longwinded but instead of declaring

<img src='http://domain/project/images/web/showreel.png' />

Setup a PHP gloabal variable like

define('SITE_URL', ' http://domain/project');

Then do

<img src='<?php echo SITE_URL; ?>/images/web/showreel.png' />

Also with regards to the .htaccess, take a look at http://blog.valtersboze.com/2009/06/add-or-remove-trailing-slash-in-url-with-htaccess/

This explains how to remove or keep the trailing slash, but not both, never both, google doesnt like both, they'll see it as duplicate content.

1
  • 1
    Thank you Sean, I had a clue that there is no way around except for using full url. Dec 2, 2011 at 0:24
2

The easy workaround is to use

<base href="/index">

in the <head> of your HTML templates. Thus all the resource links become relative to the root directory again, independent of the virtual RewriteRule structure.

http://www.w3.org/TR/html5/semantics.html#the-base-element

2

Absolute paths for your resources is enough:

<link rel="stylesheet" type="text/css" href="/project/style.css" >
<img src="/project/images/web/showreel.png" alt="Image1" />

Note the / char at the begininning. You don't need the hostname and the http part.

To avoid the differences for absolute urls between development and production you could do a simple thing. Enable virtual hosts in your apache configuration and create a virtual host for each one of your projects. In this way you don't have to use this structure in your dev environment:

http://localhost/project1
http://localhost/project2

but you can use urls like these

http://project1/
http://project2/

In this way your problem is solved and you can use url's like /images for your resources.

To enable virtual host for apache you'll need to tweak dns configuration for your machine by adding these lines to your /etc/hosts file

127.0.0.1 project1
127.0.0.1 project2

and then you'll need to create corresponding virtual hosts

2
  • That would fix the problem for a while, but when I go from my local server to a project dedicated server, the project will be in root directory. Then I would have to edit every link. Dec 2, 2011 at 0:20
  • Your development environment should be similar to your production to avoid this kind of issues. I'll update the answer with a solution to this issue.
    – Fabio
    Dec 2, 2011 at 0:29
1

Put this in the head of the document:

<base href="http://domain/project/" />

This is necessary becausee the client's browser has no way of knowing the rewritten URL, it is only known on the server side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.