Apache中URL重写和代理的组合失败-404 Not Found Path

ioekq8ef  于 2023-08-07  发布在  Apache
关注(0)|答案(2)|浏览(209)

你好ApacheMaven
我有一个第三方http web应用程序在端口8080上侦听服务器。第三方应用程序需要请求URL的格式为

  1. http://hostname:8080/?accnum=<account number>

字符串
然而,由于遗留的集成问题,请求者web客户端以

  1. https://hostname/oldcontext/?acc=&quot;<accountnumber>&quot


这里“oldcontext”是一个固定字符串,accountnumber是一个可变数字
为了实现请求者和第三方应用程序之间的集成-我安装了一个apache服务器,并使用apache代理和apache mod rewrite rewriterule指令来转换URL格式并在端口8080上发送
我的Apache Web服务器配置如下所示

  1. <VirtualHost *:80>
  2. RewriteEngine on
  3. RewriteCond %{QUERY_STRING} acc=([^&]+)
  4. RewriteRule ^oldcontext/$ /?accnum=%1 [L,R=301]
  5. RequestHeader unset Origin
  6. ProxyPass / http://127.0.0.1:8080/
  7. ProxyPassReverse / http://127.0.0.1:8080/
  8. ServerAdmin webmaster@localhost
  9. DocumentRoot /var/www/html
  10. ErrorLog ${APACHE_LOG_DIR}/error.log
  11. CustomLog ${APACHE_LOG_DIR}/access.log combined
  12. </VirtualHost>


我当然已经启用了必要的Apache模块,如下所示,并重新启动Apache

  1. a2enmod rewrite
  2. a2enmod headers
  3. a2enmod proxy
  4. a2enmod proxy_http


当我向URL发出请求的时候

  1. http://<my hostname>/oldcontext/?acc=&quot;<account number>&quot;


希望将其转换并重定向到

  1. http://127.0.0.1:8080/?accnum=<account number>


但是,
我得到以下回应

  1. {"status":404,"error":"Not Found","path":"/oldcontext/"}


我检查了代理程序工作正常-
我的请求

  1. http://127.0.0.1/?accnum=<account number>


正确重定向到

  1. http://127.0.0.1:8080/?accnum=<account number>


我得到了预期的回应
重写失败,尽管如所述
更糟糕的是,如果我包括一个catch allconfig fr URL重写如下,它仍然给我同样的错误

  1. enter code here
  2. <VirtualHost *:80>
  3. RewriteEngine on
  4. RewriteCond %{QUERY_STRING} acc=([^&]+)
  5. # RewriteRule ^oldcontext/$ /?accnum=%1 [L,R=301]
  6. RewriteRule ^(.*)$ /?accnum=%1 [L,R=301]
  7. RequestHeader unset Origin
  8. ProxyPass / http://127.0.0.1:8080/
  9. ProxyPassReverse / http://127.0.0.1:8080/
  10. ServerAdmin webmaster@localhost
  11. DocumentRoot /var/www/html
  12. ErrorLog ${APACHE_LOG_DIR}/error.log
  13. CustomLog ${APACHE_LOG_DIR}/access.log combined
  14. </VirtualHost>

  • 列表项目

我错过了什么?
谢谢你的帮助
约格什

z0qdvdin

z0qdvdin1#

  1. RewriteRule ^oldcontext/$ /?accnum=%1 [L,R=301]

字符串
此规则在virtualhost上下文中永远不能匹配。第一个参数中的regex与以/开始的解码URL路径匹配。

huwehgph

huwehgph2#

以下配置对我有效
从我收集到的信息来看,我基本上被卡住了,无法得到一个正确的RewriteCond,它将匹配我的URL,从而确保我的重写规则被触发。有一次,我发现我单独寻求帮助只是为了获得正确的重写条件(请参阅我的其他文章Apache mod rewrite RewriteCond regex help needed)
由于我的rewritecond不匹配的重定向是发送teh请求与“oldcontext”到新的位置,其中“oldcontext”不存在,因此404错误
一旦我在我的RewriteCond中获得了我的请求URL的模式匹配,这确保了我的重写规则被触发。我的rewriterule也不匹配,我按照https://stackoverflow.com/users/98959/covener的建议进行了更改,它将URL更改为新路径,然后一切正常
我包括完整的配置为完整起见

  1. <VirtualHost *:80>
  2. ServerAdmin webmaster@localhost
  3. DocumentRoot /var/www/html
  4. # LogLevel alert rewrite:trace7
  5. ErrorLog ${APACHE_LOG_DIR}/error.log
  6. CustomLog ${APACHE_LOG_DIR}/access.log combined
  7. ProxyPass / http://127.0.0.1:8080/
  8. ProxyPassReverse / http://127.0.0.1:8080/
  9. RewriteEngine on
  10. RewriteCond %{QUERY_STRING} ^acc=[a-z&;]+([0-9]+)[a-z&;]+ [NC]
  11. RewriteRule ^/oldcontext/$ http://%{HTTP_HOST}:8080/?accnum=%1 [L,R=301,B,NE]
  12. </VirtualHost>

字符串

展开查看全部

相关问题