【问题标题】:Rewrite rule not working with apache2, lucee tomcat重写规则不适用于 apache2,lucee tomcat
【发布时间】:2021-06-19 03:33:13
【问题描述】:

我已经为 ubuntu 上的虚拟主机配置了带有 lucee tomcat 和 apache2 的服务器。我启用了重写规则,我的虚拟主机如下。

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias example.com www.example.com
    DocumentRoot /var/www/html/example.com/

    <Directory /var/www/html/example.com/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error_main_example.log
    CustomLog ${APACHE_LOG_DIR}/access_main_example.log combined
    DirectoryIndex index.cfm
</VirtualHost>

从 htaccess 文件重定向工作正常,但重写规则不起作用。这是我正在尝试的 htaccess 文件。

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

RewriteRule ^(.*)/abc/([0-9]+)$ /test-404.cfm [L]

以下是示例 URL:

https://www.example.com/example.cfm/abc/2
https://www.example.com/example.cfm/abc/8
https://www.example.com/example.cfm/abc/15

它向我显示 Tomcat 404 错误

HTTP Status 404 – Not Found

Type Status Report

Message The requested resource [/example.cfm/abc/2] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/9.0.35

任何机构都可以帮助我解决这个问题。顺便说一句,站点是使用 AWS 上的经典负载均衡器配置的。

【问题讨论】:

  • 所以/test-404.cfm 应该在这里显示 不同的 404 错误消息,或者究竟是什么问题?
  • @CBroe 是的。实际上 test-404 显示了这些类型的 URL 的一些内容。问题是它没有重写而是显示tomcat 404错误。
  • 该模式应与您提到的 URL 匹配。如果您删除之前进行的其他重写内容,并在 RewriteEngine On 后仅保留这条规则,会发生什么?
  • 仅供参考,启用重写日志记录通常也有助于弄清楚到底发生了什么。
  • @CBroe。才发现。当 url 模式如下或 cfm 文件:/example.cfm/abc/15 时,重写规则不适用。但如果我使用 /example.php/abc/15 网址。它正在正确重写。因此 apache2 不考虑将 cfm 作为此类 URL 的文件。我可以在哪里定义一些吗?

标签: .htaccess apache2 tomcat9 lucee


【解决方案1】:

我也在这里发布了一个可行的解决方案,因为你没有在 Lucee 论坛上回复我们的这个交叉帖子的帖子。此解决方案也可能会帮助其他有相同问题的人。

问题是 mod_proxy 总是在 urlrewrite 之前,除非您使用特殊的 urlrewrite 规则调用 mod_proxy。要使 urlrewrite 工作并使用 mod_proxy,您需要使用 [P] 标记重写规则(用于代理)。这是一个适合您的工作示例:

  1. 步骤:将 mod_proxy.c 中 apache2.conf 中的所有内容设置为注释,只保留 ProxyPreserveHost 和 ProxyPassReverse,如下所示:
<IfModule mod_proxy.c>
        ProxyPreserveHost On
        #ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$ http://127.0.0.1:8888/$1$2
        #ProxyPassMatch ^/(.+\.cfml)(/.*)?$ http://127.0.0.1:8888/$1$2
        # optional mappings
        #ProxyPassMatch ^/flex2gateway/(.*)$ http://127.0.0.1:8888/flex2gateway/$1
        #ProxyPassMatch ^/messagebroker/(.*)$ http://127.0.0.1:8888/messagebroker/$1
        #ProxyPassMatch ^/flashservices/gateway(.*)$ http://127.0.0.1:8888/flashservices/gateway$1
        #ProxyPassMatch ^/openamf/gateway/(.*)$ http://127.0.0.1:8888/openamf/gateway/$1
        #ProxyPassMatch ^/rest/(.*)$ http://127.0.0.1:8888/rest/$1
        ProxyPassReverse / http://127.0.0.1:8888/
    </IfModule>
  1. 步骤:在您的虚拟主机配置中设置以下重写规则(这也可能在您的 .htaccess 中有效,但我不确定)。
...

<Directory /var/www/html/example.com/>
...
...
RewriteEngine On
RewriteBase /

# Catch non-existing files/directories and pass them via proxy to a 404 cfml errorpage
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule "^(.*)$" "http://127.0.0.1:8888/my-404.cfm" [P]

# Catch https://www.example.com/example.cfm/abc/2
# Catch https://www.example.com/example.cfm/abc/7 etc.
RewriteRule "^example.cfm(/abc/[0-9]+)$" "http://127.0.0.1:8888/my-404.cfm" [P]

# Pass request for cfm/cfc files to proxy with mod_rewrite rule
RewriteRule  "^(.+\.cf[cm])(/.*)?$" "http://127.0.0.1:8888/$1$2"  [P]

...

 </Directory>   

我已经测试过了,上面的解决方案效果很好。

【讨论】:

    猜你喜欢
    • 2013-07-12
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多