开发者

Problems with mod_rewrite and mod_userdir

I'm trying to get a local copy of a website running that someone else put together. The site is deployed and functional and I'm trying to get a development version set up under my public_html directory.

The site uses mod_rewrite to redirect all traffic through a gateway script that performs various initialization steps. I have a strange problem where links including the .php extention work, but those without die with the following exception:

Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0 Fatal error: Unknown: Failed opening required 'redirect:/~george/site/.fiona/core/gateway.php' (include_path='.:/usr/share/php:/usr/share/pear') in Unknown on line 0

Here is a copy of the .htaccess file (edited for my local installation)

RewriteEngine On
Options +FollowSymLinks

RewriteBase /~george/site/
RewriteCond %{REQUEST_URI} !.fiona/core/gateway.php
RewriteRule ^\.fiona/([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$ .fiona/core/gateway.php?group=$1&destination=$2\.php [L,QSA]
RewriteCond %{REQUEST_URI} !.fiona/core/gateway.php
RewriteRule ^([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$ .fiona/core/gateway.php?group=$1&destination=$2\.php [L,QSA]
RewriteCond %{REQUEST_URI} !.fiona/core/gateway.php
RewriteRule ^([a-zA-Z0-9_\-]+)(\.php)?$ .fiona/core/gateway.php?destination=$1\.php [L,QSA]

Here is what I see in my mod_rewrite logs

localhost/~george/site/login

127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/login.php -> login.php
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) [perdir /home/george/public_html/site/] applying pattern '^\.fiona/([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'login.php'
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/login.php -> login.php
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'login.php'
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/login.php -> login.php
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'login.php'
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (4) [perdir /home/george/public_html/site/] RewriteCond: input='/~george/site/login.php' pattern='!.fiona/core/gateway.php' => matched
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (2) [perdir /home/george/public_html/site/] rewrite 'login.php' -> '.fiona/core/gateway.php?destination=login.php'
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) split uri=.fiona/core/gateway.php?destination=login.php -> uri=.fiona/core/gateway.php, args=destination=login.php
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (3) [perdir /home/george/public_html/site/] add per-dir prefix: .fiona/core/gateway.php -> /home/george/public_html/site/.fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (2) [perdir /home/george/public_html/site/] trying to replace prefix /home/george/public_html/site/ with /~george/site/
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (5) strip matching prefix: /home/george/public_html/site/.fiona/core/gateway.php -> .fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (4) add subst prefix: .fiona/core/gateway.php -> /~george/site/.fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#211168d0/subreq] (1) [perdir /home/george/public_html/site/] internal redirect with /~george/site/.fiona/core/gateway.php [INTERNAL REDIRECT]
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#21103668/initial] (3) [perdir /home/george/public_html/site/] applying pattern '^\.fiona/([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'redirect:/~george/site/.fiona/core/gateway.php'
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#21103668/initial] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'redirect:/~george/site/.fiona/core/gateway.php'
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#21103668/initial] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'redirect:/~george/site/.fiona/core/gateway.php'
127.0.0.1 - - [05/Oct/2011:08:47:37 --0400] [localhost/sid#20f41ce8][rid#21103668/initial] (1) [perdir /home/george/public_html/site/] pass through redirect:/~george/site/.fiona/core/gateway.php

localhost/~george/site/login.php

127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/login.php -> login.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) [perdir /home/george/public_html/site/] applying pattern '^\.fiona/([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'login.php'
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/login.php -> login.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'login.php'
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/login.php -> login.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-zA-Z0-9_\-]+)(\.php)?$' to uri 'login.php'
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (4) [perdir /home/george/public_html/site/] RewriteCond: input='/~george/site/login.php' pattern='!.fiona/core/gateway.php' => matched
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (2) [perdir /home/george/public_html/site/] rewrite 'login.php' -> '.fiona/core/gateway.php?destination=login.php'
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) split uri=.fiona/core/gateway.php?destination=login.php -> uri=.fiona/core/gateway.php, args=destination=login.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (3) [perdir /home/george/public_html/site/] add per-dir prefix: .fiona/core/gateway.php -> /home/george/public_html/site/.fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (2) [perdir /home/george/public_html/site/] trying to replace prefix /home/george/public_html/site/ with /~george/site/
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (5) strip matching prefix: /home/george/public_html/site/.fiona/core/gateway.php -> .fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (4) add subst prefix: .fiona/core/gateway.php -> /~george/site/.fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224bf668/initial] (1) [perdir /home/george/public_html/site/] internal redirect with /~george/site/.fiona/core/gateway.php [INTERNAL REDIRECT]
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224c97b8/initial/redir#1] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/.fiona/core/gateway.php -> .fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224c97b8/initial/redir#1] (3) [perdir /home/george/public_html/site/] applying pattern '^\.fiona/([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri '.fiona/core/gateway.php'
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224c97b8/initial/redir#1] (4) [perdir /home/george/public_html/site/] RewriteCond: input='/~george/site/.fiona/core/gateway.php' pattern='!.fiona/core/gateway.php' => not-matched
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224c97b8/initial/redir#1] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/.fiona/core/gateway.php -> .fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/开发者_如何学JAVAsid#222fdce8][rid#224c97b8/initial/redir#1] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-z]+)/([a-zA-Z0-9_\-]+)(\.php)?$' to uri '.fiona/core/gateway.php'
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224c97b8/initial/redir#1] (3) [perdir /home/george/public_html/site/] strip per-dir prefix: /home/george/public_html/site/.fiona/core/gateway.php -> .fiona/core/gateway.php
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224c97b8/initial/redir#1] (3) [perdir /home/george/public_html/site/] applying pattern '^([a-zA-Z0-9_\-]+)(\.php)?$' to uri '.fiona/core/gateway.php'
127.0.0.1 - - [05/Oct/2011:08:58:18 --0400] [localhost/sid#222fdce8][rid#224c97b8/initial/redir#1] (1) [perdir /home/george/public_html/site/] pass through /home/george/public_html/site/.fiona/core/gateway.php

The links without the .php extension seem to make the initial redirect successfully, however, for some reason they appear to be unable to reroute to the absolute path.

I am by no means an expert on mod_rewrite, so there could very well be other issues here. Any ideas would be great.

EDIT Based on Marius' answer, I believe I've narrowed this down to an issue with mod_userdir in combination with mod_rewrite. Moving the site to /var/www resolves the problem. I would still like to get this working in my public_html directory.

EDIT Marius lead me closer to the solution in sites enabled. It turns out the problem was that MultiViews (allows apache to interpret index as index.php) was not enabled for user directories. Adding the following to my default sites enabled (/etc/apache2/sites-enabled/000-default) resolved the issue with .php extensions, but disables mod_rewrite.

<Directory /home/*/public_html>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory> 

My entire sites enabled follows

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
    <Directory /home/*/public_html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>


I have seen a lot of these problems when using the "userdir" module (the module that makes you able to use ~username). Could you try this exact same thing with a "normal" directory, that is not using the userdir module?

Just re-created your site with the following structure:

marius@debiansrv:~/public_html/htaccesstest$ find ./.fiona ./.fiona/core ./.fiona/core/gateway.php ./.htaccess

Here the htaccess file is 100% the same as yours, and it works fine. You can check this here: http://home.goodworkaround.com/~marius/htaccesstest/

Are you using ".fiona" as folder name? I suspect you may be using "fiona". Also, if this is not the case, you should paste the site settings from Apache.


It turns out the issue was with my sites enabled file. Apparently user directories were not configured properly to allow mod_rewrite to work. Copying the VirtualHost settings for /var/www/ with minor edits resolved the issue.

<Directory /home/*/public_html>
    Options Indexes FollowSymLinks
    AllowOverride all
    Order allow,deny
    allow from all
</Directory>


It will be looking for what the base directory of where you are hosting the files is located, relative to the URL. So if your site is actually at http://example.com/~george/site/, your RewriteBase would be correct. After that, it will be looking for something in that directory, and then based on your rules, at RewriteBase/.fiona/core/gateway.php, which it can't find. I believe that if you put the .fiona directory in site/, it should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜