Domanda

Actually i am trying to make a PHP MVC like application. A basic one. The current milestone i am reaching already includes:

  • Basic RESTful Routing

Means, if i type:

  • www.example.com/items/book/8888

.. it properly just stays there as it is and i can already slice out the URL by slashes / and loads the responsible Controllers .... etc from the top single index.php file. I mean, so it is OK for the backend PHP.

But the only problem is, it still CAN NOT process the REWRITES properly. For example, the CSS & JS are BROKEN as if i VIEW PAGE SOURCE of the page www.example.com/items/book/8888, the asset files are being called as:

  • www.example.com/items/book/8888/css/main.css
  • www.example.com/items/book/8888/js/jquery.js

.. which really are PROBLEMS because in the code is like:

  • <link type="text/css" rel="stylesheet" media="all" href="css/main.css">
  • <script type="text/javascript" src="js/jquery.js"></script>

So the question is:

  • How can i use Apache REWRITE (or whatever approach) to make sure every ASSET FILES to be correctly being called from the DOCROOT.

For example, if i am in the URL:

  • www.example.com/items/book/8888

My ASSET FILES should still be called as:

  • www.example.com/css/main.css
  • www.example.com/js/jquery.js

Or is there any other methods i need to follow? Please kindly help suggest. Thank you.

È stato utile?

Soluzione

In a similar case, we have not found other method than use absolute path for resources files .css, js, .jpg, ... ie, we specify these sub-files with absolute web path by starting from the root slash /:

css:

<link type="text/css" rel="stylesheet" media="all" href="/My-Virtual-Root-Path/css/main.css">

js:

<script type="text/javascript" src="/My-Virtual-Root-Path/js/jquery.js"></script>

.png:

<img src="/My-Virtual-Root-Path/images/logo.png" />

You can now make your rewrite with freedom for main files PHP, HTML, JSP, ... In this case you should remake your script code to fix this.

Using this you should not rewrite resource files, ie, you rewrite only main files by adding this in your vhosts or other apache configuration file:

RewriteCond %{REQUEST_URI} \.(asp|cgi|jsp|php|php3|htm|html|pl)$
RewriteRule [foo] [bar]

Using relative path will be too complex to redirect to the good physical location, like this:

<link type="text/css" rel="stylesheet" media="all" href="css/main.css">
<script type="text/javascript" src="js/jquery.js"></script>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top