About Subdomain masking for CMS

Hi there Sitepoint,

This is my first post in this forum, and I am not that good with .htaccess. Please bear with me.

I have searched around the sitepoint.com and have tried the solutions, but none works.

So here is my situation.
I have a website, say the name is mydomain.com

mydomain.com is using CMS, so that there are several non-existent directory, such as “about” in www.mydomain.com/home/about/thissite.html

The problem is that I need to make this subdomain:

sub.mydomain.com

so that whenever people type “sub.mydomain.com

  1. they get the content of www.mydomain.com/home/id/ (where “id” is non-existent directory)
  2. but the URL bar at the top still displays sub.mydomain.com (hence this is not 301 redirect)
    it is just one particular subdomain (in this example, it is “sub”), not any subdomains.

that means if I type “sub.mydomain.com/something.html” (must be “html”), then I will get the content of

www.mydomain.com/home/id/something.html

with “sub.mydomain.com/something.html” still displayed at the top.

In addition to the info above, this is the current .htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*\.html$ index.php [L]

RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteCond %{REQUEST_URI} !^/home/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /home/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteRule ^(/)?$ home/index.php [R=301,L]

so that if user type “mydomain.com” they will be redirected to “mydomain.com/home/index.php

What should I do to solve this?
Do I have to make a subdomain “sub” in the cPanel, or non-existent/virtual subdomain is also fine?

I have done many trials and errors for 2 days and I still havent managed to solve this. frustrated

Please help

Regards,
Ryonn

Hi, and welcome to SitePoint :wave:

First, allow me to walk through your current .htaccess


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*\\.html$ index.php [L]

Okay, the idea is good, but you don’t need the .*
In fact it’s better (faster) if you drop it.
.*\.html$ means “Look for a bunch of stuff and then the string should end in .html”
just \.html$ means “Look for a string that ends in .html”, so the regex engine has less to look for, which makes it faster


RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteCond %{REQUEST_URI} !^/home/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /home/$1

  1. Escape dots in regex. Instead of ^(www.)?mydomain.com$, use ^(www\.)?mydomain\.com$

  2. Stay clear from :redhot: (.*) :redhot:. There is a much more efficient and elegant solution for this problem.

  3. Remove the / in front of /home

  4. Make sure Apache matches the domain name case insensitive, so it will also work if people request [noparse]www.Domain.Com[/noparse] for example. Add [NC] at the end of the is first line.

So you get:


RewriteCond %{HTTP_HOST} ^(www\\.)?mydomain\\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/home/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? home%{REQUEST_URI} [L]

Instead of matching the URL, remembering it, and putting it at the end of the URL this just uses the %{REQUEST_URI} variable which contains exactly that value, and saves the regex engine some trouble.
I’ve also removed the / in front of /home because it will make Apache look in /home in the filesystem first, not at the URL. Takes longer to process and it might even produce very unwanted results since /home actually exists on linux filesystems! (security breach!)


RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteRule ^(/)?$ home/index.php [R=301,L]

Don’t use this. This is what the DirectoryIndex is for


DirectoryIndex index.php

Yes you have to make an actual subdomain for this; there is no such thing as a non-existent/virtual subdomain.

Then make sure Apache serves the same content on that subdomain as it does for the main domain, and add code in the .htaccess along the lines of

 
# If somebody request something on the subdomain ...
RewriteCond %{HTTP_HOST} ^sub\\.domain\\.com$ [NC] 
# .. and the URL has not been rewritten to /id/ yet
RewriteCond %{REQUEST_URI} !^/id/
# ... rewrite it
RewriteRule .? id%{REQUEST_URI} [L]

As a last tip: Think about if you want the use your domain with www or without and make one redirect to the other. Don’t make it optional everywhere so both work (and people could get confused and seach engines might punish you for duplicate content!)

HTH

Hi Scallio,

I’m glad that I’m in good hand now =)

All of your optimizations work except this one:

DirectoryIndex index.php

which I change to

DirectoryIndex home/index.php

The following is still not working though:

# If somebody request something on the subdomain ...
RewriteCond %{HTTP_HOST} ^sub\\.domain\\.com$ [NC] 
# .. and the URL has not been rewritten to /id/ yet
RewriteCond %{REQUEST_URI} !^/id/
# ... rewrite it
RewriteRule .? id%{REQUEST_URI} [L]

I also try this but doesnt work too:

# If somebody request something on the subdomain ...
RewriteCond %{HTTP_HOST} ^sub\\.domain\\.com$ [NC] 
# .. and the URL has not been rewritten to /id/ yet
RewriteCond %{REQUEST_URI} !^/home/id/
# ... rewrite it
RewriteRule .? home/id%{REQUEST_URI} [L]

I have made sub.mydomain.com subdomain in cPanel with document root at:

public_html/home/id

and redirection to None

I try entering this: “sub.mydomain.com/something.html
and I get this error:

Not Found

The requested URL /id/index.php was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

I try entering this: “sub.mydomain.com
and I get this error:

Not Found

The requested URL /id/ was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

I try entering this: “sub.mydomain.com/virtual-folder/something.html
and I get this error:

Not Found

The requested URL /id/index.php was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

the public_html/home/id does not have anything inside apart from empty cgi-bin

is there something that I miss?

oh yah… and about this

As a last tip: Think about if you want the use your domain with www or without and make one redirect to the other. Don’t make it optional everywhere so both work (and people could get confused and seach engines might punish you for duplicate content!)

I have the following to handle it

RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

Can it be optimized too?

Please help

more update/finding…

The “index.php” that the system wants in
“The requested URL /id/index.php was not found on this server.”

is located in /home/myaccount/public_html/home/index.php

sorry for keep posting this… I cannot edit the previous message. =/

Could you please let me know why the following is not working?

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \\.html$ /home/myaccount/public_html/home/index.php [L]

Where is the .htaccess located? In the public_html directory?

If so, you should be able to just use:


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \\.html$ home/index.php [L]

If that doesn’t work your host is probably using mass virtual hosting. In which case you can try this:


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \\.html$ [color="red"]/[/color]home/index.php [L]

Both are still not working, Scallio =(

http://www.mydomain.com/home/id/something.html works

but not with

sub.mydomain.com/something.html

although the document root of sub.mydomain.com is in public_html/home/id

Not working how? Do you get an error? If so, what does it say?

this is the error for both “home” and “/home”

Not Found

The requested URL /id/home/index.php was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Sorry, I completely missed the 3rd post of this thread :blush:

If you’ve set the document root of your subdomain to public_html/home/id it seems to me you don’t need any rewriterules for that at all. It should just work without any extra .htaccess configuration.
Have you tried that?

Hi Scallio,

I end up using PHP URL masking to handle the request.
It’s more flexible that way.

Thank you for your help so far. Really appreciate it =)

Regards,
R