1

Here comes a question because my current solution is not really satisfying me for the following problem:

I have an own PHP Framework with MVC pattern in development. And my Router works perfect and all but I have one question I could not find a solution for.

Well I route every incoming request to index.php file which is located in the base-path of my framework. Ofcourse it is no problem to work with relative paths when including css such as:

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

This works perfect in the browser. Ofcourse it does not matter what I enter in the URL because every request gets redirected as stated above to the index to make a reasonable routing possible.

However when my url contains multiple slashes which look like subfolders, for example: "/manual/details/1_2" then I get a normal routing process but the browser cannot find the css file unless I add "../" for each "/" in my url to map backwards to my base-path.

For example for the above URL this would work instead:

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

My curent solution:

I wrote a PHP function in my Routing class that determines the required amount of "../" pattern and I always cast the function before implementing a resource to build the exact path at any time.

Example:

<link rel="stylesheet" type="text/css" href="<?=Router::getInstance()->getSubdirectoryPrefix()?>style/include/css/style.css" />

Needless to say that this is very unhandy and also sucks if you forget to place that function. If your route ever changes or you forget that you are in a subdirectory you will wonder why your resource could not be found.

I also know about putting an alias such as Alias /public style/ in my Virtual-Host configuration of Apache but I want to find another - project and PHP internal way without having an unhandy crap such as pasting the alias function all the time and without setting up a virtual-host option so the framework can stay lightweight and does not require any nasty external options like modifying Virtual-Host.

I'd love to hear your solutions, best would be .htacces - oh and by talking over it I leave my .htaccess code here aswell:

RewriteEngine On

Options -Indexes

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]

Thanks in advance for your help~

1 Answer 1

1

You must use absolute paths instead of relative paths for your html links (css, javascript, images, etc).

For example:

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

The leading slash (before style) means to begin from document root folder, then go into style folder, and so on... (you may have another prefixed directory if it's not in root folder)

You had some problems because some of your rules can create virtual directories (e.g: http://domain.com/some/directory/subdirectory/etc/).

Also, in your htaccess, it wouldn't hurt to use a leading slash (or a RewriteBase)

Options -Indexes
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /index.php [L]

or (both are the same)

Options -Indexes

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]
1
  • 1
    Lol this was way too easy...Thanks alot this worked like a charm!
    – Steini
    Commented Sep 3, 2014 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.