Forum Moderators: phranque

Message Too Old, No Replies

.php files not loading with mod rewrite

.php files not loading with mod rewrite

         

janet901

11:19 am on Aug 16, 2005 (gmt 0)

10+ Year Member



I've tried several things to fix this without any luck. I was hoping someone here might have a better idea.

.php files on my server load perfectly when using the direct file path www.example.com/subdomain/test.php.

But when I try accessing it using a rewrite rule in my httpd file it says it cannot the file because its either unavailable or cannot be found.

I get the following in my rewrite log

init rewrite engine with requested uri /subdomain//index.html
applying pattern '^(.*)' to uri '/subdomain//index.html'
pass through /leeching//index.html

init rewrite engine with requested uri /subdomain//index.php
applying pattern '^(.*)' to uri '/subdomain//index.php'
pass through /leeching//index.php

Theres nothing in the error log. I think the problem lies with the double slash, but have not been able to get rid of it. When I use an external rewrite it works perfectly, only thing is I want an internal rewrite.

My Rewrite Code is:

RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.example.com(:80)?<>/([^/]*) [NC]
RewriteCond %1<>%3!^(.*)<>\1$ [NC]
RewriteRule ^(.*) /%1/$1 [NC,PT]

I'm hoping this is a silly small error on my behalf.

jdMorgan

1:03 pm on Aug 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I'm hoping this is a small error

Yes, but an important one.

.htaccess is referred to as a "per-directory" context. Therefore, in .htaccess, the requested URL-path "seen" by mod_rewrite's RewriteRule when matching against patterns are stripped of the path to the local directory.

For example. if you request "example.com/foo/bar/quux/test.html," and your .htaccess file is in the /foo subdirectory, the requested local URL-path "seen" by RewriteRules in that file will be "bar/quux/test.html"

If located in the foo/bar/quux subdirectory, then the RewriteRule would "see" only "index.html".

Likewise, requested URL-paths for resources located in your top-level Web directory are stripped of their leading slashes.

In httpd.conf, the requested URL seen by RewriteRule is the entire requested URL path, because you are now at the very "top" of the path. So the pattern used to check URLs needs to include the leading slash:


RewriteRule [b]^/([/b].*) /%1/$1 [NC,PT]

As posted in another thread here, the code you found relies on POSIX 1003.2 regular expression support in your operating system. It is therefore not very portable, since POSIX 1003.2 regex is supported only on some *nix OSes.

Also, the purpose of the code that relies on advanced regex is to prevent looping in an .htaccess context. It is not needed if the code is in httpd.conf. A simplified and portable routine for httpd.conf would be:


RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule ^/(.*) /%1/$1 [L]

Jim

janet901

6:08 pm on Aug 16, 2005 (gmt 0)

10+ Year Member



Thanks for explaining that, Jim and its cleared up the double slash problem, but has not sorted the .php parsing problem.

I thought the problem lay with the mod rewrite, as only then did the browser try to download the .php files rather than parse them. I think that the problem may actually lie with the VirtualHost setup.

I've been googling and can't find anyone else with this problem. My php.conf are in a seperate file included at the top of my httpd file. They're working fine because normally php parses ok. I found this in my error log:
'mod_include: Options +Includes (or IncludesNoExec) wasn't set, INCLUDES filter removed,' so I added 'Options All'. Now the error log simply says 'Directory index forbidden by rule: /home/httpd/vhosts/domain.com/httpdocs/' whenever I type in an url with a .php. Although mod rewrite finds my .html files fine!

These are my VirtualHost configurations

<VirtualHost 111.111.111.1;80>
DocumentRoot /home/httpd/vhosts/domain.com/httpdocs
ServerName domain.com
ServerAlias domain.com
Options +FollowSymlinks
<Directory /home/httpd/vhosts/domain.com/httpdocs>
Options All
Order allow,deny
allow from all
</Directory>

RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule ^/(.*) /%1/$1 [L]

</VirtualHost>

I've tried lots of different things and I'm hoping somebody may have come accross this same problem as I'm running out of options. I'm hoping its something small I've missed. This forum is excellent btw, I'm so impressed by the expertise and the high level of expertise.

Em

jdMorgan

6:58 pm on Aug 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try re-adding the [PT] flag, and also try adding [T=application/x-httpd-php]

That is, try both of these flag combinations on the RewriteRule:


[PT,L]
[PT,T=application/x-httpd-php,L]

Jim

janet901

2:49 am on Aug 18, 2005 (gmt 0)

10+ Year Member



I tried those two options, but still no luck. Although the php files are parsing at
[domain.com,...] the browser attempts to download them at [domain.com....] This is
must be where the rewrite problems are coming from. All .html files are loading ok.

My virtual host configurations are:
<VirtualHost 000.000.000.0:80>
DocumentRoot /home/httpd/vhosts/domain.com/httpdocs
ServerName domain.com
ServerAlias domain.com
Options +FollowSymlinks
<Directory /home/httpd/vhosts/domain.com/httpdocs>
Order allow,deny
allow from all
AllowOverride All
</Directory>
</VirtualHost>

If I add *.domain.com to the ServerAlias it attempts to download the .php files at www.domain.com as well.
Is there a php safe mode or something that I have to turn off to avoid this. Thanks again for your suggestions!

[edited by: jdMorgan at 3:12 am (utc) on Aug. 18, 2005]
[edit reason] Obscured specifics. [/edit]

coopster

3:41 pm on Aug 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What do you have in your www.domain.com <VirtualHost> container that is different from this container?

janet901

7:00 pm on Aug 19, 2005 (gmt 0)

10+ Year Member



I've finally figured out what the problem is. I'm using Fedora 2 & Plesk 7.5. Even though I was putting my configurations in the httpd.conf file, in the <VirtualHost> section provided, they were being over writen by Plesk.

I've listed the steps here on how to create subdomains to subdirectorys on the fly, incase anybody else is struggling with Plesk.

Create file vhost.conf at home/httpd/vhosts/example.com/conf/vhost.conf

Insert the following code:
<IfModule mod_rewrite.c>
ServerAlias *.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^(www¦admin¦control\.) [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule ^/(.*) /%1/$1 [L]
</IfModule>

Run the following:
/usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=example.com

Restart with command shutdown -r now

coopster

10:03 pm on Aug 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



And yet another reason I hate control panels so much ;)