1

My setup:

  • Ubuntu 13.04
  • Apache/2.2.22 (Ubuntu)
  • PHP 5.4.9-4ubuntu2.2

--

$ ls /etc/apache2/mods-enabled/*.load
alias.load           auth_basic.load    authn_file.load    authz_default.load
authz_groupfile.load authz_host.load    authz_user.load    autoindex.load
cgi.load             deflate.load       dir.load env.load  expires.load mime.load
negotiation.load     php5.load          reqtimeout.load    rewrite.load
setenvif.load        status.load        userdir.load

Using mod_userdir, which redirects to /home/*/www

I have an .htaccess file in /home/*/www/styles with the following directives:

RewriteEngine On
RewriteRule (styles-files/.+)\.(\d{10})\.(\w{2,4})$ $1.$3 [L]

Now here's the confusing part (personal details masked out)

Loading http://localhost/~***/styles/styles-files/css/jquery.qtip.css works correctly (the file is displayed in the browser)

Loading http://localhost/~***/styles/styles-files/css/jquery.qtip.1376640525.css gives me 404 error, but the message says "/home/***/www/styles/styles-files/css/jquery.qtip.css" not found, so it looks to be redirecting. And that is the correct path to the file.

Rewrite log shows:

(3) [perdir /home/***/www/styles/] strip per-dir prefix: /home/***/www/styles/styles-files/css/jquery.qtip.1376640525.css -> styles-files/css/jquery.qtip.1376640525.css
(3) [perdir /home/***/www/styles/] applying pattern '(styles-files/.+)\\.(\\d{10})\\.(\\w{2,4})$' to uri 'styles-files/css/jquery.qtip.1376640525.css'
(2) [perdir /home/***/www/styles/] rewrite 'styles-files/css/jquery.qtip.1376640525.css' -> 'styles-files/css/jquery.qtip.css'
(3) [perdir /home/***/www/styles/] add per-dir prefix: styles-files/css/jquery.qtip.css -> /home/***/www/styles/styles-files/css/jquery.qtip.css
(1) [perdir /home/***/www/styles/] internal redirect with /home/***/www/styles/styles-files/css/jquery.qtip.css [INTERNAL REDIRECT]

but then right after that, Apache throws an error saying

[error] [client 127.0.0.1] File does not exist: /var/www/home

The rewrite works correctly if I move everything to /var/www. There seems to be some sort of a clash between mod_rewrite and mod_userdir, but I can't find anything about it online.

Any thoughts on what might be happening here and how I can fix this?


After some more reading around, I realized that because the RewriteRule is in an .htaccess file, the resulting substitution is treated as a URL-path rather than a filesystem path because of the implicit PT flag.

Adding a RewriteBase directive made this work, but is not a satisfactory solution for me, because I want this to be portable.

The docs further hint that

The only way to circumvent [the PT flag] is to rewrite to -.

But I haven't been able to get that to work by appending

RewriteRule .* - [L]

and removing the [L] from the previous rule.

Aurimas
  • 131
  • 5

1 Answers1

1

I was able to solve this using RewriteCond and %{REQUEST_URI} which allowed me to reconstruct the URL instead of passing back the system filepath as a URL.

RewriteEngine On
RewriteCond %{REQUEST_URI} ^(/.*?)styles-files/
RewriteRule (styles-files/.+)\.(\d{10})\.(\w{2,4})$ %1$1.$3 [PT]
Aurimas
  • 131
  • 5
  • I think you can drop the `RewriteCond` if you use `RewriteRule ^/(styles-files/.+) ... ` as I noted in my answer below. Give it a try! – KM. Aug 19 '13 at 13:32
  • You should always use a Cond/Rule block anyway, but you could have added a (.*) to the front of your previous Rule, and sucked in the rest of the path that way. In any case, glad it's working. – Chris S Aug 19 '13 at 13:38
  • Thanks, I tried both of those suggestions, but as I explained in my comment to the other answer (which is now gone), since this is in an .htaccess file, the path that the regex is applied to, has been stripped off of the parent directory prefix (as can be seen in the rewrite log). As noted in apache docs, the resulting path _never_ starts with / and since styles-files is in the same dir as .htaccess, there is also nothing else to glob. From the log, you can see that the substitution works correctly, it's just treated as a URL rather than a filesystem path. See bottom of my question. – Aurimas Aug 19 '13 at 17:24