首页 > 解决方案 > 重写规则不适用于 apache2,lucee tomcat

问题描述

我已经为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 上的经典负载均衡器配置的。

标签: .htaccessapache2tomcat9lucee

解决方案


我在这里也发布了一个可行的解决方案,因为您没有在 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>   

我已经对其进行了测试,上面的解决方案运行良好。


推荐阅读