I am trying to get Apache to rewrite the URL based on certain conditions but it is failing. I have tried searching for the answer but have come up empty handed. We are using Apache as our webserver and proxying the requests to Tomcat for using our Coldfusion/Lucee code.
Basically what is happening is that the rewrite is working fine for when I am accessing an HTML file but when I try to access a CFM or CFC (Coldfusion) file it will ignore the rewrite rule completely. What I want is to have the URL rewritten before passing through the proxy but for some reason I can not get that to work.
Thanks in advance to anyone that can help me solve this.
This is in my httpd.conf file:
Here is the Virtual Host file:
<VirtualHost *:80>
ServerName dev.xxxxx.com
DirectoryIndex default.cfm index.cfm index.htm index.html
DocumentRoot "Z:/XXXXXXXXX"
<Directory "Z:/XXXXXXXXX">
Require all granted
Allow from all
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
</Directory>
<Proxy *>
Allow from 127.0.0.1
</Proxy>
RewriteEngine On
DirectoryIndex index.cfm
ProxyPreserveHost On
ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$ ajp://localhost:7009/$1$2
</VirtualHost>
Snippet towards the bottom of httpd.conf
<IfModule proxy_module>
<Proxy *>
Allow from 127.0.0.1
</Proxy>
ProxyPreserveHost On
ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$ ajp://127.0.0.1:7009/$1$2
ProxyPassMatch ^/(.+\.cfchart)(/.*)?$ ajp://127.0.0.1:7009/$1$2
ProxyPassMatch ^/(.+\.cfml)(/.*)?$ ajp://127.0.0.1:7009/$1$2
ProxyPassReverse / ajp://127.0.0.1:7009/
</IfModule>
This is in .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^(.*) /index.cfm/$1 [L]
You will need to use the [P] flag which causes the request to be handled by mod_proxy, and handled via a proxy request.
For example
RewriteRule ^(.*) /index.cfm/$1 [L]
becomes
RewriteRule ^(.*) /index.cfm/$1 [PL]
or if you need the first forward slash like
RewriteRule ^/(.*) /index.cfm/$1 [PL]
Also take a look at the [PT] flag.
https://httpd.apache.org/docs/current/rewrite/flags.html
Updating the answer with what I have working in my Apache config file (I don't have a virtual hosts config and I don't have .htaccess because I don't need them in my case)
My rule is that if I have /x/ in request url then I call index.cfm from /xml/ subfolder.
ProxyRequests Off
ProxyVia Off
ProxyPreserveHost On
<Proxy *>
AddDefaultCharset off
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
RewriteEngine On
RewriteRule ^/x/(.*)$ /xml/index.cfm [PT]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With