• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

I'd like a subdomain to be redirected to the domain httpdocs directory

D

denka

Guest
Hello

I have problems with a subdomain redirection to the domain httpdocs directory.
ie
URL sub1.domain.com
PATH ...httpd/vhosts/domain.com/subdomains/sub1/httpdocs/
to
URL sub1.domain.com
PATH ...httpd/vhosts/domain.com/httpdocs/

I have found the idea on this forum and make the vhost.conf in the directory ...httpd/vhosts/domain.com/conf/ within (I have changed the code a little)

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteMap lowercase int:tolower
ServerAlias *.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain.com$
RewriteCond %{HTTP_HOST} !^webmail\.domain.com$
RewriteCond %{HTTP_HOST} ^[^.]+\.domain.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+)\.domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs/$2
</IfModule>

And it works great!
But I've got the problem
All subdomains redirect to the ...httpd/vhosts/domain.com/httpdocs/ :(
I want the subdomain webmail to be not redirected like another subdomains...

Any ideas? Help me please.
Many thanks.
 
Put the following line as the first condition:

RewriteCond %{REQUEST_HOST} ^whatever.domain.com$

Change 'whatever' to the name of the subdomain you wish the rule to match on.

The first condition should be a 'match' for the subdomain you want the Rule to affect.
In this way, only the specific subdomain will be affected, not all subdomains.

Also, I've never had to \ the dots in the matching condition lines as you have in yours.
(and for future newbie readers, after editing the vhost.conf file you need to restart apache for the changes to take effect)
 
Thank you for your reply. Ive changed the vhost.conf

ServerAlias *.domain.com
RewriteEngine On
RewriteCond %{REQUEST_HOST} ^sub1.domain.com$ [OR]
RewriteCond %{REQUEST_HOST} ^sub2.domain.com$ [OR]
RewriteCond %{REQUEST_HOST} ^sub3.domain.com$ [OR]
RewriteCond %{REQUEST_HOST} ^sub4.domain.com$ [OR]
RewriteCond %{REQUEST_HOST} ^sub5.domain.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+).domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs/$2

But it still redirects me to the .../domain.com/httpdocs/ when I call another subdomains ... even a nonexistent subdomain

Where is error?
Many thanks
 
[Edit] - I cannot believe I wrote that. Ignore this post.

AFAIK, the ServerAlias should be surrounded by <VirtualHost> tags.

Just for giggles, try removing that line.
Time for a break and a vacation.... :D
 
I've tried. :(

Without ServerAlias It doesn't work...

I added the rows to etc/http/conf/http.conf

<VirtualHost *:80>
ServerAlias *.domain.com
</VirtualHost >

after Include /etc/httpd/conf/httpd.include row.
and removed ServerAlias *.domain.com from vhost.conf

The PHP doesn't work :(

Strange.
Many thanks
 
I edited my original post regarding ServerAlias, I don't know what I was thinking, the ServerAlias can go in the vhost.conf file.

Reverse your last changes to get back to where you were at before.

Changes to httpd.conf and httpd.include are overwritten by Plesk on occasion, so are not good places to put customizations.

Right now I'm boozed up to kill some pain, I don't see where it would still be applying the RewriteRule without matching the conditions above it....
 
Redirection to nonexistent subdomains is probably due to a * DNS entry for the domain.

What other contents in your vhost file are there? If you have additional Rewrite statements in the file, those could still be having an effect on the part you are trying to implement right now.

I just tested putting your same entries into my vhost above my other rewrite cond/rules, saved, restarted httpd and the original rules were still over taking the new ones.

Interestingly, when testing your Rewrites, I have found that Firefox is not acting properly with the rules. Mozilla is working properly with the rules.
 
Thanks for your care :)

The full content of vhost.config is

ServerAlias *.domain.com
RewriteEngine On
RewriteCond %{REQUEST_HOST} ^sub1.domain.com$ [OR]
RewriteCond %{REQUEST_HOST} ^sub2.domain.com$ [OR]
RewriteCond %{REQUEST_HOST} ^sub3.domain.com$ [OR]
RewriteCond %{REQUEST_HOST} ^sub4.domain.com$ [OR]
RewriteCond %{REQUEST_HOST} ^sub5.domain.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+).domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs/$2

No more rules/mods.

I tried this

ServerAlias *.domain.com
RewriteEngine On
RewriteCond %{REQUEST_HOST} ^sub1.domain.com$ [OR]
RewriteCond %{REQUEST_HOST} ^sub2.domain.com$ [OR]
RewriteCond %{REQUEST_HOST} ^sub3.domain.com$ [OR]
RewriteCond %{REQUEST_HOST} ^sub4.domain.com$ [OR]
RewriteCond %{REQUEST_HOST} ^sub5.domain.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^[^.]+ .domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs/$1

The same result...
I'm newbie in apache I don't know why this happens :confused:

Thanks
 
Ok, I found the problem. Had to turn on RewriteLog to find it. For whatever reason, after the vhost.conf rewrite rules are applied, then the Plesk rewrite rules are run on the output of your rules.

This is the vhost.conf file which does redirect just those subdomains which are entered as conditions to the domain's docroot index file, and does *not* affect webmail.domain.com (which is handled by Plesk in one of the httpd config files elsewhere)

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteLog "/home/httpd/vhosts/domain.com/httpdocs/rewrite_error.log"
RewriteLogLevel 9
RewriteMap lowercase int:tolower

ServerAlias *.domain.com
RewriteCond %{REQUEST_URI} !error_docs
RewriteCond %{HTTP_HOST} ^xyz.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^abc.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^def.domain.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+).domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs [L]
RewriteEngine off
</IfModule>


I suggest you leave out the RewriteLog and RewriteLogLevel for now.
Make sure the RewriteCond for !error_docs is present.
Other than the logging lines, Do not omit any other of these lines!
Please only replace the xyz, abc, def, and domain.com and do not make other changes
until you are sure it is working. As it is shown here, it does work fine under
Plesk 7.5.3 on RedHat
, and if someone enters a URL which is not defined as a subdomain on
your server, the Plesk wildcard DNS entry will still redirect it to your domain's
home page as well (assuming you have a *.domain.com in your DNS).

The reason we do *not* put the $1 on the end of the last rule line is because that would append the subdomain name. In other words, if the URL was abc.domain.com, then $1 = abc, and /home/httpd/vhosts/domain.com/httpdocs$1 would end up being /home/httpd/vhosts/domain.com/httpdocs/abc

which I don't think is what you want.

Remember to undo your earlier changes to http.conf and/or httpd.include!
 
Espcially Thanks to jamesyeeoc

Currently there are 3subdomains on my Plesk 7.5, Fedora
I've changed the vhost.conf file: full text here


<IfModule mod_rewrite.c>
RewriteEngine on
RewriteLog "/home/httpd/vhosts/domain.com/httpdocs/rewrite_error.log"
RewriteLogLevel 9
RewriteMap lowercase int:tolower

ServerAlias *.domain.com
RewriteCond %{REQUEST_URI} !error_docs
RewriteCond %{HTTP_HOST} ^sub1.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^sub2.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^sub3.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+).domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs$2 [L]
RewriteEngine off
</IfModule>


Reload the apache.
All Subdomains redirect to domain's httpdocs directory well.

Then add a new subdomain ie new.domain.com
And get redirection from new.domain.com to domain's httpdocs directory again :(
I don't want it. I want the new subdomains to be used their own httpdocs directories.
Maybe this can be done with open_basedir or something in anotherway???

Help me. SOS
Thanks tons
 
Ok, I was just helping you with your original code, and it turned out as a kludge.

Normally what I do is to put in rewrite sections for each subdomain, not a combination section as you have. But let's try this:

Note: I tested this and the mystuff, shop, xoops subdomains went to my docroot, undefined subdomains such as abcxyz and other random names also went to docroot. Other real subdomains (forum, files) went to the proper place. As well as webmail going to horde/imp worked fine.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteLog "/home/httpd/vhosts/your-domain.com/statistics/logs/rewrite_error.log"
RewriteLogLevel 0

RewriteCond %{REQUEST_URI} !error_docs
RewriteCond %{HTTP_HOST} ^mystuff.your-domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^shop.your-domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^xoops.your-domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^your-domain.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+).your-domain.com(.*) /home/httpd/vhosts/your-domain.com/httpdocs$2 [L]

ServerAlias forum.your-domain.com
RewriteCond %{HTTP_HOST} ^forum.your-domain.com$
RewriteCond %{REQUEST_URI} !^/subdomains/forum/
RewriteCond %{REQUEST_URI} !error_docs
RewriteRule (.*) /subdomains/forum/$1 [L]
ServerAlias files.your-domain.com
RewriteCond %{HTTP_HOST} ^files.your-domain.com$
RewriteCond %{REQUEST_URI} !^/subdomains/files/
RewriteCond %{REQUEST_URI} !error_docs
RewriteRule (.*) /subdomains/files/$1
</IfModule>

php_value session.save_path /home/httpd/vhosts/your-domain.com/httpsdocs/mambo/tmp

# for safe_mode 0=off 1=on
<Directory /home/httpd/vhosts/your-domain.com/httpdocs>
php_admin_flag engine on
php_admin_value register_globals 1
php_admin_value safe_mode 0
php_admin_value open_basedir /home/httpd/vhosts/your-domain.com/httpdocs:/tmp:/usr/share/pear:/proc:/etc:/bin
</Directory>
# Added /proc and /etc and /bin for phpsysinfo.php
Notice I don't have the ServerAlias *.your-domain.com, nor the RewriteEngine off at the bottom. You do not need to leave the logging level on except if you want to debug, so you can put 0 instead of 9.

Also note, this is not something I would normally do, I like having discreet sections for each and every subdomain.

Where did you come up with the original rewrite rules which you posted (and that we've been modifying)?
 
I have put the vhost.conf to the .../vhost/domain.com/conf/ folder.

It doesn't matter... I can use the dubdomains/.../conf/vhost.conf

I've tried to put this vhost.con to subdomains/kids/config folder

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteLog "/home/httpd/vhosts/domain.com/httpdocs/rewrite_error.log"
RewriteLogLevel 0
RewriteMap lowercase int:tolower

ServerAlias kids.domain.com
RewriteCond %{REQUEST_URI} !error_docs
RewriteCond %{HTTP_HOST} ^kids.domain.com$
RewriteRule ^kids.domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs$1 [L]
RewriteEngine off
</IfModule>

This doesn't redirect the kids.domain.com to domain's httpdocs folder.

Would you please find error? You are my hope!:) :)

Thanks tons
 
Instead of putting it under the /subdomains/kids/../conf folder, just incorporate it into the vhost.conf which is in the /home/httpd/vhosts/domain.com/conf/vhost.conf

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteLog "/home/httpd/vhosts/domain.com/statistics/logs/rewrite_error.log"
RewriteLogLevel 0

RewriteCond %{REQUEST_URI} !error_docs
RewriteCond %{HTTP_HOST} ^sub1.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^sub2.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^sub3.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+).domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs$2 [L]

ServerAlias kids.domain.com
RewriteCond %{HTTP_HOST} ^kids.domain.com$
RewriteCond %{REQUEST_URI} !^/subdomains/kids/
RewriteCond %{REQUEST_URI} !error_docs
RewriteRule (.*) /subdomains/kids/$1 [L]


</IfModule>
 
Thank you Thank you Thank you :D :D :D :D :D

This is the right vhost.conf for me:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteMap lowercase int:tolower

ServerAlias dev.domain.com
RewriteCond %{REQUEST_URI} !error_docs
RewriteCond %{HTTP_HOST} ^dev.domain.com$
RewriteRule dev.domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs$1 [L]

ServerAlias forums.domain.com
RewriteCond %{REQUEST_URI} !error_docs
RewriteCond %{HTTP_HOST} ^forums.domain.com$
RewriteRule forums.domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs$1 [L]

RewriteEngine off
</IfModule>


Have put the vhost.conf to .../vhost/domain.com/conf folder. And it works like a sharm !!!!!!!!!!!!!!!!!
Other subdomains restrict their own httpdocs.

Thanks a million :) :) :)
 
But I've got a small problem :( :rolleyes:

The Plesk adds a new subdomain and it redirects it to the www.domain.com, i.e new.domain.com redirects to www.domain.com

And it needs to restart apache...
Then all works OK again.

:(

Many thanks:
 
For any changes to take effect, apache must be restarted. If Plesk is not restarting Apache when you add a subdomain, that is a different problem altogether.
Then all works OK again.
So it's working then?

When you first create a subdomain, you need to put at least an index.html some sort of index file into the subdomain's directory.

I no longer use Plesk to create subdomains, so I'm not sure if it overwrites things, or just adds things to the vhost file (can't remember). If it overwrites, then all you can do is to remember to go back and manually re-edit the file.
 
ok maybe a stupid question

but i can't find a vhost.conf file in the conf folder,

only httpd.include httpd.include.bak webalizer.conf


Any ideas?!
 
Ok i have setup a range of subdomains in the rewrite rules appearing like this:

Code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteLog "/home/httpd/vhosts/domain.com/statistics/logs/rewrite_error.log"
RewriteLogLevel 0

RewriteCond %{REQUEST_URI} !error_docs
RewriteCond %{HTTP_HOST} ^sub1.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^sub2.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+).domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs$2 [L]

</IfModule>

I ran /usr/local/psa/admin/sbin/websrvmng --reconfigure-all

rcapache2 restart

And all was OK!

But on visiting sub1.domain.com i get the default domain for the IP of my server.

Should i disable the *.domain.com A record pointing to the server IP?

Have i missed something step?
 
Back
Top