Why is "www.example.com" not the same as "example.com"? - php

I have a website. If I login in the domain of this format http://example.com and then change my address to http://www.example.com, i find my account is not logged in. If I change the address to http://example.com, I find my account is logged in.
I contacted my host, they told me its a programming issue.
How can i solve this issue so both addresses represent same access/session/cookies?
I'm using PHP & MySQL

www.example.com and example.com are two different domains as far as the browser is concerned, apparently, even though they both direct to the same site. Same would happen if you parked a different domain there, say, example.net.
In order to solve the issue, it is rather common to rewrite the URL via .htaccess. Decide upon which domain name you prefer to use and add something like this to your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
or
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301]
(the first one removes, the second one adds the www)

How can i solve this issue so both addresses represent same
access/session/cookies?
You have to set the domain path of your cookie like this to make it available on all subdomains: (www is a subdomain):
.domain.com

It is not same..
usually you can go to www.example.com just with writing example.com to your browser, but your browser added www to your url..
so basicly it is not same

Related

.htaccess 2 domains, same target, 1 redirect

I own 2 domains that are pointing to the same static ip. I wanted to set domain1.com to be redirected to domain1.com/down.php but only for the target domain1.com and domain.com/index.php/html.
The domain2.com should work as usual, so no redirects here. It should target /var/www/html like it currently does.
Background is: that there was a project which should not be available on the domain1.com, but there are specific files such as domain1.com/subdir/subdir/file1.jpg, which should still be accessible there.
Any ideas?
Many Thanks
.htaccess to redirect domain1.com/*
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ down.php?_url=/$1 [QSA,L]
The rule only works if the condition is meet. ?_url=/$1 is not needed but may prove useful.
If you use htAccess, also it seems you use Apache2
You can also use virtual host directly from apache 2 configuration :
https://httpd.apache.org/docs/current/vhosts/examples.html

IP Address Redirect to URL when visited in browser

When a user visits my sites IP address in a web browser, I'd like to have it redirect to the site's main page (or redirect to a page I choose). I'm not sure if this is possible but we had basically set up a site on a temp URL (it was http://{IP_ADDRESS}/{SUBDIRECTORY}) and when it was ready we pointed the domain to the folder.
It works great but it was set in WordPress and some of the links were done using absolute paths so when people click on them, they go to the IP address and thus have a 404 error. We are obviously changing those links but the client wants any other ones to redirect to the homepage to be safe.
The server is a dedicated IP and I tried this in the .htaccess file (from: Redirect IP address to domain name):
RewriteCond %{HTTP_HOST} ^121\.12\.12\.123
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
I put in my credentials as well. It did not work.
Any ideas how I can accomplish this?
Try with this :
RewriteCond %{HTTP_HOST} ^121\.12\.12\.123$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Ensure mod_rewrite is on.

how to create subdomain by .htaccess on rewrite directory

I have created a subdomain with this .htaccess; it is working fine on my www.example.com domain but not on www.example.pk. I do not understand where the problem is.
www.example.com
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([-a-zA-Z0-9]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?index\.php$
RewriteRule ^(.*)/*([0-9]+)*$ index.php?r=list&source=product&product_get=%1&page_page=$1 [NC,QSA,L]
www.example.pk
RewriteCond %{HTTP_HOST} !^www\.example\.pk$ [NC]
RewriteCond %{HTTP_HOST} ^([-a-zA-Z0-9]+)\.example\.pk$ [NC]
RewriteCond %{REQUEST_URI} !^/?index\.php$
RewriteRule ^(.*)/*([0-9]+)*$ index.php?r=list&source=product&product_get=%1&page_page=$1 [NC,QSA,L]
I created www.example.com domain then the subdomain with cpanel *.example.com. Is it necessary or not?
When you work with subdomains, you need to configure your DNS correctly to direct subdomains to your server. The subdomain-feature of cpanel might have done this for you, or it already contained a wildcard cname dns record. DNS records are usually cached, but I am unsure if this is the case for non-existing records too.
Does the subdomain already resolve to my server?
First check if your subdomain already resolves to an ip. Open a terminal and type ping yoursubdomain.example.com (obviously you need to replace this with your actual domain). If it can't be resolved to an ip, then there is probably a problem with your DNS record. You can do that online if you want here or on several other, similar, sites.
Editing your dns
If your host gives you an opportunity to edit the DNS records manually, you'll need to know the following:
There is 1 A-record in your dns. This is how the name is translated to an ip.
Any subdomains are linked to that record using CNAME-records.
You can either add a wildcard-subdomain (which will translate any (un)thinkable subdomain to your site) or a specific subdomain to your dns. You do so by adding
CNAME (from) *.example.com (to) example.com
The TTL (time to live) is how long a record can be cached by others. It will, at most, take TTL to get everyone in the world to 'notice' changes you make in a record of your dns. This site gives some more information about that.
.htaccess
I am not sure what kind of urls you want to rewrite, but there are a couple of things you can change:
%{REQUEST_URI} always starts with a /. There is no need to add a ? behind it in your condition.
For readability, if you use %1-type-of-backreferences, put the corresponding condition directly above it, even if it turns out they are not overwritten by other conditions without capture groups.
^(.*)/*([0-9]+)*$ doesn't make much sense to me. It matches every url, because everything after (.*) is optional, and can be there an infinite amount of times. I doubt this is what you wanted it to be.
RewriteCond %{HTTP_HOST} !^www\.example\.pk$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{HTTP_HOST} ^([-a-zA-Z0-9]+)\.example\.pk$ [NC]
RewriteRule ^(.*)/*([0-9]+)*$ index.php?r=list&source=product&product_get=%1&page_page=$1 [NC,QSA,L]

Mod rewrite base URL

I have several domains that I want to all rewrite to one domain. I don't want it to redirect because I want the URL to look like what the user has entered in. For example if they enter www.example.com I want it to load the page from www.sample.com/default.php?from=example
I have worked a little with rewriting if you have www.site.com/var1/var2 making it load www.site.com/index.php?one=var1&two=var2
Is it possible to do what I am looking for just through the .htaccess file? I tried looking around and couldn't exactly find what I was looking for
Thanks
If the sites are hosted on different servers or don't share a common document root, then you'll have to rely on mod_proxy and you can use the P rewrite rule flag. For example, these rules in an htaccess file in www.example.com's document root:
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteRule ^/?$ http://www.sample.com/default.php?from=%1 [L,P]
Will take the request http://www.example.com/ and invisibly proxy it to http://www.sample.com/default.php?from=example. The browser's URL address bar will remain http://www.example.com/.
Note that the rule only matches against the request URI /. If you want to do more, you'd have to create the correct regular expression and grouping.
If you have redirects on the sample.com site, you'll need to employ ProxyPassReverse to rewrite the redirects. Also see ProxyPassReverseCookieDomain and ProxyPassReverseCookiePath if there are cookies involved.
If you can do this in vhost or server config instead, then consider simply using ProxyPass instead of mod_rewrite. The ProxyPass directive won't work inside htaccess files.
EDIT:
Seeing as how everything is in the same document root, you won't need to proxy anything. Simply:
RewriteCond %{HTTP_HOST} !^www\.sample\.com$ [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteRule ^/?$ /default.php?from=%1 [L]
I use online generators.
Use this generator: mod-rewrite

Www and non www sites

I have a domain say http://www.testexample.com. When I login to http://www.testexample.com and come back to http://testexample.com in browser; the logged in user information is not displayed.
I know that the both of the above are treated differently and hence it is not retaining the session for http://www.testexample.com while accessing http://testexample.com.
Please let me know if cakephp has a way to do a match on the TLD. So whenever I type http://testexample.com it should take session for http://www.testexample.com
I am using the following code to redirect from one URL to the other
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ps6309 [NC]
RewriteRule ps6309.domain.co.in [L,R=301]
this is on my local test machine. This works sometimes and sometimes doesn't.
Also I have added the rewritelog directive to my httpd.conf file.
But the log file is not getting updated.
Please let me know if anyone has any pointers to this.
Use .htaccess to redirect all http://domain.com -> http://www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
Set the domain for the cookie as testexample.com, then it can be shared across sub domains as well as not worrying about www.
If you have many projects and don't want to hard code your domain name on .htaccess again and over again, try this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
this will redirect non-www to www. While you're using cakephp, then put it on your .htaccess which is located at /webroot
Try ini_set('session.cookie_domain', $domain); (documented as ini_set session.cookie_domain and session_set_cookie_params()), where $domain is your domain name prefixed by a .. So, using the domain example.com (per rfc 2606), you'd use:
ini_set('session.cookie_domain', '.example.com');
Please note that this is not a CakePHP specific solution - looking at the code for CakeSession, session.cookie_domain is never set, meaning that it falls to it's default value. Stuffing that line in your app/config/bootstrap.php or app/config/core.php should do it for you.

Categories