I have a website set up at example.com/directory/ currently. /directory/ is set up to display different contents depending on the subdomain, that part is working fine. I'm having trouble editing my httpd.conf file to work correctly though.

Obviously I can only set up my A records for sub.example.com to point to example.com and not example.com/directory/ so I've set up some rules in my httpd to get around this, but they don't seem to be working. This is what I've got so far:

RewriteCond %{HTTP_HOST} ^sub1\.example\.com$ [NC]
RewriteRule ^((?!folder).*)$ /directory/$1 [NC,L]

RewriteCond %{HTTP_HOST} ^sub2\.example\.com$ [NC]
RewriteRule ^((?!folder).*)$ /directory/$1 [NC,L]

What seems to be happening though is those rules just direct each subdomain to example.com/directory and drop the subdomain completely. I'm a little lost as to what I need to change here, I'm not very well versed in Apache.

I know this should be simple, but I can't seem to find any documentation that is straightforward enough for me to understand, does anybody have any pointers?

  • You use both folder and directory in your rewrite rules. Shouldn't those be the same? – Stephen Ostermiller Oct 6 '16 at 10:20
  • You also say that you want to "redirect", but your rules don't have the [R=301] flag in them that would tell rewrite to do the redirect rather than serving the content from one URL at another. – Stephen Ostermiller Oct 6 '16 at 10:22
  • Why use mod_rewrite at all? Why not just create a virtual host for sub.example.com and make its DocumentRoot set to /path/to/directory – Unbeliever Oct 9 '16 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.