Url Rewriting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vmpriyan
Forum Newbie
Posts: 2
Joined: Mon Sep 15, 2014 8:47 am

Url Rewriting

Post by vmpriyan »

Hi,

I have 2 domains "first.com" and "second.com" (both are pointed to same server).
I hosted a site under "first.com". When I type the url "second.com", the site should redirect to "first.com" but in address bar it should show only "second.com".
I tried the following code in .htaccess file, and its redirecting to first.com but url is also changing as first.com.

RewriteCond %{HTTP_HOST} ^second\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.second\.com$
RewriteRule ^/?$ "http\:\/\/first\.com\/" [R=301,L]

I want to show the url as second.com but redirect to first.com.

Anybody pls help me to achieve this.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Url Rewriting

Post by requinix »

Don't attempt any redirection at all. Because redirection, as the word implies, will redirect the user, and you don't want them to redirect. You want them to stay exactly where they are.

Make sure your Apache vhost is set up to accept both domain names for the site (ie, with a ServerName and a ServerAlias) and then make sure your application doesn't redirect - or if it must, redirects using either an absolute path without the domain name or a completely absolute URI using the current domain name (which you can find in $_SERVER["HTTP_HOST"]).
vmpriyan
Forum Newbie
Posts: 2
Joined: Mon Sep 15, 2014 8:47 am

Re: Url Rewriting

Post by vmpriyan »

Thank you Requinix for your reply,

But actually as per my requirement, I need to hide the typed url, (if a user types second.com, the site first.com should be served, and the typed url should not be changed).
Any idea to get this?

and I'm using a shared hosting.

Thank you
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Url Rewriting

Post by requinix »

Yes, and what I said fits that exactly.

Except you're using shared hosting. That makes it harder. If you have (a) both servers on the same account (b) and on the same physical machine, (c) shell access, and (d) some luck, you may be able to do it. Otherwise you should look into dedicated hosting options.
Post Reply