1

I'm trying to achieve masking subdomains to folders using .htaccess

UPDATE for the sake of clarity: I Would like the user to enter the url: subdomain.example.com and stay in the same url while getting the contents from example.com/subdomain

I'm using a Plesk 12 webhost. I've tried lots of post and the only thing i could achieve was getting the redirect from xxxx.example.com to example.com/xxxx (not masked).

The thing is that the folders are generated by other program (with their own structure and content) so i don't really know how else put mod_proxy in there to make the magic happen.

UPDATED 11-06-2015 I moved the subdomain folders to /sd/ in order to have them organized and apart from any other file.

UPDATED 11-07-2015 I was able to make the program only create 1 "prettyurl" for each element, so now i don't even have folders, just /sd/template/subdomain1/, /sd/template/subdomain2 and so on.

My current .htaccess:

Options +FollowSymlinks -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#RewriteRules for pretty url
RewriteRule ^sd/template/index.php$ "sd/template/" [R=301,L,QSA]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]

#sd/template/index.php
RewriteRule ^sd/template/.*?([^\.\/]*)\.html$ sd/template/?pname=$1 [L,QSA]
RewriteRule ^sd/template/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$ sd/template/?d=$1$2$3 [L,QSA]
RewriteRule ^sd/template/[^\.]*?([^/\.]*)/$ sd/template/?fname=$1 [L,QSA]
RewriteRule ^sd/template/[^\.]*?([^/\.]*)$ "$0/" [R=301,L,QSA]

RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ /sd/template/%1 [L,NC]
</IfModule>

I created a wildcard subdomain too, but i don't really know if it helps or makes it worse.

Right now, if i enter "test1.example.com" at the adress bar i get redirected to "example.com/test1/" correctly, but what i need is to show the contents without changing the URL shown.

UPDATED 11-07-2015 Now if i use test.example.com i get redirected to test.example.com/sd/template/test/. What should i do to just get the test.example.com?

Any help is greatly appreciated as i'm stuck here after 3 days reading one and another post from all around the web.

  • Not clear what you want here. Sounds like you're getting the proper content, but the URL being displayed in the users address bar is not correct? What URL would you like them to see in the address bar? – nathangiesbrecht Jun 5 '15 at 14:00
  • yes, exactly. I would like to get the "subdomain.example.com" url, while fetching the contents of "example.com/subdomain" – LordNeo Jun 5 '15 at 21:26
1

The R flag in your RewriteRule is for forcing an external redirect. You don't want to use that if you are masking where the content is coming from.

Try this:

RewriteRule ^/(.*)$ /path/to/your/subdomain/directories/%1/$1 [L,NC]

You may want to be more specific in your sub domain matches to tighten up security.

  • Tried this using: RewriteRule ^/(.*)$ /%1/$1 [L,NC] but i only get the default apache/centos test page. – LordNeo Jun 8 '15 at 13:37
  • Possibly try RewriteRule ^(.*)$ /%1$1 [L,NC] but you may also need "RewriteBase /" after "RewriteEngine On" – Dave Lozier Jun 8 '15 at 20:51
  • I got it working @DaveLozier just that if i enter test.example.com i get the url test.example.com/sd/template/test/ fetching the right contents. How can i remove the extra /sd/template/test/ from the url? – LordNeo Jul 11 '15 at 4:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.