【问题标题】:How to implement urlrewrite in lucee如何在lucee中实现urlrewrite
【发布时间】:2020-05-03 21:41:27
【问题描述】:

我想在我的 lucee 应用程序中重写 url。我使用 url "http://localhost:8888/sampleApp/views/test.cfm" 来显示视图页面,我希望它显示为 "http://localhost:8888/sampleApp/test.cfm"。

【问题讨论】:

标签: url-rewriting coldfusion lucee


【解决方案1】:

这不会在 Lucee 中实现,它将在 Web 服务器中完成,例如Apache、IIS、Nginx 等... Lucee 是运行在 Tomcat 等应用服务器上的 servlet,而不是 Web 服务器,请勿将两者混为一谈。

【讨论】:

    【解决方案2】:

    如前所述,这取决于网络服务器,尤其是在生产环境中。但是,在某些情况下,为了开发,您可能希望在没有前端 Web 服务器的 servlet 容器引擎中使用 urlrewrite。从您的帖子中阅读,您使用的是端口 8888,这让我假设您使用的是 Lucees 默认的 servlet 容器 Tomcat。

    从 Tomcat 8.0 开始可以使用 urlrewrite。但正如已经说过的,这只建议用于开发或在生产中单独使用 Tomcat。如果您在 Tomcat 前面有另一个网络服务器,您将保持此设置不变,前端的网络服务器将执行 url 重写。对于单独使用 Tomcat 的工作解决方案,添加 RewriteValve 和包含 url 重写规则的 rewrite.config 文件,如下所示:

    1. 编辑 {path_to_lucee}/conf/server.xml 并将 RewriteValve 指令添加到 的正文中。应该是这样的:
    <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
     ...
     <Valve className="org.apache.catalina.valves.rewrite.RewriteValve"/>
     ...
    </Host>
    
    1. 然后在以下位置创建一个 rewrite.config {path_to_lucee}/conf/Catalina/localhost/rewrite.config 您需要创建文件夹 Catalinalocalhost 以防它们不存在。然后在 rewrite.config 文件中添加以下简单的 url 重写规则:
    RewriteCond %{SERVLET_PATH} !-f
    RewriteRule ^\/sampleApp\/(.*)\.cfm(.*)$ sampleApp/views/$1.cfm$2 [L,QSA]
    
    1. 重启 Tomcat 以使新设置生效

    您很可能必须调整此规则。有关更多和更深入的信息,请参阅: Apache Tomcat 9 Rewrite Valve

    【讨论】:

      【解决方案3】:

      您是否在 Windows 上使用 IIS?如果是这样,您只需要URL Rewrite Module 2.0

      安装它,用你的重写规则设置你的web.config,你就可以开始了。

      您也可能不希望在您的 URL 中添加 .cfm 扩展名。我使用这个规则:

      <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>
      

      您需要为您的网站支持的所有文件类型添加更多条件,例如 .mkv.js

      【讨论】:

        猜你喜欢
        • 2022-05-12
        • 2019-01-15
        • 1970-01-01
        • 1970-01-01
        • 2015-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多