Prathyusha
Prathyusha

Reputation: 45

How to implement urlrewrite in lucee

I want to rewrite the url in my lucee application. I use the url "http://localhost:8888/sampleApp/views/test.cfm" to display the view pages and I want it to display as "http://localhost:8888/sampleApp/test.cfm".

Upvotes: 1

Views: 759

Answers (3)

volume one
volume one

Reputation: 7563

Are you using IIS on Windows? If so all you need is the URL Rewrite Module 2.0

Install that, set up your web.config with your rewrite rules and you're good to go.

You will also probably not want the .cfm extension in your URLs. I use this rule for that:

<rule name="Rewrite .cfm" enabled="true" stopProcessing="false">
          <match url="[^?]*" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="^(.*).png" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="^(.*).cfm" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="^(.*).cfc" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="^(.*).jpg" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="^(.*).gif" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="^(.*).mp4" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="^(.*).mp3" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="^(.*).css" negate="true" />
          </conditions>
          <action type="Rewrite" url="{R:0}.cfm" />
        </rule>

You would have add to more conditions for all file types your website supports like .mkv or .js

Upvotes: 1

AndreasRu
AndreasRu

Reputation: 1138

As already mentioned, this is up to the webserver to do, especially in production. However, in some cases and for development you may want to use urlrewrite in your servlet container engine without a fronted webserver. Reading from your post, you are using port 8888, and that makes me assume you are using Lucees default servlet container Tomcat.

Since Tomcat 8.0 it is possible to use urlrewrite. But as already said, this is only recommended for development or with Tomcat as stand alone in production. If you have another webserver in front of Tomcat you would let this settings untouched and the fronted webserver would do the url rewrite. For a working solution with Tomcat in stand alone, add the RewriteValve and a rewrite.config file containing the url rewrite rules as follows:

  1. Edit {path_to_lucee}/conf/server.xml and add the RewriteValve directive to the body of your <host name="localhost">. That would be like so:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
 ...
 <Valve className="org.apache.catalina.valves.rewrite.RewriteValve"/>
 ...
</Host>
  1. Then create a rewrite.config at {path_to_lucee}/conf/Catalina/localhost/rewrite.config You'll need to create the folders Catalina and localhost in case they don't exist. Then add the follwing simple url rewrite rule to rewrite.config file:
RewriteCond %{SERVLET_PATH} !-f
RewriteRule ^\/sampleApp\/(.*)\.cfm(.*)$ sampleApp/views/$1.cfm$2 [L,QSA]
  1. Restart Tomcat for the new setting to take effect

You'll very probably have to tweak this rule. For more and deeper information, please see: Apache Tomcat 9 Rewrite Valve

Upvotes: 3

andrewdixon
andrewdixon

Reputation: 1069

That would not be implemented in Lucee, it would be done in the web server, e.g. Apache, IIS, Nginx, etc... Lucee is a servlet running on an application server like Tomcat, not the web server, do not conflate the two.

Upvotes: 3

Related Questions