• The Horde webmail has been deprecated. Its complete removal is scheduled for April 2025. For details and recommended actions, see the Feature and Deprecation Plan.
  • We’re working on enhancing the Monitoring feature in Plesk, and we could really use your expertise! If you’re open to sharing your experiences with server and website monitoring or providing feedback, we’d love to have a one-hour online meeting with you.

Subdomain - variable Mod Rewrite doesnt work. Please help

H

hp_solomon

Guest
simple rewriting rules worked on me like redirecting or renaming the URL in the same directory but when i tried to rewrite URL that has subdomains and folder it doesn't work. I don't know why.

Here's the script I created on .htaccess located in my root website

RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_URI}!^/index\.php
RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
RewriteRule .* http://www.domain.com/viewforum.php?f=%1 [nc]

What i want to achieve is that urls of

http://variable.domain.com will access the page http://www.domain.com/viewforum.php?f=variable

However, the address bar should still show http://variable.domain.com.

This script doesnt work... I don't know why. This is the error i get from the page:

------------------
This is the Pleskâ„¢ default page
If you see this page it means:

1) hosting for this domain is not configured
or
2) there's no such domain registered in Plesk.

For more information please contact Administrator.
------------------



Any Ideas? Please help, I've been researching the net and tried so many but still I failed.

:(
 
Rewrite subdomain to folder by proxy

1) I did not test this in an .htaccess file, but rather, in the domain's vhost.conf file (/home/httpd/vhosts/domain.com/conf/vhost.conf)

2) I don't have a forum installed on the docroot of the test domain, so I used a short viewforum.php with just:
<?php
echo $f;
?>

This is the rewrite section which will redirect the requests, pass the parameter, and not change the browser's URL:
Code:
<IfModule mod_rewrite.c>
# Turn on the rewrite engine
RewriteEngine on
# Make sure all input is lowercase for comparison
RewriteMap	lowercase	int:tolower

# All sub names to be matched
ServerAlias *.domain.com

# Not index.php requested
RewriteCond %{REQUEST_URI} !^/index.php

# Not [url]www.[/url] requested
RewriteCond %{HTTP_HOST} !^[url]www.domain.com[/url]$

# subname present in request
RewriteCond %{HTTP_HOST} ^(.+).domain.com

# Do the PROXY redirect to preserve the browser URL
RewriteRule .* [url]http://www.domain.com/viewforum.php?f=%1[/url] [L,P]
</IfModule>
Feel free to try putting it into your .htaccess if you like, but this has been tested to work in the domain's vhost.conf file in any case. It also assumes the domain's DNS has a wildcard entry.

I browsed to 'aaa.domain.com' (which does not exist) and was shown 'aaa' by the echo script, browser URL still showed http://aaa.domain.com. Same thing for other subdomain names tested 'xyz', 'abc', etc.

Hope this helps.

If you wish to see it work, browse to:
http://something.james-yee.net

then

http://www.james-yee.net/viewforum.php?f=something
(replace 'something' with whatever random name you wish)

I will leave this in place for a day or so.
 
james, thanks for the reply.

How can I add DNS wildcard entry in the plesk DNS page. I clicked the DNS page but there are options on the type of DNS that should be added, these are A, CNAME, TXT, etc.

Please teach me on how to add the wildcard entry using the PLESK DNS page.

The link you posted above doesn't work too...

Thanks,
 
Make sure you change the DOMAIN DNS, you can do the Server Template later to affect all newly created domains.

1. Login to the control panel.

2. Click on domains.

3. Click on the domain you want to modify.

4. Click on DNS.

5. Select type CNAME

6. Click ADD

7. Put an asterisk in the "Enter Domain

8. Name" field (the first field).

9. Fill in the domain name with a period at the end in the second field. (The period is IMPORTANT)

10. Click Ok.

You have now put a wildcard CNAME entry for the domain into the domain's DNS.

I'll have to relax my firewall, probably your IP is in one of the country blocks I have (even on my test servers).
Ok, I've removed all the country blocks, so give it another try. At some point I will re-enable my full firewall...
 
what name should i enter on the 2nd field? any name?

is this valid? say

subdomain.

or

anyname.

(with a period at end)

Thanks.
 
If you wanted to create the subdomain wildcard entry "*.plesk.com", then in the second field you would enter: plesk.com. since the domain name is plesk.com

The first field would be where you would put the subdomain name, in this case you put an asterisk character to make a 'wildcard' entry.

Wildcard entries will allow any browser requested subdomain names for the domain to be directed to the proper domain. Without it, if I were to browse to "xyz.domain.com" but this subdomain had never been created on the domain, then I would receive an error that the URL could not be found or does not exist.
 
I was able to add the wildcard entry on the dns page.

My problem now is how to edit the vhost.conf file.

then through SSH, i went to the directory but there is no such file.
How can i create it? I typed on the command line:

pico vhosts.conf

but nothing happens, instead it returned:

-bash: pico: command not found.

Lastly, is this the whole code that should be entered in vhosts.conf? no begin tag for the file?

<IfModule mod_rewrite.c>
# Turn on the rewrite engine
RewriteEngine on
# Make sure all input is lowercase for comparison
RewriteMap lowercase int:tolower

# All sub names to be matched
ServerAlias *.domain.com

# Not index.php requested
RewriteCond %{REQUEST_URI} !^/index.php

# Not www. requested
RewriteCond %{HTTP_HOST} !^www.domain.com$

# subname present in request
RewriteCond %{HTTP_HOST} ^(.+).domain.com

# Do the PROXY redirect to preserve the browser URL
RewriteRule .* http://www.domain.com/viewforum.php?f=%1 [L,P]
</IfModule>




Thanks,
 
i created the file using vi using the exact code you posted, replacing domain.com to my domain.com

But still doesnt work. Should i restart the server? or apache? I also noticed that in your code, it doesnt have a slash "/" before the dot (.) of the domain name, is this ok?

Thanks,
 
After making any changes to apache conf files, you need to do the following:

/usr/local/psa/admin/bin/websrvmng -u --vhost-name=domain.com

restart apache service


(replace domain.com with the actual domain name)
 
My hosting said that i should run

/usr/local/psa/admin/bin/websrvmng -a -v

but failed

it says

httpd failed to gracefully restart.


i think there is a syntax error.. my hosting said

RewriteMap takes two arguments, a mapname and a filename

RewriteMap lowercaseint:tolower


what should i run?
/usr/local/psa/admin/bin/websrvmng -a -v

or

/usr/local/psa/admin/bin/websrvmng -u --vhost-name=domain.com


thanks.
 
ok.. i fixed the vhost.conf. i missed the space between lowercase and int, my bad. sorry.

Now i got a new problem

all images, css, all links are now gone.. they are now pointed relative to the subdomain, not to the document root of the domain name...

Any solution to this? a rewrite rule maybe? or should i update all links to absolutely map from the document root of domain name.

please advice. Thanks.
 
When you begin messing with rewriting URI/URLs sometimes paths do need to be adjusted from relative to hard.

Since you asked to rewrite a 'variable' subdomain name, then paths really should be hard paths, not relative.

To tell you the truth, this is the first time I have ever seen anyone want to do this.
 
ah ok... I'll just hard coded it then...

Thank you very much for the help... :)

by the way, is rewriting URLs takes up too much server resources? like the way i did?
 
I guess that depends on your server specs and how busy the server gets. In general, I don't think I've ever seen or heard of a decent server getting bogged down due to rewrites, I wouldn't worry about it.

Additional note about the websrvmng command: Plesk recommends doing the -v -a command, but there appears to be times when that doesn't do the trick. I have never seen the full -u --vhost-name=domain.com command not do what it is supposed to.
 
the site is a music site that offers free forum for registered bands.

To access the bands forum you need to go to the viewforum.php page passing the band id or the band name as a parameter.

It would be easier and better for bands and members of the community if all bands have their own subdomain.

And with your help I was able to achive to rewrite URLs to make it seem like a real subdomain. Thanks.

I'm not yet complete with making the links absolute to make the images and links work properly.

Now i got a new problem. When I logged in to the site and go to the page using a link or typing the URL of the fake subdomain , my session dissapeared. Everytime I go to the page even I am logged in, I was logged out or not totally logged out because when i go back to the page without the subdomain, i was logged in again.

Seems like using the subdomain my session dissapeared but the session is there when i go back to the page without the subdomain.

Is there a way to maintain a single session to multiple subdomain, or is there a solution to fix this issue. Any advice.?

Please help.

Thank you very much.
 
Here is the directory structure of the site when i access it through FTP:
bin
cgi-bin
conf
error_docs
etc
httpdocs ---> root folder of the site... and the root of phpbb
lib
pd
private
subdomains ---> empty, no folders and files
tmp
usr
var
web_user


I think the solution to this is to point all my subdomain to the directory root of my domain name. that is on /httpdocs/ directory.

How can i point all my subdomains to the root directory of my main subdomain? Or how can i make my subdomains be under the root domain directory.


Please help.

I hope someone can help me for I am trying to fix this for more than a week now. Thank you very much.
 
Found this script on other threads

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteMap lowercase int:tolower
ServerAlias *.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain.com$
RewriteCond %{HTTP_HOST} !^chat\.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/$1$2
</IfModule>


will this work on my situation.. Changing the previously created script to:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteMap lowercase int:tolower
ServerAlias *.domain.com
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteCond %{HTTP_HOST} !^webmail\.domain.com$
RewriteCond %{HTTP_HOST} ^[^.]+\.domain.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule .* http://www.domain.com/viewforum.php?f=%1 [P]
RewriteRule ^([^.]+).domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs/$2 [L]
</IfModule>


Any ideas?
 
Originally posted by hp_solomon
Found this script on other threads

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteMap lowercase int:tolower
ServerAlias *.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain.com$
RewriteCond %{HTTP_HOST} !^chat\.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/$1$2
</IfModule>


will this work on my situation.. Changing the previously created script to:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteMap lowercase int:tolower
ServerAlias *.domain.com
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteCond %{HTTP_HOST} !^webmail\.domain.com$
RewriteCond %{HTTP_HOST} ^[^.]+\.domain.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule .* http://www.domain.com/viewforum.php?f=%1 [P]
RewriteRule ^([^.]+).domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs/$2 [L]
</IfModule>


Any ideas?
Sorry I'm too tired to read the entire thread, but looking at your version of what you found elsewhere will not work properly at all. At the moment, all I can recommend is that you do some reading on Apache's mod_rewrite. I would dissect it and try to fix it, but I really am too tired right now.

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

I know their docs are a bit cryptic at times, but you will see some of the blatant errors in what you have added/changed in your version.
 
Thanks for the reply Shadowman, i tried to read the link you posted, and tried some script too, but still I failed. I can't find the right script ...

I'm sure I only need the right script for the mod_rewrite to work it properly. If your not too busy or if you find time, can you please help me provide the right script, I'm sure it will only take u a minute or 10. :D You are my hope.

Thank you very much...
 
Back
Top