英文:
How to redirect one URL to a different directory (outside of DOCKER) using apache2
问题
我有一个安装在 Docker 内的 WordPress 网站 example.com,同时我还有一个存储在 /var/www/store 中的 CI 项目。我希望当用户在地址栏中输入 example.com/store 时,能够重定向到 Docker 外部的 /var/www/store。以下是我的尝试:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
RequestHeader set "X-Forwarded-SSL" expr=%{HTTPS}
ServerAdmin kontak@japati.net
ServerName example.com
ProxyRequests Off
<Location />
ProxyPreserveHost On
ProxyPass http://127.0.0.1:8100/
ProxyPassReverse http://127.0.0.1:8100/
</Location>
RewriteEngine On
RewriteRule ^/store(.*)$ /var/www/store$1 [L]
SSLEngine on
SSLCertificateFile /opt/acme-cert/example.com/cert.pem
SSLCertificateKeyFile /opt/acme-cert/example.com/site.key
SSLCertificateChainFile /opt/acme-cert/example.com/fullchain.cer
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
但是这并不起作用,WordPress 显示页面未找到错误。我还尝试了 alias /store /var/www/store,但仍然不起作用。
英文:
I have example.com which is an wordpress site installed inside docker, and I also have CI project stored in /var/www/store. I want user to be redirected to /var/www/store (outside docker) when entering example.com/store in address bar. This is my attempt.
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
RequestHeader set "X-Forwarded-SSL" expr=%{HTTPS}
ServerAdmin kontak@japati.net
ServerName example.com
ProxyRequests Off
<Location />
ProxyPreserveHost On
ProxyPass http://127.0.0.1:8100/
ProxyPassReverse http://127.0.0.1:8100/
</Location>
RewriteEngine On
RewriteRule ^/store(.*)$ /var/www/store$1 [L]
SSLEngine on
SSLCertificateFile /opt/acme-cert/example.com/cert.pem
SSLCertificateKeyFile /opt/acme-cert/example.com/site.key
SSLCertificateChainFile /opt/acme-cert/example.com/fullchain.cer
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
But this doesn't work, I get page not found error from wordpress. I also tried alias /store /var/www/store but it still doesn't work.
答案1
得分: 0
你的问题是你试图将Docker与Apache混合使用,但现在所有请求都被转发到端口8100,这意味着显然RewriteRule是无法帮助的。(它们也会被转发。)(无论如何,你不能使用RewriteRule去访问一个物理位置,只能访问一个URL。)
你的<Location />块正在将所有请求转发到端口8100。让我们来修复一下:
你需要添加这个:
<VirtualHost *:443>
#.......一些配置.......
Alias /store "/var/www/store" #<---------------- 将"/store"映射到/var/www/store
ProxyPass /store ! #<-------------- 将这行放在其他ProxyPass声明之前
<Location />
ProxyPreserveHost On
ProxyPass http://127.0.0.1:8100/
ProxyPassReverse http://127.0.0.1:8100/
</Location>
或者你可以这样做:
#.......一些配置.......
Alias /store "/var/www/store" #<---------------- 将"/store"映射到/var/www/store
<Location /store>
ProxyPass !
</Location>
<Location />
ProxyPreserveHost On
ProxyPass http://127.0.0.1:8100/
ProxyPassReverse http://127.0.0.1:8100/
</Location>
英文:
Your problem is that you're trying to mix Docker with Apache, but right now ALL requests are getting forwarded to port 8100, which means obviously RewriteRules aren't going to help. (They'll get forwarded too.) (Anyways, you can't use RewriteRule to go to a physical location, only to a URL.)
Your <Location /> block is forwarding ALL requests to port 8100. Let's fix that:
You'll want to add this:
<VirtualHost *:443>
#.......Stuff.......
Alias /store "/var/www/store" #<---------------- map "/store" to /var/www/store
ProxyPass /store ! #<-------------- Place this before any other ProxyPass declarations
<Location />
ProxyPreserveHost On
ProxyPass http://127.0.0.1:8100/
ProxyPassReverse http://127.0.0.1:8100/
</Location>
Or you could instead do this:
#.......Stuff.......
Alias /store "/var/www/store" #<---------------- map "/store" to /var/www/store
<Location /store>
ProxyPass !
</Location>
<Location />
ProxyPreserveHost On
ProxyPass http://127.0.0.1:8100/
ProxyPassReverse http://127.0.0.1:8100/
</Location>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论