htaccess allow only right domain name in ip dedicated website - php

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/

Related

Is it possible to point a full URL to another server?

Bit of a weird one.
One of my clients has a shared hosting server which he hosts his website on lets say:
http://www.example.com
To develop a web application for him I have had to use a different server lets say 255.255.255.255 which allows SSH access.
My client wants http://www.example.com/portal to essentially "point" or display the portal & web pages etc from server 255.255.255.255.
I know you can point domains but I have no idea where to start on something like this, any help or suggestions would be appreciated
J x
Better: place it on a subdomain: http://portal.example.com
Worse: proxy all request from http://www.example.com/portal to your site. But you need access to web server config - unlikely shared hosting will allow you to do it.
Or you need your own hosting like VPS and so on.
A plain 301 redirect comes to mind:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} www.example.com/portal$ [NC]
RewriteRule ^(.*)$ http://255.255.255.255/$1 [L,R=301]
This should obviously go into the .htaccess on www.example.com
Add this to .htaccess file placed in the root of the www.example.com site:
RewriteEngine On
RewriteRule ^portal/?(.*)$ http://255.255.255.255/$1 [P]

Serving Static Content from a Cookieless Domain - using subdomain and .htaccess

I can have many images per page (e.g. 70) and want to serve them without the overhead of cookies.
I would prefer to set up a subdomain and add a CNAME record (as opposed to having to use a separate domain). Currently, I have a CNAME record for my donmain: www.example.com ==> example.com and I want to keep the functionality of resolving example.com to www.example.com.
I've discovered that when I access my site using www.example.com it uses different cookies from when I use example.com. Could I use .htaccess to redirect all example.com requests to www.example.com requests and solve the problem this way? Would all cookies then default to www.example.com when set?
UPDATE
Then I would create the CNAME record static.example.com and point it to www.example.com. I would then point all my image requests to static.example.com. Would they be cookieless?
Yes that's what is needed and good for SEO too. Multiple url for same content is not a good thing. You need to make changes in htaccess file written below.
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
You're on the right track, but it's not immediately guaranteed that static.example.com will be cookieless. You'll need to explicitly set all your cookies for www.example.com so that they won't be shared among subdomains. A separate domain is much easier to deal with in this regard.

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!

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