Forum Moderators: phranque

Message Too Old, No Replies

Subdomain redirect without changing browser URL

         

johnahya

10:54 am on Jun 13, 2012 (gmt 0)

10+ Year Member



Hi,

I am developing eCommerce store and facing some issue related to htaccess rewrite for sub domains.

I have a main store say www.example.com & few categories like necklaces, earrings, rings etc. I need to setup sub domain for each categories like necklaces.example.com, earrings.example.com, rings.example.com etc.

So If I go to necklaces.example.com, it should show content of http://www.example.com/necklaces.html page. I dont want to redirect to that page. I want that browser URL should not change & it will call http://www.example.com/necklaces.html in background.

I tried with below rewrite rule but browser URL is changing to http://www.example.com/necklaces.html.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^necklaces\.* [NC]
RewriteRule .* http://www.example.com/necklaces.html [L]

I dont want to change browser URL. Please help me in this regard.

Thanks,
John

[edited by: incrediBILL at 11:55 am (utc) on Jun 16, 2012]
[edit reason] fixed URLS, use Example.com [/edit]

g1smd

11:10 am on Jun 13, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



URLs are used "out there" on the web. URLs always have a domain name.

Filepaths are used "here" inside the server. Filepaths don't have a domain name. Domain names mean nothing inside the server.

URLs and filepaths are not at all the same thing. Don't confuse those two things or you will never understand mod_rewrite.

A redirect tells a browser to make a new request for a new URL. A RewriteRule with domain name in the target or the R flag (or both) is a redirect. You don't want that. Redirects always change the URL seen in the address bar of the browser.

What you need is an internal rewrite, one that maps the subdomain URL request to the internal server filepath. Luckily RewriteRule can be configured to deliver redirects or to perform an internal rewrite with only minor changes in syntax.

Before you do any of that you will need to set the DNS so that subdomains actually resolve to your server. You'll also need to take steps to ensure that only valid requests will return content.

The URL examples in your post are unreadable as you didn't follow the instructions in [webmasterworld.com...]

johnahya

11:45 am on Jun 13, 2012 (gmt 0)

10+ Year Member



Hi,

Here am posting again. Sorry for the inconvenience.

I am developing eCommerce store and facing some issue related to htaccess rewrite for sub domains.

I have a main store say www.example.com & few categories like necklaces, earrings, rings etc. I need to setup sub domain for each categories like necklaces.example.com, earrings.example.com, rings.example.com etc.

So If I go to necklaces.example.com, it should show content of http://www.example.com/necklaces.html page. I dont want to redirect to that page. I want that browser URL should not change & it will call http://www.example.com/necklaces.html in background.

I tried with below rewrite rule but browser URL is changing to http://www.example.com/necklaces.html

RewriteEngine on
RewriteCond %{HTTP_HOST} ^necklaces\.* [NC]
RewriteRule .* http://www.example.com/necklaces.html [L]

I dont want to change browser URL. Please help me in this regard.

Thanks,
John

lucy24

7:12 pm on Jun 13, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your rule is configured as a Redirect. Normally this is done by using the [R] flag, but when you give the complete protocol-plus-domain, it turns into a Redirect no matter what flags you have or don't have. Possibly excluding [P] and related flags, but I'm not going there.

If your subdomains are properly mapped, you may not need a RewriteRule at all. Putting the content of subdomains into physical directories within the main domain directory is the most common way of handling subdomains, so that shouldn't be a problem.

johnahya

7:11 am on Jun 14, 2012 (gmt 0)

10+ Year Member



Hi,

Right now I am having same issue that is describe in below post:
[webmasterworld.com ]

I tried work out given in that post but its not working.

Can anyone help me with this? I am trying to solve it from last 2 days but not succeed.

Thanks,
John

lucy24

8:06 am on Jun 14, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ooh, great answer from jdMorgan. Lemme cut & paste here for future swiping:
What about this code does not work? Does it do something wrong, not do anything at all, or what?

How did you test? What were the expected results? What were the actual results? How did the actual results differ from the expected results? Any error messages in the browser? Any relevant entries in the server error log? Did you completely flush (delete) your browser cache before testing?


The next answer is also to the point:
Did you configure your DNS to map these subdomains to your server? If not, you'll need to do that.

(He goes on to talk about fixed IP addresses, but that part isn't mandatory in all circumstances.)

On the other hand, this line from the OP
I need to have multiple subdomains redirect to specific product category URLs, without showing customer

is, ahem, uh, what's the word? Gibberish, let's say.

That earlier thread seems to have-- haha-- left everyone hanging, so I don't know where it ended up. Since it dates from 2009, you have to assume the OP has now solved his problem. Or chucked it all and went into selling insurance instead. But I can't figure out if he was saying that www.example.com/bluewidgets (the place that holds the content for bluewidgets.example.com) was a physical location, an URL, or both.

At least in your case it's pretty clear which bits are URLs and which bits are physical directories. But we're still waiting for news from the DNS front.

johnahya

9:31 am on Jun 14, 2012 (gmt 0)

10+ Year Member



Hi,

In my case I am trying to setup subdomain locally using virtual hosts.

I have added below code in my httpd-vhosts file:
<VirtualHost *:80>
DocumentRoot "C:\wamp\www\example"
ServerName local.example.com
</VirtualHost>

<VirtualHost *:80>
DocumentRoot "C:\wamp\www\example\necklaces"
ServerName necklaces.example.com
</VirtualHost>


All source code resides in example folder. I need to setup 30-40 subdomain so its not feasible to copy whole source code in each subdomain folders. I have URL structure like below:

http://local.example.com
[local.example.com...]
[local.example.com...]

What I want is to have only .htaccess file in subdomain folder that allow rewrite (in my case C:\wamp\www\example\necklaces folder).

So if I write [necklaces.example.com...] in browser it should call page [local.example.com...] & browser URL should not be changed.

Hope it make sense. I am newbie for subdomain rewrites. Please help to me to get it work.

Thanks,
John

g1smd

7:02 pm on Jun 14, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You're going to have to add a whole load of named subdomains pointing to 127.0.0.1 in your HOSTS file in order to simulate the online DNS situation.

Without that step NOTHING will work.