Do you know if anyone has documentation on creatin...
# cfml-general
g
Do you know if anyone has documentation on creating a rewrite rule with Lucee and Linux to remove index.cfm from the URL? I have tried various rules, but they all result in a 404 error and I suspect that the server does not redirect the request to Lucee for processing.
g
Try this <IfModule mod_rewrite.c> RewriteEngine On # Remove CFML extension and redirect to clean URL RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.cfm -f RewriteRule ^(.+)\.cfm$ /$1/ [R=301,L,QSA] # Transform query string parameters to path segments RewriteRule ^(.+)/([^/]+)/([^/]+)(/.*)?$ %{REQUEST_URI}?r=$2&m=$3 [L,QSA] # Serve actual CFML files internally RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)$ $1.cfm [L,QSA] </IfModule> First, enable URL rewriting in your Lucee Server Administrator Navigate to Web Context → URL Mapping Enable "URL Rewriting" Set "URL Separator" to "/" Ensure mod_rewrite is enabled in Apache Test thoroughly with different URL structures Consider adding error handling for invalid URLs Monitor server logs for rewrite issues Remember that Lucee's built-in URL rewriting handles CFML-specific routing
👀 1
b
Hey Gregory, not sure how you are doing this now. Maybe using a rewrite valve in Tomcat, no idea. It can be handled with a Tomcat rewrite valve, but you do have other options. One way is to use CommandBox which allows you to attach a rules.json file to your server.json file. This is probably the easiest way, I'm sure someone at Ortus can help you there. Another way is to use a Lucee docker image that comes pre-packed with Ngnix. This way, you can create your rewrite rules before it hits Lucee.
b
@Gregory Alexander Rewriting isn't something CF does, it's performed by your web server and the process differs based on the server
CommandBox has a pre-baked rewrite that works for frameworks like ColdBox and FW/1
Copy code
server set web.rewrites.enable=true
Here are the ColdBox docs on how to configure rewrites for the ColdBox framework https://coldbox.ortusbooks.com/the-basics/routing/requirements/rewrite-rules
There are different ways to do it, but generally it's to use the cgi.path_info to read the actual path, so the browser sees
Copy code
<http://site.com/foo/bar|site.com/foo/bar>
the web server rewrite changes it to
Copy code
<http://site.com/index.cfm/foo/bar|site.com/index.cfm/foo/bar>
which is the CF engine sees come in a
Copy code
cgi.path_info = "/foo/bar"
you then read that info in your index file and process the string as you see fit.
g
Thanks, everyone! I will try to implement the rewrite setting in Lucee!
Thanks, Brad, I am not using CommandBox on this site, just plain Lucee (although I am using CB on the development version)
b
@Gregory Alexander Well, that's not entirely true. Lucee is not a web server, so you can't use "plain Lucee" to run a website
You are using SOME webserver, whether it's IIS, Apache, CommandBox, or Tomcat
And whatever web server you are using, that is where the rewrites need configured
e
Copy code
RewriteEngine On RewriteRule ^([^/]+)/([^/]+)/?$ /index.cfm?param=$1&foo=$2 [L]
👍 1
Add that to Apache or an .htaccess file
Mod_rewrite is your friend
or you can add support directly to tomcat, with this project: https://github.com/paultuckey/urlrewritefilter
Lastly, this is the slower, but the most "clean" way if you are using Tomcat. https://tomcat.apache.org/tomcat-9.0-doc/rewrite.html
b
FWIW, Tuckey is sadly abandonware. There hasn't been a real release in many many years and it's been dropped by the author.
CommandBox used Tuckey, but I've deprecated it in favor or our Server Rules (built on Undertow's predicate language)
g
Hi Alholden, Yes, Brad is correct in his response. It is for SEO purposes. GSR, do I need to switch to multi-mode to edit the web context in Lucee? Everytime I click on the web context link it opens up a new Lucee window. Thank-you everyone! I will get to this over the weekend, you're all great!
e
I see "abandonware" as an opportunity.
b
Ha, well yes. We actually maintained a fork of Tuckey for a while which had fixes in it we needed since the main project had stopped merging pulls.
g
No you do not need multi mode for this
g
Thanks gsr, I can't seem to get into any web contexts UI. I suspect I may have to edit the json directly.
b
@Gregory Alexander What exactly are you needing to do in Lucee's web context. The whole point of single context mode is there is no separate web context. All config is done directly in the server context. Is there a setting you can't find?
g
Sorry for not responding earlier- I am trying to get a dozen things done- including releasing a new version of Galaxy Blog for Lucee. Brad- I was trying to follow the instructions that GSR gave, ie: "First, enable URL rewriting in your Lucee Server Administrator Navigate to Web Context → URL Mapping Enable "URL Rewriting" Set "URL Separator" to "/"
Brad, my issue with the following code raises the a 404 error (page not found) so I had thought to follow GRS's instructions regarding the Lucee context
<IfModule mod_rewrite.c> RewriteEngine On # Remove CFML extension and redirect to clean URL RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.cfm -f RewriteRule ^(.+)\.cfm$ /$1/ [R=301,L,QSA] # Transform query string parameters to path segments RewriteRule ^(.+)/([^/]+)/([^/]+)(/.*)?$ %{REQUEST_URI}?r=$2&m=$3 [L,QSA] # Serve actual CFML files internally RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)$ $1.cfm [L,QSA] </IfModule>
This also creates a 404 file not found: RewriteRule ^(.*)$ index.cfm/$1
Tried this too- but nothing works: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.cfm?/$1 [L,QSA]
The issue with the last rewrite rule is that the page renders the Lucee code verbatim, ie <cfif gregory_feels_stupid_with_rewrite_rules>🤮
b
Yeah, TBH it's a little hard to follow what you've got going on. I have no earthly clue what Lucee setting GSR is speaking of, so I can't help you there. If the raw CF code is coming back from the server, that means Lucee isn't server the file, Apache is. I'd have to understand better exactly what your rewrite rules are doing to the requests as well as how your proxy to Tomcat is configured to really help. Whatever rewrites you have above isn't the standard framework rewrites I'm familiar with
👍 1
g
There is no settings required Just follow what I have written and it will work
g
HI GSR,
My issue is that I can't access the Lucee contexts as you wrote here: First, enable URL rewriting in your Lucee Server Administrator Navigate to Web Context → URL Mapping Enable "URL Rewriting" Set "URL Separator" to "/" Whenever I set a rewrite rule with Apache it either breaks the page or serves up my code so I am thinking that you're direction to set the Lucee setting is correct but I can't get to the context.
g
i am not sure the setu[p you have but there are ways to fix it, lets take a step by step approach lets do it simple
Copy code
# Enable mod_proxy in Apache
a2enmod proxy
a2enmod proxy_http

# Restart Apache
service apache2 restart
step 2
Copy code
<IfModule mod_proxy.c>
    ProxyPreserveHost On
    ProxyPassReverse / <http://127.0.0.1:8888/>
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # Handle existing .cfm files
    RewriteCond %{REQUEST_FILENAME}\.cfm -f
    RewriteRule ^(.+)\.cfm$ <http://127.0.0.1:8888/$1.cfm> [P,L]
    
    # Let Lucee handle clean URLs internally
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ <http://127.0.0.1:8888/$1> [P,L]
</IfModule>
Add these security rules before your rewrite rules:
Copy code
# Block direct CFML file access attempts
RewriteCond %{THE_REQUEST} \.cfm[\s/?] [NC]
RewriteRule ^ - [F,L]

# Prevent directory traversal
RewriteCond %{REQUEST_URI} ..+/.* [NC]
RewriteRule .* - [F,L]
g
Thank-you!