Reputation: 3221
I'm attempting to host my website's primary domain from a subfolder of its document root - yielding a directory structure like:
/public_html/www.main-domain.com/ <--I need to do this with htaccess
/public_html/sub1.main-domain.com/ <--For subdomains I can just set the doc root w/ cpanel
/public_html/sub2.main-domain.com/
It's shared hosting (HostGator), so I don't have the ability to use virtual hosts. This situation has solutions posted all over the web (1, 2, etc), consisting of htaccess rules like:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?main\-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/www.main\-domain.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www.main-domain.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?main\-domain.com$ [NC]
RewriteRule ^(/)?$ www.main-domain.com/index.php [L]
The above rules do work, except if I try to access any subfolder of www.main-domain.com without a trailing slash. For instance, if I visit the url
http://www.main-domain.com/images/ (with trailing slash)
it works fine. However, if I visit
http://www.main-domain.com/images (no trailing slash)
then the browser URL bar changes to show the actual, full server path:
http://www.main-domain.com/www.main-domain.com/images/
Note that in both cases, the content is served correctly - the only issue is that (unless a trailing slash is explicitly entered) the URL bar will change to reveal the 'true' server directory structure, rather than just the desired "www.main-domain.com/images."
This is effectively the same question as here, however the accepted answer is to use virtual hosts - which isn't possible in my case. The same person also posted on ServerFault, but that's been closed to replies. The one promising answer there suggests adding the following before the final index.php rule:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} !/$
RewriteRule ^(.*) $1/
Unfortunately, that didn't work for me. If someone could suggest a set of rules that accomplishes the 'primary domain in a subfolder,' and which doesn't suffer from the "no-trailing-slash" issue, I'd be very grateful.
--Edit--
I've got this mostly working via the following rules:
# Externally redirect to add trailing slash to extensionless URLs if missing
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(([^/]+/)*[^./]+)$ http://www.example.com/$1/ [L,R=301]
# Internally rewrite all requests to subfolder
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^(.*)$ /www.example.com/$1 [L,NC,QSA]
This works, except with URLs like:
http://www.example.com/my-script?xdm_e=https%3A%2F%2Fjk.test
Which gets redirected to:
http://www.example.com/my-script/?xdm_e=https%253A%252F%252Fjk.test
Note that in addition to adding the trailing slash (as desired), percent symbols have also been expanded to "%25". I thought this might have something to do with the "B" flag, but adding that to the RewriteRule just made things worse ("my-script" got changed to "my%252dscript", and the latter percents were still altered).
Again I am stumped...
Upvotes: 1
Views: 1201
Reputation: 3221
The solution was as simple as adding the "NE" flag. My final working rules are therefore:
# Externally redirect to add trailing slash to extensionless URLs if missing
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(([^/]+/)*[^./]+)$ http://www.example.com/$1/ [L,NE,R=301]
# Internally rewrite all requests to subfolder
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^(.*)$ /www.example.com/$1 [L,NC,QSA]
Upvotes: 1