4th level subdomain redirect without changing the URL using .htaccess

Viewed 61

Please excuse me and my near-to-zero apache skills. I'm an owner of a website and I would love to redirect my fourth level domains (for example fyz.edu.ondrejaugusta.cz, che.edu.ondrejaugusta.cz etc.) to a directory /subdomains/fyz, /subdomains/che respectively.
Also I would appreciate if the URL didn't change to ondrejaugusta.cz/subdomains/fyz (or /che) and stayed like fyz.edu.ondrejaugusta.cz. I'm struggling to get it working for more than a week now - I've read a lot of articles, none of which (sadly) helped me. Please tell me what I'm supposed to do / change, I am clueless.

RewriteCond %{HTTP_HOST} ^fyz\.edu\.ondrejaugusta\.cz$
RewriteRule ^ http://ondrejaugusta.cz/subdomains/fyz%{REQUEST_URI} [L,P]
RewriteCond %{HTTP_HOST} ^che\.edu\.ondrejaugusta\.cz$
RewriteRule ^ http://ondrejaugusta.cz/subdomains/che%{REQUEST_URI} [L,P]

This is the code I've got so far. It's working, but redirecting to /subdomains/fyz (/subdomains/che).
Also don't worry, I'm not teaching informatics or English.

1 Answers

I assume you are actually looking for an internal rewrite and not for a redirection, since you ask the visible URL in the browser not to get altered.

This will internally rewrite any request to such a "4th level" host name to a folder in the local "subdomains" folder:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/subdomains/
RewriteCond %{HTTP_HOST} ^([^.]+)\.edu\.ondrejaugusta\.cz$
RewriteRule ^ /subdomains/%1%{REQUEST_URI} [QSA,END]

Obviously the rewriting module has to be loaded and enabled for those hosts, likewise the interpretation of such distributed configuration files (".htaccess") if you really want to rely on those.

Related