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.
Related
I'm trying to get a rewrite working and need a hand...
I've got a set of websites, which domains are like this
example.com
example.co.uk
example.ie
example.com/gm
The way the site is setup means that all domains can display the .com/gm content (GM doesn't have it's own domain and we want it only served from the .com domain)
So I'm trying to redirect anyone using incorrect URLs to the correct TLD with the gm subfolder. E.G:
going to www.example.co.uk/gm would 301 to www.example.com/gm
I've currently got this:
RewriteCond %!{HTTP_HOST}:{REQUEST_URI} !^example.com:gm$ [L,R]
RewriteRule ^(.*)$ https://www.example.com/gm$1 [R=301,L]
Which obviously isn't happy
Appreciate a point in the right direction, basically from what I can tell I need a rule that checks the domain doesn't equal .com if they're trying to get to /gm/ and redirects them to .com/gm/
Thanks!!
It is easier to check if the /gm/ path was requested in the RewriteRule, and prefix that with a RewriteCond that checks for the host name – something like this should do it:
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(gm/.*)$ https://www.example.com/$1 [R=301,L]
Your htaccess is full of syntax errors.
Try the following:
RewriteCond %{HTTP_HOST} ^(www\.)?example.co.uk$
RewriteCond %{REQUEST_URI} ^/gm/
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
This will redirect
example.co.uk/gm
to
example.com/gm
Clear your browser's cache before testing this.
Summary
I have bought two domains, both 'example.com.tw' and 'example.com'. I hope that when a user input 'example.com.tw' in the address bar, it can redirect to 'example.com' automatically. Below is my try.
My try
RewriteCond %{HTTP_HOST} ^www\.example\.com\.tw [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L,NE]
Questions
Is it correct above?
Is there any better way to improve the code above?
The issue is that when a user input the subdomain (Such as: 'news.example.com.tw'), will it redirect correctly? (Will it redirect to 'news.example.com' successfully?)
To remove the .tw TLD:
Assuming both domains point to the same place and any subdomains are simply in subdirectories off the main domains document root (or point to the document root of the main domain itself). Then, in your root .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.tw$
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]
Replace 302 (temporary) with 301 (permanent) when you are sure it's working OK.
I am redirecting users from wildcard sub-domains to a specific sub directory using mod rewrite rule while not changing address bar url. I have done it success fully but I also need the wildcard sub-domain name to use internally.
For example if someone try xyz.domain.com I am redirecting him to say domain.com/abc and I also need that xyz for internal use.
I tried $_SERVER['SERVER_NAME'] but it returns current sub directory address. Also tried $_SERVER['HTTP_REFERER'] but with no success. So if anyone can help please.
My .htaccess code is given below:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^(.*)$ "http\:\/\/domain\.com\/project\/" [P,NC,QSA]
My previous answer doesn't work with dynamic subdomains. So in your case it wont work.
When I tested your .htaccess rules, my server sets a $_SERVER['HTTP_X_FORWARDED_HOST'] variable with a value of original host "xyz.domain.com".
Maybe this could help.
Answer for static spesific subdomains;
put these lines to .htaccess before your redirect condition
RewriteCond %{HTTP_HOST} ^xyz.domain.com(.*)$ [NC]
RewriteRule .* - [E=internalSubdomain:xyz]
then you can use $_SERVER['internalSubdomain'] and $_SERVER['REDIRECT_internalSubdomain'] in your php code
I have a merchant website linked to a bank.
I have defined this success URL in bank :
http://www.mydomain.com/deposit.php
When the user starts payment process he enters to bank website and after payment he redirects to the success URL in my website.
If a user enters my website with this URL (without www) :
http://mydomain.com/
The session expires and he should login again. but when he enters with this URL :
http://www.mydomain.com/
The session will not expire and everything is okey. how can i solve this problem?
The thing is your session cookie is probably specifically tied to www.mydomain.com, so, when you access mydomain.com, the browsers chooses not to send the session cookie.
Take a look at ini_set function, and the session.cookie_domain value.
if (count(explode('.', $domain)) > 2) {
ini_set('session.cookie_domain', $domain);
}
You can force www. (or force using just http://mydomain.com aswell) using the .htaccess file.
RewriteCond %{HTTP_HOST} !^www.mydomain\.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R]
The ! in the RewriteCondition tells the server that when the address does NOT equal the following, redirect it to the RewriteRule.
You can find alot more Rewrite tips 'n tricks using .htaccess on the web.
Note: For this to work, you will need to enable this first in your .htaccess file:
RewriteEngine On
And make sure your Apache (or other host) configuration supports mod_rewrite.
Taken from the HTML5 Boilerplate .htaccess:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
Placing that in your .HTACCESS file should fix the problem. However, someone else will have to elaborate on what exactly is the cause of this problem.
Edit: Seems to me like Pablo Santa Cruz is right.
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