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

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.

Related

URL Rewrite, Virtual Subdomains

Been struggling with this problem for a while now.
We have a basic online app which you can access at say
app.domain.co/signup?user=onlinestore
onlinestore is the username of the account.
We want each user to get their own subdomain that they can use the app from.
In this case the subdomain would be onlinestore.domain.co/signup and it would show them the contents as if they were here: app.domain.co/signup?user=onlinestore.
Now as well as that subdomain mirroring the login pages, when they login I would like for all of the pages inside of the app to be able to use the subdomain as well. Ideally I don't want the user to see the app.domain.co domain at all, just their personalised domain. Theres quite a number of pages inside the app so im not sure if theres a simple way for them all to be accessible from any subdomain (if logged in ofc).
Examples:
onlinestore.domain.co will become app.domain.co/signup?user=onlinestore
onlinestore.domain.co/payments will become app.domain.co/payments?user=onlinestore
onlinestore.domain.co/contact will become app.domain.co/contact?user=onlinestore
Current HTACCESS File (in the wildcard subdomain)
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.co$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+).domain.co
RewriteRule ^$ https://app.domain.co/signup/?user=%1 [L]
We've created a wildcard subdomain but it's not working with https, and it's doing a full redirection as opposed to a rewrite, I'm still on shared hosting so I'm not sure if I might have some restraints due to this.
Is there a simple way to do this? I don’t want the users to see the app.domain path at all, just their custom subdomain path.

Point subdomain to root without using a subdirectory

I have the following scenario:
blog.site.com points to server1
site.com points to server2.
I want blog.site.com to become site.com/blog. So I thought I'll point blog.site.com to server2 and put in a subfolder blog where I can place an .htaccess file doing the 301 redirect. However by creating a blog subdirectory it overrides the /blog/title-goes-here with a blank directory.
My problem is server1 is being decommissioned and so it won't be available to be used for the 301 redirect.
Is there a way I can point blog.site.com to server2to become site.com/blog/title-goes-here without using a blog subdirectory? Or using a blog subdirectory but not interfering with the url?
Please note. This is a wordpress website which the posts from serve1 have been imported. It's important to keep the blog as a part of the site.com website.
Unless I'm missing something, you don't need a redirect - you need rewrite rules.
Have your DNS entries for blog.site.com and site.com point to server2. Then in the .htaccess on server2 add the corresponding rules:
RewriteCond %{HTTP_HOST} ^blog\.site\.com$
RewriteRule (.*) /blog/$1 [L]
Now whether you access site.com or blog.site.com, you'll end up accessing the same server. The rewrite rule will then ensure that requests of type blog.site.com/something actually call site.com/blog/something.
Note that I haven't tested this though.

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!

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/

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.

Categories