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 %.)

  • "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? – grawity Apr 9 '18 at 6:23
  • Because I will be adding another domain? – user163831 Apr 9 '18 at 6:50
  • But each VirtualHost block, and therefore each domain, has its own DocumentRoot setting. – grawity Apr 9 '18 at 12:41

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.