views:

117

answers:

2

I have modified a website with a redirection to a single page:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

It works as it suppose to be. Everything is redirected to index.php. Here is a working example that display index.php with all images and css:

http://.....com/section1

The problem occur when I try :

http://....com/section1/subsection

The page is redirected to index.php, BUT no images, no css, no javascript. Inside the PHP script everything is like this directly linked to the images or css or javascript like:

<img src="images/images1.png> ... <img src="images2.png">

Why does url with many slash like .com../123/123/whatever does not work with images, css or javascript when .com/no_slash_url works?

+2  A: 

You're rewriting without redirection, so the client thinks it's at the original unrewritten URL and all relative paths will be resolved relative to that URL. You should be able to work around this using the <base> element:

<base href="http://myserver.com" />


Let me try and clarify this with an example. If I have a page at http://mysite.com/index.php and all its images are at http://mysite.com/images/, my images might be referenced with a relative url like images/myImg.png. The client translates this url using the current path in the address bar. Now if I rewrite my URLs so that they look like http://mysite.com/mycategory/myarticle, any relative images would resolve to http://mysite.com/mycategory/myimages/, which is incorrect. By adding the base tag:

<base href="http://mysite.com" />

Each browser will now resolve the images relative to that path instead, correctly looking for http://mysite.com/images/.

Andy E
Won't this make dns lookup at every images, css and javascript for nothing?
Daok
Andy E
I do not understand this. Why use a `base` tag and not simply address `/images/xyz.jpg`? But then, I don't really understand what the OP wants yet.
Pekka
@Pekka: In my eyes, adding a `<base>` tag is simpler than changing many image tags. And it just seems more "correct to me. When your *real* url isn't at the current path, you should set the base to the real path. It will avoid confusion and running into the same problem again in the future (ie the next time you add an image)
Andy E
@Andy I can see your reasoning, but I don't like the `base` tag at all because of the long-term maintenance complications it brings along - it implies a different path context, visible only through the tag somewhere in the page. I tend to rather go through the trouble, define a `image_dir` in the scripting language running the site and prepending every image tag anywhere with that variable. I *can* see how `base` can work for a situation like this, but I personally still wouldn't use it.
Pekka
@Pekka: I can see why you might think it's a long term maintenance problem, but it only is if you use a static string as the href. Most CMS output the site root (as defined in the settings) as the base href, meaning that if you move the CMS application to another domain or path, it still works in very much the same way. The reason they use base is that some sites will have many authors, and they can't expect them all to understand the difference between root-relative and path-relative urls.
Andy E
I will try the base tag because Andy explication seem to be fine. I will give you some news tommorow about it.
Daok
This solution works for few things and broke some other links. I think I am in the good path with it. I will accept it but for sure it needs some works. Example, I have a wiki in a subfolder and it doesn't work since that move. But well, I'll do some modification and I am sure it will works. Thank you everybody.
Daok
A: 

Take a look at the html base tag

<base href="http://www.mysite.com/" />
Shuriken