What htaccess rules will REMOVE the subdirectory serving the site from the URI (404 if included) & internal links?

0

**SEE UPDATE BELOW*****

My primary domain site is to be served from a subdirectory on my server that is named same as the domain, so that will probably be confusing to read in this messy block of my webroot .htaccess:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /

  # remove www
  RewriteCond %{HTTP_HOST} ^www\.(.+)$
  RewriteRule ^.*$ %1 [NC,QSA,R=301]

  #RewriteCond %{ENV:REDIRECT_STATUS} . [OR]
  RewriteCond %{HTTP_HOST} ^(www\.)?my\-web\.agency$
  RewriteRule ^my-web.agency/ - [L,R=404]
  RewriteRule !^my-web.agency/ my-web.agency/%{REQUEST_URI} [NC,QSA]
</IfModule>

I'd just been working on it for so long, altering the rules and structuring that that mangle is what I've got going presently.

The www. STAYS, so that bit to remove it is entirely ineffective.

The !^my-web.agency/ rule sure does indeed serve the website effectively. HOWEVER, my following attempt at rendering a 404 if the subdirectory is included in the URI FAILS.

Please explain to me how to get all of this straightened out. I'm trying to read the docs and learn and figure it out but somehow I just can't and I've put plenty of time in to it already, I really need to stop, please help, I'll be ever grateful to learn and take careful note. Thank you much.

*****UPDATE:** Now I have

# remove www, externally for consistent domains (successful)
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule .* http://[[percent]]1/$0 [NS,L,R=301,QSA]

RewriteCond %{HTTP_HOST} ^(www\.)?my\-web\.agency$
RewriteCond %{REQUEST_URI} ^my-web.agency
RewriteRule ^(.*)$ - [R=404,L,NC]

RewriteCond %{HTTP_HOST} ^(www\.)?my\-web\.agency$
RewriteCond %{REQUEST_URI} !^my-web.agency
RewriteRule ^(.*)$ /my-web.agency/$1 [L,NC,QSA]

That does effectively make the www. disappear if attempted. But all the INTERNAL LINKS STILL INCLUDE the subdirectory that is supposed to be hidden (e.g., internal links read "http://my-web.agency/my-web.agency/somepage"), and accessing pages with the subdirectory in the URI still works; I would rather it return a 404.

DocumentRoot or RewriteBase seem to be logical possible solutions, if only I could figure out the proper implementation. (The [[percent]] proceeding http:// in 1st RewriteRule is to allow it through the editor without throwing an error; simply indicates %.)

user163831

Posted 2018-04-09T06:06:57.010

Reputation: 129

"My primary domain site is to be served from a subdirectory on my server that is named same as the domain" – so why not alter DocumentRoot to match? – user1686 – 2018-04-09T06:23:07.867

Because I will be adding another domain? – user163831 – 2018-04-09T06:50:54.030

But each VirtualHost block, and therefore each domain, has its own DocumentRoot setting. – user1686 – 2018-04-09T12:41:25.927

No answers