Monday, May 18, 2015

apache 2.2 - RewriteRule not working with mod_userdir



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.


Answer



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]

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...