puk
puk

Reputation: 16782

How does Apache know when and how to look for relative paths?

If we have a file structure like so:

/
├── aboutus
│   ├── aboutus.html
│   └── logo.jpg
├── validate.php
├── index.html
└── logo.jpg

Although one would have to specify the folder when linking to aboutus.html ( ie. <a href="aboutus/aboutus.html">), in both index.html and aboutus.html if you place logo.jpg, Apache will know which one to load. It probably looks for the file in the current directory only. I have the following directive in my Virtual Host section:

RewriteCond %{REQUEST_URI} !^/validate.php
RewriteRule ^/(.*) /validate.php?uri=$1

I am a total newbie with these rewerite rules, so this is what I want the above to do: I want it to intercept any requests from the browser. These include requests entered in the address bar, as well as requests from within web pages (ie. js files, images...). Next I want to send the requested file to validate.php

validate.php obviously validates the user, but essentially just returns whatever file the user requested. If aboutus.html inserts its picture like so <img src = "logo.jpg"/>, how can I ensure that validate.php returns /aboutus/logo.jpg and not /logo.jpg?

Upvotes: 0

Views: 200

Answers (3)

TerryE
TerryE

Reputation: 10898

Puk, You have to keep two aspect separate:

  • Any HTTP request, such as a GET are absolute, and the browser will convert any relative href or src attributes based on either the path of the referring document (or the path defined in a <base> tag) so if you load http://somehost/fred/xx.html which then references an image style1/images/pic.png then the browser will request http://somehost/fred/style1/images/pic.png

  • How mod_rewrite processes paths is quite different. That's the business of the server. See the RewriteRule Documentation which explains how relative and absolute paths are evaluated inside a per-server configuration (apache.conf or vhost.conf) and inside a Per-directory configuration (e.g. in an .htaccess or block)

Upvotes: 1

Jiř&#237; Hern&#237;k
Jiř&#237; Hern&#237;k

Reputation: 2477

Anyways in htaccess file you always work with absolute path. I mean starting with root slash.

try to use:

RewriteCond %{REQUEST_URI} !^/validate.php
RewriteRule ^(.*) /validate.php?uri=$1

Upvotes: 0

Alexander Pavlov
Alexander Pavlov

Reputation: 32296

Essentially, the absolute path is determined from the base resource URL and a relative resource URL (more on this here). For your validate.php to be able to properly resolve the relative URLs for images, stylesheets, etc., you should somehow let it know the absolute URL of the document containing these resources (and this is even trickier if the document has the <base> tag).

Upvotes: 1

Related Questions