Using .htaccess to set a cookie and 301 redirect
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, redirects, 301-redirect, mod-rewrite, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :I currently have in my .htaccess:
RewriteRule ^$ http://chfmaine.com/ [CO=moved:yes:chfmaine.com:1/,R=301,NE,NC,L]
I was basing this off of this post.
That's a lot more than I need but the setting of the cookie should be the same. Maybe I am misunderstanding the domain part of the cookie. If I set it to CHFmaine.com shouldn’t that be where it works?
I tried adding the cookie after the 301. Ether way the 301 works but no cookie is passed. This is redirecting from maine-horse-trails.com to chfmaine.com. I have 23 of these for all the different pages that were on the old site. Anything landing at this .htaccess will be redirected and the cookie should be set. So if there is a way to first pass the cookie to anyone then proceed onto the matching rewrite that would be helpful. But if I can get this way to work I dont mind using copy and paste to get it on all the possibilities. and I do not want to pass any queries in the URL.
My question is, what am I doing wrong here? Why can I not find this cookie after a successful redirect? And is there a better option?
You can't find the cookie after a redirect because the cookie is never set. The redierect happens in Apache, before any PHP or other code is run.
You could use a PHP redirect. For example, land on the page, set the cookie, then redirect.
See this question for more information. https://stackoverflow.com/questions/612034/how-can-i-set-a-cookie-and-then-redirect-in-php
Also, cookies are domain specific. If you set a cookie on one site, it doesn't carry over to the next. Depending on what you're using the cookie for, a $_GET may be a better option.
Comments
Post a Comment