1

I have a website running at localhost/pm and the RewriteBase is correctly set to /pm/. There is a link tag in my document: <link ... href="themes/default/css/default.css">.

When the url is localhost/pm or localhost/pm/foo the CSS works all right. When there are more slashes in the URL, however, like localhost/pm/foo/bar the relative URL if the stylesheet changes to foo/themes/default/css/default.css.

How do I get this to work without having to put some sort of PHP path resolution in the link tag?

# invoke rewrite engine
RewriteEngine On
RewriteBase /pm/

# Protect application and system files from being viewed
RewriteRule ^(?:system)\b.* index.php/$0 [L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

EDIT:

Basically what I need now is this: If request contains folder name /themes/ scrap everything that is before /themes/ and rewrite the rest to /pm/themes/...

I tried it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/$1 but I get an internal server error. Why?

If I do it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/ (ie. just remove $1 from the end) and use the URL http://localhost/pm/foo/themes/foo/ the resulting physical location is http://localhost/pm/themes which is what is expected too, which in turn means that at least my regex is correct. What am I missing?

Simon Josef Kok
  • 680
  • 1
  • 8
  • 20
  • You must exclude the css from being rewritten with a RewriteCond, see this answer http://stackoverflow.com/a/15348786/1741542 or rewrite CSS requests as well, see http://stackoverflow.com/a/15535329/1741542 – Olaf Dietsche Mar 23 '13 at 17:20
  • Didn't help. Using a tag did, however. – Simon Josef Kok Mar 23 '13 at 17:37
  • Be careful, when using the `base` tag, see http://stackoverflow.com/q/1889076/1741542 for details. If your css files are located below `/pm/themes/default/css/`, you can rewrite these as well. – Olaf Dietsche Mar 23 '13 at 17:53
  • @OlafDietsche Ouch, the anchor issue is a bad thing. Any ideas why the solution in your first comment does not work for me? Could it be a server config issue? – Simon Josef Kok Mar 23 '13 at 18:36
  • According to this similar question: http://stackoverflow.com/a/3701958/1383913 my problem can't be solved with so far suggested measures. I have elaborated my question further, please see the edit. – Simon Josef Kok Mar 23 '13 at 20:23

2 Answers2

1

The RewriteRule is almost correct

RewriteRule ^.*(/themes/.*)$ /pm/themes/$1

This rewrites http://localhost/pm/foo/themes/default/css/default.css to http://localhost/pm/themes/themes/default/css/default.css, which is one themes too much. Use this instead

RewriteRule /themes/(.*)$ /pm/themes/$1 [L]

But now you have an endless rewrite loop, because /pm/themes/.. is rewritten again and again. To prevent this, you need a RewriteCond excluding /pm/themes

RewriteCond %{REQUEST_URI} !^/pm/themes/
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]

Now the request is rewritten only once and you're done.

Olaf Dietsche
  • 66,104
  • 6
  • 91
  • 177
0

You probably need to add the following lines before your RewriteRule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

It will only evaluate your rewrite rule if the requested file or directory doesn't exist.

You should post your .htaccess file so we can offer better advice

Will Warren
  • 1,264
  • 14
  • 32