How to manage multiple cities in classified site? - php

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.

Related

redirect to different server based on url path

I've made a new website for a client who has an intranet integrated into their old website.
The new website is currently on a different server, but when the domain A records point to the new server, the old site (and intranet) will obviously not be accessible, but I need to keep their intranet active. The path to their intranet is: abc.com/intranet
Is there a way to have URL path direct to the old server? For example:
abc.com - new website loads on new server
abc.com/intranet - old website loads on older server
If it's not possible, I suppose I'm looking at creating a sub-domain on abc.com for the intranet. Any thoughts are appreciated.
You need to use an Apache RewriteRule using mod_rewrite. This can be placed in an .htaccess file on your server’s root or it can be placed directly into your Apache config file.
If you want to redirect example.com to example.com/intranet, then this is the Apache RewriteRule that should work for your needs:
RewriteEngine on
RewriteRule ^(.*)$ /intranet [L,R=301]
This will grab any URL on the site this RewriteRule is placed on & redirect them to /intranet. That /intranet can also be a full URL such as this example below:
RewriteEngine on
RewriteRule ^(.*)$ http://example.com/intranet [L,R=301]
EDIT: Upon rereading your question, I am not 100% sure the answer above works for you as-is. So I think if you are describing how to point one URL path from one server to another, you would do this. This gets placed on the new server:
RewriteEngine on
RewriteRule ^/intranet(.*)$ http://old_example.com/intranet [L,R=301]
That would grab any URL coming from new_example.com/intranet and redirect it to old_example.com/intranet.
ANOTHER EDIT: Since the original poster indicates the server will have the IP fully changed, then a subdomain for the old server is the best way to go. You can’t redirect content on one domain the way you describe if you switch the domains fully to different IP. Both servers need to be active with an active—but different—domain name for what you want to happen.
abc.com/intranet is a path in the virtual file system exposed by your web server, so is not possible to serve the content from different web server. You have 2 options here.
Put a reverse proxy in front of both servers and get the content from server A or B based on the original client request.
As you said, create a subdomain and also redirect /intranet to the new subdomain.
Hope this help!

How to create dynamic subdomain using PHP and htaccess?

I have one problem:
I want to set up PHP and htaccess to make dynamic subdomains. And I can not figure out how.
Currently my URL looks like this:
www.exemple.com/index.php?subdomain=mike&component=content&id=26&title=people-love-apps
I need it to look like this:
www.mike.exemple.com/content/26/people-love-apps.html
I know it can be done but I do not know a solution how to do it.
Is very important to me with $ _GET [] functions to read parameter in the URL.
The problem is that users on my site make their web site and get a free domain (sub-domain) automatically. This should be automatic.
Thanks!
You can't make dynamic subdomains with .htaccess
You will need to configure the apache virtual host to accept requests for multiple domains
See the Apache documentation - Using Name Based Virtual Hosts
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com *.example.com
DocumentRoot /www/domain
</VirtualHost>
Adding the wildcard subdomain *.example.com, your PHP application will receive all requests for any domain below example.com, ie garbage.example.com, busted.example.com, llama.example.com, etc.
Creating Wildcard Sub Domain Using Apache VirtualHost
Multiple Domains to One Virtual Host | Wildcard Host (shared hosting)?
Apache default virtual host for multiple domains
At this point, your application will have to determine the validity of the subdomain and display the appropriate error for unknown subs.
From there, parse the domain for mike.
OF COURSE you can interprete dynamic subdomains with htaccess.
The following rule will solve the problem.
RewriteCond %{HTTP_HOST} www\.([^.]+)\.example\.com [NC]
RewriteRule ^/content/([0-9]+)/([a-zA-Z0-9_\-])\.html /index.php?subdomain=%1&component=content&id=$1&title=$2 [L]
so anyone visting
www.mike.exemple.com/content/26/people-love-apps.html
will see the content of
www.exemple.com/index.php?subdomain=mike&component=content&id=26&title=people-love-apps

Set some folder on Apache server as a defaul folder

I'm developing some web based application based on PHP.
I have some folder structure that will be located inside the public html file.
I'd like to make it work so that when a user types for ex. http://mysite.com/ he/she gets into http://mysite.com/public but I don't want the user to know that he/she is inside public, the user should think that his directly inside public_html folder.
Any hints?
P.S. I'm doing it on hosted server, so I have access with only Cpanel, I'm not the admin of the server.
You either need to use mod_alias or mod_rewrite for this. How much of cPanel is available to you? How much does you host let you do?
I'll just have to have a look through my WHM server to work out how to do Aliases, but you can do rewrites with a .htaccess file. I would recommend Aliases over rewrites thought, as they are less complicated and less resource-hungry.
EDIT
Just been into my root login for our WHM/cPanel based server, and I can't find any way to use mod_alias - I think this is probably because it would require an Apache restart. You will have to use mod_rewrite.
Put this in a .htaccess file in public_html:
RewriteEngine on
RewriteRule (.*) public/$1 [L]
You can set up an addon domain and point it to /public_html/public directory.
Edit:
Parked domain should work too.

Multiple domains for site subsections

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.

How to build a subdomain from script

In my project I have to make a subdomain, i.e
if the user name is XXX when he register, a sub domain will be created like XXX.example.com
how to do it?
I will use php for scripting.
I found a script that seems to do exactly that, create a subdomain on your server on demand.
It probably needs a little bit of tweaking for it to work on your particular control panel, but the review are quite positive as far as I can tell.
Link
Have you considered using htaccess and url rewriting?
Found this code that may help you:
# Rewrite <subdomain>.example.com/<path> to example.com/<subdomain>/<path>
#
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.example\.com(:80)?<>/([^/]*) [NC]
# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
# Rewrite to /subdomain/path
RewriteRule ^(.*) /%1/$1 [L]
Source (Post #6)
This might be a little more complex than you think.
I suggest to do some reading on mod rewriting and htaccess.
You could start here:
htaccess Tutorial
Modrewrite tutorial
Subdomain Modrewrite Example
EDIT: Or just go with one of the nice examples provided my fellow SO users. ;)
As long as this is for non-SSL sites, then by far the easiest way is not to bother - just use a wildcard DNS domain and vhost, then map any domain specific behaviours in your PHP code. If you need SSL sites then its a lot more complicated - you need to have a seperate IP address/port for each certificate - and woldcard certs can be very expensive.
If you're wanting to set up some sort of hosting package then its a bit more involved - how you go about this depends on what webserver and DNS server you are using.
Assuming (again no SSL) with Apache on Unix/POSIX/Linux and bind, then, again I'd go with a wildcard DNS entry, then:
1) create a base dir for the website, optionally populate this with a default set of files
2) add a vhost definition in its own file in /etc/httpd/conf.d named as XXX.conf
3) send a kill -HUP to the HTTPD process (causes it to read the new config files without having to do a full restart).
One thing to note is that you really shouldn't allow the httpd process direct write access to its own config files - you definitely don't want to give it root privileges. A safer solution would be to create a CLI script to perform this using the username as an argument then make it setuid and invoke it from the script run by the HTTPD process.
C.
the best way is to use a joker in your DNS server :
www.example.com. IN A 1.2.3.4
*.example.com. IN A 1.2.3.4
By this way, No subdomain has to be created : all are pointing to the same IP by default.
In your PHP code, you just have get $_SERVER["HOST"] and get the fist part :
$hostParts=explode('.',$_SERVER["HTTP_HOST"]);
$user=$hostParts[0]
First, you need to make sure you have a wildcard domain setup in DNS, and make sure your webserver (apache?) directs all queries for that wildcard domain to your php file.
Then in php you can look at $_SERVER["HTTP_HOST"] to see which subdomain is used for that particular request.
Since you will make sub-domains when an user registers.
Try this as .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^.htaccess$ - [f]
RewriteCond %{HTTP_HOST}!^www.domain.com
RewriteCond %{HTTP_HOST} ^([^.]+).domain.com
RewriteRule ^$(.*) /$1/%1 [L]
make a function of a controller which will take the value of sub-domain and display what necessary.
like::
public function show ($domain)
{
**.**..*..**.** Your code goes here
}
when a user will try this xxx.domain.com/controller/show this will be domain.com/controller/show/xxx . if you want to xxx.domain.com to be domain.com/controller/show/xxx just edit the htaccess file as you want.

Categories