PHP: Best solution for links breaking in a mod_rewrite app

Posted by psil on Stack Overflow See other posts from Stack Overflow or by psil
Published on 2010-03-29T14:21:18Z Indexed on 2010/03/29 14:23 UTC
Read the original article Hit count: 284

I'm using mod rewrite to redirect all requests targeting non-existent files/directories to index.php?url=*

This is surely the most common thing you do with mod_rewrite yet I have a problem:

Naturally, if the page url is "mydomain.com/blog/view/1", the browser will look for images, stylesheets and relative links in the "virtual" directory "mydomain.com/blog/view/".

Problem 1:

  • Is using the base tag the best solution? I see that none of the PHP frameworks out there use the base tag, though.

  • I'm currently having a regex replace all the relative links to point to the right path before output. Is that "okay"?

Problem 2:

It is possible that the server doesn't support mod_rewrite. However, all public files like images, stylesheets and the requests collector index.php are located in the directory /myapp/public. Normally mod_rewrite points all request to /public so it seems as if public was actually the root directory too all users.

However if there is no mod_rewrite, I then have to point the users to /public from the root directory with a header() call. That means, however that all links are broken again because suddenly all images, etc. have to be called via /public/myimage.jpg

Additional info: When there is no mod_rewrite the above request would look like this: mydomain.com/public/index.php/blog/view/1

  • What would be the best solutions for both problems?

© Stack Overflow or respective owner

Related posts about php

Related posts about design-patterns