user1102979
user1102979

Reputation: 45

URL Rewrite and design files

I am coding a portal which has multi categories.

Example:

http://localhost/portal/cats

http://localhost/portal/dogs

Ofcourse this is possible with URL rewrite

Like this

RewriteRule ^cats$ /portal/index.php?cat=cats

Ok so this works alright.

Problem happens when I try to access page like this

http://localhost/portal/cats/black-cat-22

My url rewrite is:

RewriteRule ^cats/(.*)-([0-9]+)$ /portal/page.php?cat=cats&id=$2

Why?

Because all my CSS & image files are located in http://localhost/portal/elements and not in http://localhost/portal/cats/elements - which is not even real directory.

So I am looking for solution how to do rewrite if URL includes /elements/ it reads from /portal/elements not from any other directory.

If something is unclear, free to ask below.

Cheers and happy new year.

Upvotes: 2

Views: 118

Answers (2)

bummzack
bummzack

Reputation: 5875

That's where the html <base> tag comes to the rescue :)

Add something like this as first line in your HTML head:

<base href="http://localhost/portal/" > <!-- or end with /> if XHTML --> 

Then everything you reference with relative URLs will be relative to that base.

So you can reference your CSS using:

<link rel="stylesheet" type="text/css" href="elements/stylesheet.css" />

Which should resolve to: http://localhost/portal/elements/stylesheet.css, no matter what URL you're currently visiting (be that portal/cats, or portal/cats/some-black-cat etc.

Upvotes: 3

hakre
hakre

Reputation: 198115

As you map the URLs when they come into your application, you need to map back the URLs when they "return" from the application.

This either can be done at the point where you create the URLs for the output, e.g. define your base url and assign the local part:

$baseUrl = 'http://localhost/portal/';
$localUrl = 'elements/image.png';
$fullUrl = $baseUrl . $localUrl;

Or you have your own internal URL specification and it get's mapped globally with an output filter (PHP way of doing that).

As you use .htaccess to map the URLs into your application, use it as well to map them back. This can be done with mod_substituteDocs easily.

In your application you use a special URL sheme, e.g. called site://, it will be replaced with your servers base URL http://localhost/portal/ automatically then. The .htaccess code:

<Location />
    AddOutputFilterByType SUBSTITUTE text/html
    Substitute s/site:\/\//http://localhost/portal//nq
</Location>

Upvotes: 1

Related Questions