Multiple domains for site subsections - php

Here's the deal. Basically I've got multiple domains, and I would like one of the domains to point to the regular base of the site, but the other domains to point to a subsection of the site without modifying the url in the address bar. The idea is that each of the extra domains are branded for their section.
Example:
www.domain.com Top level of site
www.domain2.com points to www.domain.com/abc
www.domain3.com points to www.domain.com/def
etc.
Also note that in the example 'abc' and 'def' are not real directories on the filesystem, but are virtual areas defined in a database.
I've spent quite a bit of time looking around and couldn't find anything that fits this. I do not want to use domain cloaking because it uses frames. Regular redirects obviously are easy to point to the right thing, but they change the url in the address bar.
Is there any way to accomplish this?
Edit:
I've added the alias as Mark suggested below, but can anyone shed light on how I could then use mod_rewrite to make this work?

First, your DNS records need to point to the IP of the web server.
Second, you have to set up Apache to serve multiple domains from the same VirtualHost. Enable mod_alias and then use the ServerAlias directive in your VirtualHost definition. For example:
ServerName www.domain.com
ServerAlias domain.com www.domain2.com domain2.com www.domain3.com domain3.com
If you've done that correctly, you should see the exact same content at each domain, without any redirection.
What you do next is an architectural decision. You can use mod_rewrite to hard-code URL routing rules based on domain, or you can use PHP to do the routing, based on the value of $_SERVER['HTTP_HOST'].
A mod_rewrite solution might involve adding something like this to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} domain2\.com$
RewriteRule ^(.*)$ /abc/$1 [L]
RewriteCond %{HTTP_HOST} domain3\.com$
RewriteRule ^(.*)$ /def/$1 [L]
This is a high-level overview. Please comment on my answer if you need details on a particular part of the process.

Related

Domain Masking and Path Forwarding

I need to direct my domain www.abc.com to www.xyz.com/folder/form/ and pass parameters to the form, but I need to mask the path so that when users go to www.xyz.com that's all they see along with any passed parameters in the url. Is the possible? How can I achieve this?
I do not own both domains. I own www.abc.com and the folder in www.xyz.com, but not the naked domain. The domains are on different servers.
I have tried the solution setting the DNS to *.abc.com and the CNAME to www and www.xyz.com. But, CNAME's do not allow pointing to a path. If I do a 301 or 302 forward the domain is not masked. What else can I do. I've been pouring over this for days.
Few ways to achieve url masking without using DNS setting.
On server level: Setting up reverse proxy.
If you are using apache, you can use the module named mod_proxy.
Once you have enabled the module, Add following to your .htaccess which is in your abc domain root.
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://www.example.com/folder/form/
ProxyPassReverse / http://www.example.com/folder/form/
Another way to achieve similar result would be, as follows which does pretty much same thing under the hood.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?abc\.com$ [NC]
RewriteRule ^ http://www.example.com/folder/form/%{REQUEST_URI} [L,P]
On language level: Using a scripting language like php and then pipe the data to and fro via. the second domain using curl. Kinda mix of 'man in the middle attack" and "phishing". But most importantly, it's pretty much possible.
Then we have other run of the mill solutions like using <iframe>. etc.

.htaccess Rewrite "external" subdomain to subfolder if match

Lets say i have wildcard subdomains on an external domain, can be anything like
demo.site1.com
blah.site1.com
and have directories like these on an external websites
external.com/websites/demo.site1.com
external.com/websites/blah.site1.com
and i want to make it so that all requests for lets say demo.site1.com should rewrite to
external.com/websites/demo.site1.com if the subdirectory matches demo.site1.com
NO REDIRECTION, I MUST KEEP THE URL THE SAME...
I have to do this for multiple subdomains with the same kind of subdirectories.
Is it possible to do?? if not is it possible without matching???
I read the apache .htaccess docs over and over for the past 12 hours and can't seem to
figure out a way how to do it.
Your help will mean alot to me...
You should configure Your domain in domain administration panel to point at this addresses.
If You don't have access to them use iframe, but as far I know .htaccess doesn't provide this kind of functionality.
try this. put it inside htaccess on site1.com document root. it work only if both domain has permission to access together
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.site1.com/(.*)$
RewriteRule ^(.*)$ external.com/websites/$1.site1.com/$2 [L]

htaccess allow only right domain name in ip dedicated website

i'm trying to allow only my domain name to view my website...
i have a dedicated ip and Anyone can basically set your domain to my ip, It creates duplicate content of my website.
Try this code, it will rewrite all domain names used to access your site to the correct one:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
This means that everything that do not match the www.domain.com will be redirected/rewrited to www.domain.com.
Am I understanding you correctly that you are worried that other people will point their domain to your IP, thereby lowering your SEO ratings? First of all: wow. Secondly: configure the web server with virtual hosts to respond to a particular domain name only, not to all requests regardless of HTTP Host header. How to do this exactly depends on what web server you're running exactly and whether you can configure virtual hosts on it.
As far as i know the host will only respond to the correct domainnames configured.
You could also make sure with htaccess that all incoming request get rewrited to the correct domain.
As in this example .domain.com is rewited to www.domain.com and will never be available the other way arround.
http://www.tech-recipes.com/rx/296/rewrite-domaincom-to-wwwdomaincom-using-htaccess-in-apache/

How to manage multiple cities in classified site?

I am working on classified site. I am using PHP and MySql for that.
On my site when user select their city that time i want to change website url like in subdomain style. foreg:-
http://cityname.mywebsitename.com
Only one thing in my mind to implement this is creating sub domains for all cities. But it would be more difficult to manage when your cities goes 50+. You can't upload again and again same script for multiple domains. I think this is not a good idea to do this. I want to use a single script for that so i can manage it in simple way.
If you have any idea than please share..
Thanks in advance
I say you how i made it:
in DNS add wildcard subdomain record
*.domain.tld. IN A 1.2.3.4
add wildcard virtualhost
<VirtualHost 1.2.3.4>
DocumentRoot /var/www/vhosts/wildcard
ServerAlias *.domain.tld.
ServerName domain.tld
...
</VirtualHost >
create a .htaccess that redirect all requests to a FrontController (usually index.php)
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
parse $_SERVER['HTTP_HOST'] from php to obtain city name and play with your new FrontController (usually index.php)
i've done it on dedicated server or VPS with linux and plesk. With some modifications on the previous basis points, it is possible everywhere;)
You can't upload again and again same script for multiple domains.
You wouldn't need to. A single web-server can support multiple domains, and files can be shared between them. See, for example, Apache's examples of setting up "virtual hosts".
Just setup a DNS wildcard so that they all go to the same site. Then using PHP you can figure out what subdomain is currently being used. Also just make sure that they all use a SESSION or COOKIE based on the domain not the subdomain if you want information to be accessible between them all.
You will also need to setup apache to accept the wildcard. So that all subdomains get directed to the same code.
$urlParts = explode('.', $_SERVER['HTTP_HOST']);
$subdomain = $urlParts[0];
Don't go the path of using Apache Virtual Hosts... While you could store scripts in a common folder and access them all via PHP (include('../common_scripts/file.php') you still have to manage the vhost configuration. Better to set up a DNS wildcard and then have a single site which takes into account the current URL when running queries.

htaccess "faking subdomains"

I've been looking at htaccess and how to use rewrite rules in order to make a sort of "fake subdomain" for users.
so far i have:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([a-z0-9-]+)/? http://$1.domain.com [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^([a-z]+)\.domain\.com$
RewriteRule ^$ index.php?user=%1
this will take any /somthing and make it a subdomain and then pass the subdomain value in as a param to pick up.
i want to be able to now use actual parameters, and pass them through normaly with the extra "user" param from the "subdomain"
e.g.
fred.domain.com/index.php?page=1&sort=up
would give me in $_GET
['user'] = 'fred'
['page']= 1
['sort'] = up
but for the life of me I cant figure out how to do this! as when i add any other params, I loose the user bit
Any help? =)
Also any helpful tutorials on htaccess would be nice! as all ones ive found haven't really explained what each bit does and why =\
Thanks in advance
.htaccess file has nothing to do here. It can't help you.
To direct a user to your server, you have to alter DNS record, not web-server config.
I am assuming that:
You own a domain, domain.com say, that is being hosted by some shared hosting service (SHS) provider.
You either have access to a control panel to set up subdomains, or your SHS provider has already set up a * A record mapping to its shared service.
Some SHS providers simply map *.domain.com to a fixed subdirectory, say /webroot/domain.com/public_html; others do this top level-redirection for their users and provide a control panel which allow account-holders to associate subdomains with specific subdirectories.
So you need to work out which applies in your case:
by looking at your SHS providers FAQs,
by trying http://sss.domain.com/ to see if it routes to your DOCROOT
by using a phpinfo() script to see what Rewrite environment variable it uses to point to your DOCROOT. (For technical reasons in Apache, see Dynamic mass virtual hosts with mod_rewrite, %{DOCUMENT_ROOT} cannot be properly initialised for each user, so providers typically use a RewriteMap to initialise a stand-in; mine uses %{ENV:DOCUMENT_ROOT_REAL})
So let's assume that you want to set up a blog at say http://blog.domain.com/, you may need to:
Issue permanent redirects [R=301] from legacy references to http://domain.com/blog/* to http://blog.domain.com/*
Issue internal redirects from http://blog.domain.com/* to DOCROOT/blogdir/*
Add the necessary conditional interlocks to prevent infinite redirection loops, where this second internal redirect is then treated as a legacy reference.
I could give you a set of rewrite rules to do this, but given that I've answered a dozen flavours of this same Q this months, you can find lots of templates by searching or by looking at my blog (Webfusion, .htaccess) where I've written a number of articles giving more explanation.
If you want a single application to catch some wildcard subset of domain, as long as you can encode this in a regexp then you can do something like:
RewriteCond %{HTTP_HOST} (sompattern)\.domain\.com
RewriteRule ^(?!appdir/).* appdir/catchall.php?arg=$0&subdomain=%1 [L,QSA]
The (?!appdir/) bit is called a lookahead negative assertion and this stops the rule refiring on itself.
I'm pretty sure you can set vhost *.yourdomain.com to point to a single path and then use mod_rewrite for the url parsing.

Categories