0

This almost the same as .htaccess rewrite subdomain to directory

this solution works:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^([^.]+)\.jcubic\.atthost24\.pl$
RewriteRule ^((?!subdomains/).*)$ /subdomains/%1/$1 [L,NC,QSA]

but I would like to forward but without /subdomains directory so

http://img.jcubic.atthost24.pl/.* should read http://jcubic.atthost24.pl/img/.*

Is this possible? This doesn't work

RewriteRule ^((?!%1/).*)$ /%1/$1 [L,NC,QSA]

I'm not sure what version of Apache I have.

jcubic
  • 61,973
  • 54
  • 229
  • 402

1 Answers1

1

This probably is what you are looking for:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^img\.jcubic\.atthost24\.pl$
RewriteRule ^/?(.*)/?$ /img/$1 [END,QSA]

Note however that you should try to place such rules inside the http server's host configuration instead of using dynamic configuration files (".htaccess").

Above rule will work in both situations, though.

In case you get an internal error (http status 500) check the error log file. If it complains about an unknown END flag, then you probably operate a very old version of the apache http server. In that case try the L flag, it shoulod work the same here, though that actually depends on the specific situation.

UPDATE, according to your comment:

In case the hostname "img" is to be treated in a dynamic manner you can do that:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.jcubic\.atthost24\.pl$
RewriteRule ^/?(.*)/?$ /%1/$1 [END,QSA]

Maybe you want to add a check for whether that folder actually exists in the physical file system of the http server.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • This is for single domain. What If I have 20 of them? – jcubic Jun 07 '19 at 16:11
  • I'm just checking this again with different domain, and the problem is that http://javascript.jcubic.pl/ don't work it don't see index.html file, but if I add a file it works fine. Can you explain why and also if there is a way to load default file. – jcubic Dec 04 '20 at 15:17
  • I can only answer to your comment if you define what you actually mean by "domain" here. Unfortunately the words is used for various things these days... – arkascha Dec 05 '20 at 21:29
  • And about the index document... Not sure, is that a real "index.html" file in that folder? Then I see no reason for that behavior. If you actually mean that there is no folder at all, but you _still_ expect the rule to work, then that is something else. Then you do _not_ want to map a host name to a folder as you asked in the question. So please, be more precise so that I can help. – arkascha Dec 05 '20 at 21:31
  • javascript is a folder and it have real index.html, this is Jekyll site with static files and javascript is a tag (that I've mistakenly uploaded to main directory instead to `/tag`). You can open https://jcubic.pl/javascript/ but with subdomain I need to specify a file. – jcubic Dec 06 '20 at 13:50
  • That sounds to me like the DirectoryIndex setting inside your http server is setup different for the different hosts. If you cannot pin that down right away then I suggest you enable rewrite logging to learn what is actually going on inside your rewrite engine. Please tke a look into the documentation of the rewriting module. – arkascha Dec 06 '20 at 20:02