Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a problem with an old project for which I have the following mod_rewrite conditions/rules:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

But .phtml files are being rendered as plain text in the browser.

The problem is that I have some .phtml files within a public directory that are matching the first rewrite condition '-s' (is regular file, with size) and so they're not getting sent to index.php. In fact, the .phtml files are showing as plain text! I need to keep those files there so I can't set deny from all for that directory because it also contains some images and stuff.

I tried this:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
RewriteRule \.phtml$ index.php [NC,L]

But it didn't work because it already met the first rewrite condition, so the first rule got applied (to do nothing).

How can I get around that?

share|improve this question

migrated from stackoverflow.com Dec 9 '11 at 15:50

This question came from our site for professional and enthusiast programmers.

    
Can't you just put the \.phtml$ rule first...? –  DaveRandom Dec 9 '11 at 8:57
    
Yeah putting it first worked. Thanks –  mmmshuddup Dec 9 '11 at 9:48

2 Answers 2

up vote 2 down vote accepted

I think that rule and .htaccess is not a condition here. You should configure apache tu use phtml files with php:

AddType application/x-httpd-php .php .phtml .html

then the rule

RewriteRule \.phtml$ index.php [NC,L]

can be ommited and it should work, because file exists and have size, so apache should handle it like any other PHP file.

edit: If you don't want to serve .phtml files at all, you culd use rewrite like that:

RewriteCond %{REQUEST_FILENAME} \.phtml$
RewriteRule ^.*$ index.php [NC,L]

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
share|improve this answer
    
+1 That was it! I just assumed it was already set that way (where .phtml files were treated as php), in my MAMP configuration it was like that, but I switched to Zend Server CE and in Zend that wasn't set by default. Incidentally, I'm still leaving the rewrite rule there (at the top) for my own reasons. in directories where I need to run phtml files directly I can just add RewriteEngine off. –  mmmshuddup Dec 9 '11 at 9:52

You said: "But it didn't work because it already met the first rewrite condition, so the first rule got applied (to do nothing)."

And that's quite right, because the L flag in rule tells apache that if it is applied, other rules should not be applied.

It is commonly used for redirects and such, but in your case this flag is not needed.

Try this configuration:

RewriteCond %{REQUEST_FILENAME} !-s [AND]
RewriteCond %{REQUEST_FILENAME} !-l [AND]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.