微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

重写规则不适用于 apache2、lucee tomcat

如何解决重写规则不适用于 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]

以下是示例网址:

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

解决方法

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

我已经测试过了,上面的解决方案工作正常。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。