Apache and .cfm mod_rewrite not working


#1

Hi there, I have a problem, my .htaccess file is configured to pass various paths to cfm pages, however, I am also trying to pass matches which include something.cfm to a .cfm
It seems that Lucee is processing the .cfm before mod_rewrite so it never hits the .htaccess rule.

I have two questions, is it possible to have mod_rewrite intercept .cfm before lucee?

Or
Is there a way to handle .cfm to .cfm rewrites at a lucee level?
I am using Lucee on a Linux apache/tomcat setup

Many thanks


#2

Sounds like perhaps the .htaccess isn’t enabled?

<Directory "/var/www"> 
AllowOverride All
</Directory>

Lucee only gets passed requests after apache has done all it’s own magic


#3

Thanks, I think it is as some rules are working.


#5

What we think is happening is that any .cfm extension is being nabbed by Lucee before any htaccess rules so we can’t put anything that operates on .cfm in the left hand condition of ReWriteRule as it wont get picked up.


#6

what’s your apache config like? mod_cfml and ajp?

something like this?

ProxyPreserveHost On
ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$ ajp://localhost:8009/$1$2

#7

Yep, thats it, I just tried moving the cfm to cfm mod_rewrite rule to the virtual host:

 <VirtualHost *:80>
 DocumentRoot /opt/lucee/tomcat/webapps/mydomain/mydomain.com

 ServerName mydomain.com

 ServerAlias www.mydomain.com

 RewriteEngine on

 ReWriteCond %{REQUEST_URI} !^/synth/cfman.cfm

 ReWriteCond %{REQUEST_URI} !^/synth/index.cfm

 RewriteRule ^synth/([^/.]+).cfm$ /synth/_inc/synth.cfm?file=$1 [L]

 ProxyPreserveHost On

 ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$ ajp://localhost:8009/$1$2
 </VirtualHost>

And that didnt work (removed the rules from .htaccess)
I get a too many redirects and a cftoken gets stuck on the end (not something we are using generally) - its the same result as when in .htaccess, I would rather keep everything in the codebase so that its easier to version control, but if it does have to go in httpd.conf then so be it


#8

I am at a loss, would rather not have to install URLReWrite as I only need a couple and its only for one virtual host.


#9

I think this is configured correctly?

  Options Indexes FollowSymLinks
  AllowOverride All
  Order allow,deny
  Allow from all

#10

looks about right, have you got the right modules loaded too?

you can raise the log level to debug to see what’s kicking in


#11

I would put that in a .htaccess file or make sure your rewrite rules are loaded after your mod_cfm or mod_proxy rules.

plus if your virtual host is running CFML, then you have to have the allow and order rules in the virtual host directive, or it will only work for your global default web configuration.

<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot /var/www/DocRoot
<Directory /var/www/DocRoot >
AllowOverride All
</Directory>
IfModule proxy_module
 ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$ ajp://localhost:8009/$1$2
IfModule
RewriteEngine on 
ReWriteCond %{REQUEST_URI} !^/synth/cfman.cfm 
ReWriteCond %{REQUEST_URI} !^/synth/index.cfm RewriteRule ^synth/([^/.]+).cfm$ /synth/_inc/synth.cfm?file=$1 [L] 
ErrorLog /var/log/httpd/mydomain-error_log
CustomLog /var/log/httpd/mydomain-access_log common
</VirtualHost>

#12

Thanks Terry, I’ll try that, though I’m a little confused as to which mod_ is doing the connecting now.
Below my virtual host for the site, there is this:

 <VirtualHost *:80>     
DocumentRoot /mydoc root 
ServerName mydomain.com
ServerAlias www.mydomain.com
RewriteEngine on
ReWriteCond %{REQUEST_URI} !^/synth/cfman.cfm 
RewriteRule ^synth/([^/.]+).cfm$ /synth/_inc/synth.cfm?file=$1 [L]
<Directory /opt/lucee/tomcat/webapps/sonicstate/sonicstate.com>
AllowOverride All
Order allow,deny
Allow from all
</Directory> 
<IfModule proxy_module.c>
ProxyPassMatch ^/(.+.cf[cm])(/.)?$ ajp://localhost:8009/$1$2 
</IfModule>
</VirtualHost>

<IfModule mod_proxy.c>
ProxyPreserveHost On
ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$ http://127.0.0.1:8888/$1$2
ProxyPassMatch ^/(.+\.cfchart)(/.*)?$ http://127.0.0.1:8888/$1$2
ProxyPassMatch ^/(.+\.cfml)(/.*)?$ http://127.0.0.1:8888/$1$2
# optional mappings    
#ProxyPassMatch ^/flex2gateway/(.*)$ http://127.0.0.1:8888/flex2gateway/$1
#ProxyPassMatch ^/messagebroker/(.*)$ http://127.0.0.1:8888/messagebroker/$1
#ProxyPassMatch ^/flashservices/gateway(.*)$ http://127.0.0.1:8888/flashservices/gateway$1
#ProxyPassMatch ^/openamf/gateway/(.*)$ http://127.0.0.1:8888/openamf/gateway/$1
#ProxyPassMatch ^/rest/(.*)$ http://127.0.0.1:8888/rest/$1
ProxyPassReverse / http://127.0.0.1:8888/
</IfModule>
LoadModule modcfml_module modules/mod_cfml.so
CFMLHandlers ".cfm .cfc .cfml"
ModCFML_SharedKey "some random string"
LogHeaders false
LogHandlers false
LogAliases false
VDirHeader false

I think this gets auto added when installing Lucee (its been a while). does this seem right?
Its wierd, but on the dev server, if I comment out the ajp:// lines in the virtual host, CFM still gets served, I’m wondering if somehow this aint right…


#13

It should be mod proxy and then your re-write rules.


#14

OK, tried that, put the <IfModule mod_before the virtual host, put the rewrite rules after the proxy_module inside the virtualhost, restarted everything, cleared browser cache etc - still getting the problem with the cfm redirect rule, the resulting page appends the CFTOKEN and CFID to the url and throws a too many redirects error


#15

Set mod_rerwite log to level 4
output is multiple lines like this, till I close the page.

(timestamp/IP stuff) init rewrite engine with requested uri /synth/accesindigo.cfm
 (timestamp/IP stuff) (1) pass through /synth/accesindigo.cfm
 (timestamp/IP stuff) (2) pass through /synth/accesindigo.cfm

You can see that its not applying the mod_rewrite rule this just enters loop of lines like this (1), (2) and we get the too many redirects loop. This is driving me a bit mad now.


#16

Sorry about this, next test: I removed the rules from htaccess and put them directly in the virtual host, log just passes it through, applies no rules.

RewriteEngine on
ReWriteLog /opt/rw.log
ReWriteLogLevel 3
#Main directory path to cfm template (working)
ReWriteCond %{REQUEST_URI} !^/synth/cfman.cfm
ReWriteCond %{REQUEST_URI} !^/synth/index.cfm
ReWriteCond %{REQUEST_URI} !^/synth/cfsynth.cfm
RewriteRule ^synth/([^/.]+)/$ /synth/_inc/synth.cfm?file=$1 [L]

This block works in .htaccess, but not in the virtual host wrapper rules written in this block do not seem to apply at all


#17

If it works in the .htaccess and not in the directive, then issue is where the DocRoot is for your virtual directive and when the rewrite rules are being applied.

Apache HTTPD Server configuration is applied sequentially.

You more than likely have your Virtual Directive applied before you have you have your load modcfml.

You need to switch it so your modcfml is loaded first, then your virtual configuration.


#18

Thanks Terry, currently the order in httpd.conf is:

<IfModule mod_proxy.c>
ProxyPreserveHost On
ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$ http://127.0.0.1:8888/$1$2 
ProxyPassMatch ^/(.+\.cfchart)(/.*)?$ http://127.0.0.1:8888/$1$2
ProxyPassMatch ^/(.+\.cfml)(/.*)?$ http://127.0.0.1:8888/$1$2
ProxyPassReverse / http://127.0.0.1:8888/
</IfModule>

LoadModule modcfml_module modules/mod_cfml.so
CFMLHandlers ".cfm .cfc .cfml" 
ModCFML_SharedKey "randomstringkey"
LogHeaders false
LogHandlers false
LogAliases false
VDirHeader false

then my virtual host block which contains the Directory info:

<VirtualHost *:80>
DocumentRoot /opt/lucee/tomcat/webapps/mydonain/mydomain.com
ServerName mydomain.com
ServerAlias www.mydomain.com
<Directory "/opt/lucee/tomcat/webapps/mydomain/mydomain.com/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<IfModule proxy_module.c>
ProxyPreserveHost On
ProxyPassMatch ^/(.+.cf[cm])(/.)?$ ajp://localhost:8009/$1$2
</IfModule>
RewriteEngine on
ReWriteLog /opt/rw.log
ReWriteLogLevel 3
#NEW BLOCK NB SYNTH MODELS (not working in htaccess_
ReWriteCond %{REQUEST_URI} !^/synth/cfman.cfm
ReWriteCond %{REQUEST_URI} !^/synth/index.cfm
ReWriteCond %{REQUEST_URI} !^/synth/cfsynth.cfm
RewriteRule ^synth/([^/.]+).cfm$  /synth/$1/ [L]
#Main directory path to cfm template (works in htacces but not here)
#ReWriteCond %{REQUEST_URI} !^/synth/cfman.cfm 
#ReWriteCond %{REQUEST_URI} !^/synth/index.cfm
#ReWriteCond %{REQUEST_URI} !^/synth/cfsynth.cfm 
#RewriteRule ^synth/([^/.]+)/$ /synth/_inc/synth.cfm?file=$1 [L]
</VirtualHost>

This is not working. According to what you are saying, it should right?


#19

This should be

LoadModule modcfml_module modules/mod_cfml.so
CFMLHandlers ".cfm .cfc .cfml" ModCFML_SharedKey "randomstringkey"
LogHeaders false
LogHandlers false
LogAliases false
VDirHeader false

Then

Virtual Directive

Inside Virtual Directive

LoadModule modcfml_module modules/mod_cfml.so
CFMLHandlers ".cfm .cfc .cfml"
ModCFML_SharedKey 

Then the Rewrite Rules


#20

Thanks again for your patience;
Okay so using that, I get an error when restarting the httpd - calling mode_cfml twice,
so I binned the call outside the virtual directive.
But the rewrite not working still from inside the virtual directive.

Just checking inside the virtual directive means this:

<VirtualHost etc...>
stuff
<Directory etc>
stuff
</Directory>
LoadModule modcfml_module modules/mod_cfml.so
CFMLHandlers ".cfm .cfc .cfml"
Rewrite stuff
</VirtualHost>

#21

Yes that is how it should work.

Is this Ubuntu or RHEL / CEntos?

usually only two reasons mod_rewrite will not work in a directive but works perfectly as .htaccess file
bad order or bad permissions.

is Apache HTTPD and Lucee the same user or the same permissions group?