URL Rewriting and Sub-Domains

Hi to all,

Here is my clarification in URL rewriting as a thread and hope this thread would turn into a great resource over the Web.

How to convert www.mydomain.com/folder/anyfile.php?user=xyz TO xyz.mydomain.com/anyfile.php?user=xyz

  • Whether I need to create sub-domain for deploying the above?

Please give the right code to accomplish the above.

Let me explain my situation exactly.

I have many files in the directory

like www.domain.com/company/abc.php, etc…

I want the URL should appear with the USERNAME of Logged in user, So I am going to add the username to all the links as www.mydomain.com/company/abc.php?user=username

So now what I need it the above URL should be [username.mydomain.com…]

For this I will make all the links in the site as <a href=‘username.mydomain.com/company/abc.html’>Link </a>

Now What I have to write in the .htaccess file. If you could give me the code for single file that is enough I can duplicate it to all other files.

Thanks in advance


I did that already - look for the colored text above.

The RewriteEngine off/on commands are similar to /* */ in PHP and Javascript. It’s a good practice to use the RewriteEngine on before your mod_rewrite code, too.



I have used = to hold the value of the sub-domain instead of the www.

can you please let me me know what should be added instead of it. I mean can you rewrite the second condition as right one.

And Do I want to add another code like “rewrite engine on” in the .htaccess file? Since I have only the above code in it.



I have tried the following link


where the .htaccess file have

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteCond %1 !=www
RewriteRule ^company/([^./]+)\.html$ /company/$1.php?user=%1 [L]

I have a file namely super.php in Company folder (this file just print the Value that is passed in get as ‘user’)

And I got the Error as URL not found.

I have confirmed that the Wild_card is activated in my server by my hosting provider.

Can you please tell me where I am wrong?

Thanks in advance.


The only problem that I see is the use of = in your second RewriteCond - that’s what matching does so your = is inappropriate.

RewriteCond &#37;{HTTP_HOST} ^([^.]+)\\.example\\.com$ [NC]
# be sure that you're looking at the {HTTP_HOST} case Insensitively
[COLOR="Blue"]RewriteCond %1 !www$[/COLOR]
# that's how to check for an exact match! 
RewriteRule ^company/([^./]+)\\.html$ [COLOR="Red"]/[/COLOR]company/$1.php?user=%1 [L]
# that / leading the redirect can get confused as the physical root
# - don't use it if you don't need it!




The first thing you need to do is create the subdomains (Dynamically configured mass virtual hosting) as they ALL act like different domains (and are treated that way by your DNS as well as Apache). Your host can allow/disallow that so check with them before you run off in this chase.

Second, which way are you redirecting? That’s a common problem with “newbies” as they want to redirect from something that can be served by Apache to something which does not exist. In other words, are you redirecting from the subdomain to the REAL link in the maindomain’s folder subdirectory (so long as the wildcard subdomains share the main domain’s DocumentRoot, this is not a problem)? Just remember that YOU must create the links the way that you want them seen THEN create the mod_rewrite code to redirect to the file that Apache can serve your visitor.

Okay, the rest of that looked like a “script kiddie” request so let me point you in the right direction (we do NOT do free coding here - we TEACH and ensure that you understand what YOUR code is doing).

First, you can read all about mod_rewrite in the link in my signature and the sticky threads (hmmm :nono: ).

Second, you will HAVE to use a RewriteCond statement to examine the {HTTP_HOST} Apache variable to capture the subdomain’s value. Then it is a simple matter to (internally) redirect to the folder with the URI intact (or utilizing the subdomain value).

Please remember that we are here to help you learn and will ensure that you understand what your code is doing.