Multiple domains pointing to the same folder - php

I am currently running two websites. I am able to add my domains and set the root folder to / instead of /domain1.com and /domain2.com. That way both websites go to the same folder, however they both maintain their domain names (no redirects). My code determines whether the user is from domain1.com or domain2.com and displays the appropriate content using PHP.
Now, I have switched to another web hoster. The problem is that they don't allow you to specify where the root folder is: so it has to be /domain1.com and /domain2.com. They also don't seem to allow access to httpd.conf to edit VirtualHosts.
I have tried using .htaccess to do a redirect, but the problem is that when I go to domain2.com, it redirects straight to domain1.com, and it doesn't keep its host name of domain2.com.
I have also tried setting up symlinks, but it seems to be doing the exact same thing.
Is there any way to solve this?

Can you do a rewrite rule similar to the following (don't trust my syntax)?
# if domain2.com, send all requests to domain1.com
RewriteEngine on
RewriteRule ^(.*)$ ../domain1.com/index.php/$1
So if someone does go to domain2.com, all requests are passed through the index.php file on domain1.com for processing (I presume you're doing something similar already).

You should be able to set up symbolic links using ssh that don't redirect to the other domain. I have several .co.uk domains that use the same data as the .com that don't redirect. What host is it? You should be able to do this, maybe email them asking why it redirects.
Sure it's not in the htaccess file to redirect the domain2.com to domain1.com?

Related

How to make .htaccess rewrite for language subdomain versions?

my project requires language versions and for SEO purposes I want not to create $_SESSION for language but to use subdomain name for the language version. Let say my russian version of website looks like:
ru.example.com
I want when user visits http://ru.example.com .htaccess file to load index file which is located in the root of the website..
But this is not all ! For example if user visits: http://ru.example.com/products/ .htaccess file to load index file which is located in http://example.com/products/
Is it possible this to be done somehow? Basicaly I wish subdomain name ru to be ignored somehow when server loads the file in url..
So far I have this:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ru\.example\.com$
and I dont know how to complete the rest..
Please help if this can be done
The way to do this is simply to have all your language subdomains served by the same website. No mod_rewrite required. You can check HTTP_HOST in your php to see which is being accessed. Put them all in ServerAlias or just use a generic ServerAlias *.example.com.
Make sure to also set UseCanonicalName off so any self-referential URLs that Apache generates will use whatever host was visited and not drop your language subdomain. Reference here.
If you can't do that, let us know your hosting setup, and as long as it's on the same server it can probably be done with rewrites.

Redirect and URL Mask all non-existing/existing subdomains to homepage

A website example.com has one subdomain a.example.com. The point here is I want to redirect all existing and non existing subdomain (ex- b.example.com) to example.com without changing URL(URL MASK). To explain it more further, when a user enters b.example.com he must see example.com on his screen but URL must not change from b.example.com --> example.com. I think its possible from .htaccess file but I failed to achieve it.
Do I need to configure virtual host. Since I only have access to .htacces, I wish I can get it done
You need to have two things configured correctly:
A DNS entry with wildcard (*.example.com -> your server IP)
A virtual host with wildcard alias (ServerAlias *.example.com)
Then there is nothing to do in .htaccess. .htaccess alone can't do what you want. And please note also that this could be SEO-toxic (duplicate content), but it depends on your use case.
As you do not have access to virutal hosts config, may be you could go with subfolders instead of subdomains.

relative links work when accessing folder but not domain that references folder

I have a domain example.com that has a folder in it example.com/folder/ that has another domain that points directly to it so example2.com points to example.com/folder/
When I access this via example.com/folder/ everything works fine and I can access my css and js folders which are a level below the folder in example.com/css/ and example.com/js/
When accessing from example2.com I don't see the css or js on the site and I get a 404 when trying to link directly to them by clicking the link in the view source output in firefox.
I should also mention that I have no problem with PHP includes that come from below the folder example2.com points to, just with the relative URLs referenced in the HTML.
Any idea how to get the site to let me access these folders below the root of example2.com from both example1.com/folder/ and example2.com?
I'm using a LAMP system. Also, my goal is to keep the system flexible enough that I can use it in multiple sets of domain/subdomain without having to edit server files or PHP.
EDIT - I "solved" this by adding a field in my db for the root domain all the subdomains will stem from and making static URLs for the linked js and css. This doesn't seem like the most elegant way to deal with it but it's the best I can think up right now. If anyone has any better let me know!
Just a guess, but sounds like <base href=> is needed.
In your case:
<base href="http://example1.com/folder/_">
That will make all relative URLs request resources from example1, even if users come visit on your example2.com vhost. Though all links will then also redirect people there. It affects both JS/IMGs and clicky things.
From what you have described you folder organisation looks like this:
/path/to/domain/folder/
/css/ ^
^ |
| domain2.com
domain.com
PHP on domain2.com won't have a problem accessing ../css because it operates on the server, but a browser can't go further back then /path/to/domain/folder/.
I would suggest to create a symbolic link (if possible) on the server:
ln -s /path/to/domain/css /path/to/domain/folder/css
Alternatively, you could use Alias in Apache to set up aliases for domain2.com/css to /path/to/domain/css/
Lastly, a <base> tag could help you (as mentioned by mario), but any links followed from a page with such a tag will switch back to `domain1``.
In order to keep the system as flexible as I wanted it I decided to put both the example1 url into the database and the folder name so I can construct absolute links to the files I need to access in the HTML page. This also seems necessary with my mod_rewrite, which changes tries to tack the locations of these files to the end of the "pretty URL" (like example2/post/2/css/main.css). Thanks for your ideas you steered me in the right direction!

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 Redirect images folder to another domain

ok so i have 2 domains
site1.com/images/ (all images)
and I need to redirect the imgages link site1.com/images/ to site2.com/images/ so that when
and when exp. site1.com/images/i.jpg is called it will look at site2.com/images/1.jpg and will find it.
.this is basic duplicate of the site and i don't want to move images back and forward.
Put in your .htaccess on site1.com
Redirect permanent /images http://sites2.com/images
If the websites are on the same server and running under the same user, you could use symbolic links between both websites.
E.g., create a symlink to /home/site2/public_html/images in /home/site1/public_html/images. Another option is using the Apache's Alias directive:
Alias /images /home/site2/public_html/images
Put this in the vhost config of site1.
Using redirection on Apache:
RedirectPermanent /images http://site2.example.com/images
The best way would probably fixing the HTML code pointing to one domain. If you cannot decide which domain should get the static content, create a subdomain (or even a different domain) to store the files.

Categories