1

I've got a tricky question, as far as I see it ;)

Currently, I'm in setup of a dynamic branch release pipeline which already takes a branch during pipeline processing and deploys it on our test server in a reserved folder ("branches"). Our wildcard domain points towards it.

Currently working (in the branches folder):

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.domain\.com$ [NC]
RewriteRule ^ %1%{REQUEST_URI}/public [L,NC]

But the problem now is, that for example a domain my-new-testbranch-123.domain.com get's the redirect to my-new-testbranch-123.domain.com/my-new-testbranch-123/public/. This is on one side ok (because out data is being found there, but I want to have a proper url such as the app can be routed properly. So I tried to rewrite the url via proxy. But without luck. Any suggestions?

My goal would be to use the subdomain my-new-testbranch-123.domain.com/ without any additional path in url. The document root is located in my-new-testbranch-123/public/.

I tried via chaining, but both did not work as I thought -.-:

RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.domain\.com$ [NC]
RewriteRule ^ %1%{REQUEST_URI}/public [NC,N]
RewriteCond %{REQUEST_URI} ([^.]+)/public
RewriteRule ^(.*)$ / [P]

and

RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.domain\.com$ [NC]
RewriteRule ^ %1%{REQUEST_URI}/public [NC,C]
RewriteCond %{REQUEST_URI} ([^.]+)/public
RewriteRule ^(.*)$ / [P]

(with C and N flags)

Best, Bent

Bent
  • 86
  • 1
  • 8

1 Answers1

0

Issue with your original rule is you don't have / after public and since that is an actual directory, Apache is doing a 301 redirect via mod_dir module.

You can use this rule to fix the issue:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.domain\.com$ [NC]
RewriteRule ^ %1/public%{REQUEST_URI} [L]

Make sure to test in a new browser or via command line curl to avoid old browser cache.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Well, partially. It's now working in terms of all assets etc. but I still get the path /my-new-testbranch-123/public/ and if I'm opening the site, via path it's not working either: my-new-testbranch-123.domain.com/projects – Bent Sep 02 '22 at 07:31
  • So to say, the second criteria (proxying the path) is not yet fulfilled – Bent Sep 02 '22 at 07:37
  • Do you have any other htaccess or other rules in this htaccess? – anubhava Sep 02 '22 at 08:37
  • nope, this is the full file (my version above - now with trailing /) – Bent Sep 02 '22 at 08:39
  • Test in Chrome dev tool with **caching disabled** and check in Network tab what are 301/302 redirect URLs you get. – anubhava Sep 02 '22 at 16:06