I have a redirect in place. If a request comes in to the root and the content is available in /website3 it pulls the content in from website3 but only displays the root request. This works really well. For example is a request comes in for example.com/foo/test/ and there is an index.htm file in /website3/foo/test the content displays without including website3 in the url. It also plays nicely if a different path exists like example.com/test it will go to the test directory. This is all perfect.
I am trying to factor out if the request comes in for example.com/website3/foo/test I would like this to redirect to example.com/foo/test but still pull in the content from website3. I have tried a few things and they all result in a loop.
Here is the current code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# Remove trailing slash on any URL that is requested directly (excludes rewritten URLs)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*)/$ /$1 [R=301,L]
# Rewrite root
RewriteRule ^$ website3/ [L]
# If request maps to a directory in "/website3" then rewrite and append trailing slash
RewriteCond %{DOCUMENT_ROOT}/website3/$1 -d
RewriteRule ^([^.] )$ website3/$1/ [L]
RewriteCond %{DOCUMENT_ROOT}/website3/$1 -f
RewriteRule (.*) website3/$1 [L]
CodePudding user response:
Add the following immediately after the RewriteEngine
directive to remove /website3/
from the start of any URL that is requested directly.
# Remove "/website3/" from any URL requested directly
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^website3/(.*) /$1 [R=301,L]
Note that this checks the REDIRECT_STATUS
env var in order to prevent a redirect loop - the same principle as your rule to remove the trailing slash.
The REDIRECT_STATUS
env var is not set (ie. is empty) on the initial request and set to "200" (as in 200 OK HTTP status) after the first successful rewrite.
Test first with a 302 (temporary) redirect to avoid potential caching issues.