[ColdBox 3.7.0] Url rwriting settings on Apache in front of Railo Tomcat

I’m trying to setup url rewriting to remove index.cfm from my url.

I’m running an apache server in front of railo tomcat. This are my settings:

Apache virtual host

`

<VirtualHost *:80>
DocumentRoot “E:/wamp/www/myapp”
ServerName www.myapp.test
ServerAlias www.myapp.test
ProxyRequests Off
ProxyPreserveHost On
ProxyPass /app !
<Proxy *>
Order deny,allow
Allow from all

ProxyPass / http://
`

`

www.myapp.test

`

</b>:8888/ ProxyPassReverse / http://<b>

`

www.myapp.test

`

`
:8888/

RewriteEngine On

If it’s a CFML (*.cfc or *.cfm) request, just proxy it to Tomcat:

RewriteRule ^(.+.cf[cm])(/.*)?$ ajp://%{HTTP_HOST}:8009$1$2 [P]

`

my .htaccess

`

RewriteEngine on
#RepeatLimit 0

#SQL Injection Protection --Read More www.cybercrime.gov
#Please uncomment to use these rules if below words does not conflict with your friendly-urls. You may modify accordingly.
#RewriteRule ^.EXEC(@.$ /includes/templates/404.html [L,F,NC]
#RewriteRule ^.CAST(.$ /includes/templates/404.html [L,F,NC]
#RewriteRule ^.DECLARE.$ /includes/templates/404.html [L,F,NC]
#RewriteRule ^.DECLARE%20.$ /includes/templates/404.html [L,F,NC]
#RewriteRule ^.NVARCHAR.$ /includes/templates/404.html [L,F,NC]
#RewriteRule ^.sp_password.$ /includes/templates/404.html [L,F,NC]
#RewriteRule ^.%20xp_.$ /includes/templates/404.html [L,F,NC]

BLOCK unsupported HTTP methods

#RewriteCond %{REQUEST_METHOD} !^(GET|HEAD|OPTIONS|POST|PROPFIND|TRACE)$
#RewriteRule .* - [F]

#You may also want to block proxy attempts, if you see those in your logs:

BLOCK attempts to use our server as a proxy, but allow absolute URIs (change example.com to your domain)

#RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)./?http:// [NC]
#RewriteCond %{THE_REQUEST} !^(GET|HEAD|POST)./?http://(www.)?example.com/
#RewriteRule .* - [F]

#if this call related to adminstrators or non rewrite folders, you can add more here.
RewriteCond %{REQUEST_URI} ^/(.(CFIDE|cfide|CFFormGateway|jrunscripts|railo-context|mapping-tag|fckeditor)).$
RewriteRule ^(.*)$ - [NC,L]

#dealing with flash / flex communication
RewriteCond %{REQUEST_URI} ^/(.(flashservices|flex2gateway|flex-remoting)).$
RewriteRule ^(.*)$ - [NC,L]

#Images, css, javascript and docs, add your own extensions if needed.
RewriteCond %{REQUEST_URI} .(bmp|gif|jpe?g|png|css|js|txt|pdf|doc|xls|ico)$
RewriteRule ^(.*)$ - [NC,L]

#The ColdBox index.cfm/{path_info} rules.
RewriteRule ^$ index.cfm [QSA,NS]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.cfm/%{REQUEST_URI} [QSA,L,NS]

`

My routes

`

// Allow unique URL or combination of URLs, we recommend both enabled setUniqueURLS(false); // Auto reload configuration, true in dev makes sense to reload the routes on every request //setAutoReload(false); // Sets automatic route extension detection and places the extension in the rc.format variable // setExtensionDetection(true); // The valid extensions this interceptor will detect // setValidExtensions('xml,json,jsont,rss,html,htm'); // If enabled, the interceptor will throw a 406 exception that an invalid format was detected or just ignore it // setThrowOnInvalidExtension(true);

// Base URL
if( len(getSetting(‘AppMapping’) ) lte 1){
setBaseURL(“http://#cgi.SERVER_NAME#/”);
}
else{
setBaseURL(“http://#cgi.SERVER_NAME#/#getSetting(‘AppMapping’)#/”);
}

// Your Application Routes
addRoute(pattern="/report", handler=“home”, action=“index”);
//addRoute(pattern="/coldfusion", handler=“seo”, action=“pdfReport”);

addRoute(pattern=":handler/:action?");

`

If I try to reach page without index.cfm like this: http://www.myapp.test/Main I get error from TOMCAT but If I add index.cfm in the url the code works as expected: www.myapp.test/index.cfm/Main/index

So what I should set to make this work without index.cfm in the url?

Many thanks

I am not great with the ,htaccess rules, but it does look fine to me. May I suggest that you look to see if the index.cfm is actually added as a default page.

Do you mean in the web.xml of railo?

In apache, it might be as simple as this

www.mydomain.com throws an document not found where www.mydomain.com/index.cfm actually works. The only reason I say that is the rest of the .htaccess and changes to your route look fine to me.

If I type www.mydomain.com/index.cfm I will be redirect to www.mydomain.com/ and the site works fine. If I try to access to www.mydomain.com/index.cfm/myHandler/myAction all works perfectly.

But if I try to go to www.mydomain.com/myHandler/myAction I got the tomcat error page tell me 404 error page not found…

I know what you are saying, what I am saying is double check your documents in apache and make sure that index.cfm is listed there as a default document. Becuase if you rename the application.cfm so it is not executed and try to browse as I suggested it would show if the default document for index.cfm is indeed the cause.

I have this in my apache httpd.conf

`

DirectoryIndex index.php index.php3 index.html index.htm index.cfm

`

Probably I should use tuckey url rewriter?

if you use that then why use apache?

I don’t use tuckey, I was just asking if is required to rewrite url without index.cfm on railo…

Because I don’t understand what I should set to make it work…

I think that the problem is that tomcat cannot understand that www.myapp.com/main/index should point to www.myapp.com/index.cfm/main/index

Well no it is not, especially if you have Apache or IIS installed and wish to use them otherwise you need to the provide that as a pass through to tomcat which may (I am assuming) cause more issues to your Apache configuration.

In this case like I stated the .htaccess that comes with the likes of ContentBox and other apps by the ColdBox team, all do work right out of the box, especially with what you are talking about. The thing is you need to trace back what maybe causing this, as the first port of call is Apache then we need to eliminate any ColdBox configuration or ColdBox itself.

So when I say rename you application.cfc so that it will not get executed, then try to browse the url www.mydomain.com versus www.mydomain.com/index.cfm then eliminates any configuration issues that Apache might have.

Then you switch off SES and watch the URL and test to see if everything works, then you would need to switxh it back and on go through the .htacces and see where it might be happening. But as it looks right, then I doubt it is the .htaccess but you can verify this further by switching on the logs for your application and Apache to see what both of these are seeing in relation to your rewrite rules.

It can sometimes be a lengthy process to find something like this and it almost always some obscure setting or configuration that you can look at and not see. But at least if you follow the simple dropbacks and eliminate them all one by one then you can safely pin point the cause.

Til then it is like a needle in some hay…

It might even be as simple as did you reinit the framework after modifying the routes.cfm file.

I have changed my virtualhost to this:

`

<VirtualHost *:80>
DocumentRoot “E:/wamp/www/myapp”
ServerName www.myapp.test
ServerAlias www.myapp.test
ProxyRequests Off
ProxyPass /app !
<Proxy *>
Order deny,allow
Allow from all

#ProxyPass / http://www.myapp.test:8888/
#ProxyPassReverse / http://www.myapp.test:8888/

ProxyPreserveHost On
ProxyPassReverse / ajp://%{HTTP_HOST}:8009/

RewriteEngine On

If it’s a CFML (*.cfc or *.cfm) request, just proxy it to Tomcat:

RewriteRule ^(.+.cf[cm])(/.*)?$ ajp://%{HTTP_HOST}:8009/$1$2 [P]

`

And now all seems works…

Wow my Apache config looks nothing like that, why are you proxing back to tomcat like this?

All I can think of is that you have bypassed the jakarta handlers that Adobe provide to go straight to Tomcat, would that be right?

To be honest I don’t know what you’re saying.

I have no experience with this stuff. I saw on http://jamiekrug.com/blog/index.cfm/2009/5/22/url-rewrite-goodies-for-apache-tomcat-railo-and-mura-cms something similar, and find that this works.

I really would like to know what is the best way to accomplish this settings.

So if you or other guys on the forum would share their settings this would be great for all noobs like me.

kk, I missed the Railo part which makes sense then. I think the railo connectors are better than this approach.

Maybe I missed something, but do you have a tag for your virtual hosts? Unless you explicitly allow overrides from an .htaccess file, mod_rewrite won’t work unless you place those rules in your entry. Won’t honor an .htaccess file without the correct settings for the directory.

Railo/Tomcat shouldn’t be at fault in this, whether you use a proxy pass or not. Here’s an example from one of my production sites Running Railo (with the paths and domains changes):

<VirtualHost 1.2.3.4.56:80>
SuexecUserGroup “#501” “#500
ServerName mysite.com
ServerAlias www.mysite.com
DocumentRoot E:/wamp/www/myapp
ErrorLog E:/wamp/www/myapp/logs/error_log
CustomLog E:/wamp/www/myapp/access_log common
ScriptAlias /cgi-bin/ E:/wamp/www/myapp/cgi-bin/

#Home Directory Settings
<Directory E:/wamp/www/myapp>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all

BTW, here’s my Railo.conf file for that server, but you should run the script to install the connector on your own system:

#Railo Connection via Tomcat
<IfModule !mod_jk.c>
LoadModule jk_module /etc/httpd/modules/mod_jk.so

JkMount /*.cfm ajp13 JkMount /*.cfc ajp13 JkMount /*.do ajp13 JkMount /*.jsp ajp13 JkMount /*.cfchart ajp13 JkMount /*.cfm/* ajp13 JkMount /*.cfml/* ajp13 JkMountCopy all JkLogFile /var/log/httpd/mod_jk.log

here’s my Railo.conf file for that server

Could you explain this?

My railo.conf file is the web.xml?

you should run the script to install the connector on your own system

What script I should run?

Thanks!

wsconfig same as Windows in your runtime/bin folder.

Replies inline, below:

here’s my Railo.conf file for that server

Could you explain this?

That’s the railo.conf file to connect Apache to Railo/Tomcat as the handler for CFML files. You can also place the code directly in to httpd.conf, but I prefer to place it with my other included *.conf files.

My railo.conf file is the web.xml?

web.xml is to configure Railo and Tomcat. That configures your applications. Apache has its own configuration and to serve CFML, it needs to be configured to connect to the server - unless you want to use mod_proxy, which you’ve been doing.

you should run the script to install the connector on your own system

What script I should run?

Oops. On Windows, I don’t believe there’s a connector script for Apache on Windows (probably because it expects IIS to be the web server). My bad. Here are some instructions for installing mod_jk on Windows and connecting Apache: http://kisdigital.wordpress.com/2011/06/03/quickly-connect-apache-httpd-to-railo-on-windows/