Welcome to WebmasterWorld Guest from 54.205.119.93

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

URL Rewriting and Subdomains - Mega Thread

   
11:22 am on May 26, 2010 (gmt 0)

5+ Year Member



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.

1. 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.

Thanks in Advance.
11:37 am on May 26, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi there icosytech,

Welcome to the forum :)

It sounds like you are after a .htacces/rewrite rule there to me. Try the apache forum: [webmasterworld.com ] Plenty of people in there who can assist with that.

Good luck,
Cheers,
MRb
5:42 pm on May 26, 2010 (gmt 0)

5+ Year Member



OK Thanks MRb...
2:05 am on May 27, 2010 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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


If I read you right, you're making the same error most do when taking their first stab at rewrite. You have it backwards. in your page, you have

<a href="/anyfile.php?user=xyz">XYZ</a>

Then you rewrite to xyz.mydomain.com/folder/anyfile.php?user=xyz

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^anyfile.php\?([a-z=]*)$ [xyz.mydomain.com...] [NC]
</ifModule>

Better yet,

<a href="/Users/xyz">XYZ</a>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^Users\/([a-z]+)$ [xyz.mydomain.com...] [NC]
</ifModule>

Had to use https so the stupid widget here doesn't screw up the code! You wouldn't use https . . . .

So if someone requests just /Users, user= will be empty, and you can manage it accordingly (show full user list?)

[NC] makes it case insensitive.
9:12 pm on May 27, 2010 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The RewriteRule regex pattern cannot see query string values in the request.

You need a preceding RewriteCond to examine the QUERY_STRING server variable.

Every rule needs the [L] flag added.

For redirects, if the [R=301,L] flag is omitted, you'll get a 302 redirect.

All of the rules above are redirects (as the target mentions a domain name).

If you want a rewrite, as the original question hinted, the target will be just a server filepath not a URL (i.e. the target will not contain domain name).
6:33 am on May 28, 2010 (gmt 0)

5+ Year Member



If I read you right, you're making the same error most do when taking their first stab at rewrite. You have it backwards. in your page, you have

<a href="/anyfile.php?user=xyz">XYZ</a>

Then you rewrite to xyz.mydomain.com/folder/anyfile.php?user=xyz

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^anyfile.php\?([a-z=]*)$ [xyz.mydomain.com...] [NC]
</ifModule>

Better yet,

<a href="/Users/xyz">XYZ</a>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^Users\/([a-z]+)$ [xyz.mydomain.com...] [NC]
</ifModule>

Had to use https so the stupid widget here doesn't screw up the code! You wouldn't use https . . . .

So if someone requests just /Users, user= will be empty, and you can manage it accordingly (show full user list?)

[NC] makes it case insensitive.



I am not clear with the above code.

Let me explain my situation exactly.

I have many files in the directory
www.mydomain.com/company/

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
2:15 pm on May 28, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



This is what the above replies are commenting about:

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.example.com/company/abc.php?user=username

That statement is incorrect, in that it contradicts the next two lines you posted:

So now what I need it the above URL should be http://username.example.com/company/abc.html

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

It's very important in this kind of project to distinguish between a URL --as defined by the links published on your HTML pages-- and the internal server filepath. Getting these two things mixed-up causes all sorts of problems, and makes the project seem much harder than it really is.

Based on what I've read, here's what you need to do:
  1. Change the links that appear on your HTML pages to <a href='http://username.example.com/company/abc.html'>Link</a>

  2. Add a rule to your top-level .htaccess file to internally rewrite requests for those URLs to the filepaths for your scripts:
     RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
    RewriteCond %1 !=www
    RewriteRule ^company/([^./]+)\.html$ /company/$1.php?user=%1 [L]

  3. Optionally, add another rule to prevent the script filepath from being used as a URL:
     RewriteCond %{THE_REQUEST} ^[A-Z]+\ /company/[^.]+\.php\?user=([^&\ ]+)\ HTTP/
    RewriteRule ^company/([^.]+)\.php$ http://%1.example.com/comapany/$1.html? [R=301,L]

So the "old" URL was www.example.com/company/abc.php?user=acme-widgets
The script filepath was and still is /company/abc.php?user=acme-widgets
and the "new" URL is acme-widgets.example.com/company/abc.html

This code assumes that "user=" is the only query string parameter passed to "abc.php". If other parameters may be present in the query string, then the code will need to be expanded.

Note that rule order is important. Put all of your access control rules first, followed by all of your external redirects, ordered from most-specific patterns and conditions (one or a few URLs affected) to least-specific patterns and conditions (many or all URLs affected). Finish up with all of your internal rewrites, again in order from most- to least-specific. To illustrate, the second rule posted above should precede the the first.

You may need to modify your DNS and server configuration to accept "wild-card" subdomains if this has not already been done. This assumes that you are on a server with a unique IP address -- an IP_based server versus a name-based server. If you are on a name-based server, then you will have to define each new subdomain in the server config -- probably by using a "control panel" function. In many cases, control panels on name-based servers put each subdomain into a separate filespace, and the technique discussed here will not work in that situation.

Jim
12:37 pm on May 31, 2010 (gmt 0)

5+ Year Member



Hi Jim,

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


but it says server not found (at something.example.com)

And the my site lies at "public_html/example/"

So I have to add this rule to "public_html/example/.htaccess" or "public_html/.htaccess" ?

- Srihari
1:05 pm on May 31, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



"Server Not Found" indicates that you have not configured your DNS properly, and that the IP addresses for your subdomains are undefined.

Edit your DNS zone file to add wild-card subdomains as previously stated.

Jim
5:53 pm on Jun 7, 2010 (gmt 0)

5+ Year Member



I have tried the following link

[xyz.example.com...]

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.
5:56 pm on Jun 7, 2010 (gmt 0)

5+ Year Member



Whether I have to any other code before this code in .htaccess file?
3:59 am on Jun 8, 2010 (gmt 0)

5+ Year Member



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


The above was suggested by another expert, Can anyone please let me know the concerns and Give me the clear code of it.

Thanks
Srihari
6:19 pm on Jun 8, 2010 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The = syntax is fine.

No escaping of literal periods is needed when the = sign is used.
4:18 pm on Jun 9, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



> Whether I have to any other code before this code in .htaccess file?

Yes, you will need either both of these lines or only the second one before any RewriteConds or RewriteRules in your code -- You will have to test to find out if the first line is needed (or allowed on your server).

Options +FollowSymLinks
RewriteEngine on

Jim