.htaccess Compare Cookie with data from file/link - .htaccess

From this answer, I know I can check if a cookie has a value with
RewriteEngine On
RewriteCond %{HTTP_COOKIE} !cookie_name=specific_value; [NC]
But I would like specific_value to be an list of values pulled from a file/link. Heres what I mean in a mix of .htacess and PHP
RewriteEngine On
RewriteCond %{HTTP_COOKIE} !cookie_name=file_get_contents("mycookielist.txt"); [NC]
RewriteEngine On
RewriteCond %{HTTP_COOKIE} !cookie_name=file_get_contents("http://example.com/mycookielist.txt"; [NC]

Related

301 htaccess redirects with 3 parameters to static urls

Im trying to redirect several urls with 3 parameters to different static urls with .htaccess but nothing working.
1.
http://olddomain.com/index.php?id_category=28&controller=category&id_lang=2
to
https://newdomain.com/page1/
http://olddomain.com/index.php?id_category=30&controller=category&id_lang=2
to
https://newdomain.com/page2/
http://olddomain.com/index.php
to
https://newdomain.com
I tried the below code but http://olddomain.com/index.php not going to https://newdomain.com :
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC]
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page2/? [R=301,L]
You need to have specific longer matches first and then have rules to remove index.php or domain redirect:
RewriteEngine On
# specific redirects with index.php as optional match
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page1/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page2/? [R=301,L,NC]
# remove index.php and redirect to newdmain
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule ^(?:index\.php/?)?(.*)$ https://newdomain.com/$1 [L,R=301,NC,NE]
Make sure to clear your browser cache before testing this change.
In case you are taking page's id from id_lang= variable then please try following rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules to redirect to link: https://newdomain.com/page1/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\?id_category=28&controller=category&id_lang=(\d+)\s [NC]
RewriteRule ^ https://newdomain.com/page%1/? [NE,R=301,L]
##Rules to redirect https://newdomain.com/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\s [NC]
RewriteRule ^ https://newdomain.com [NE,R=301,L]

How to check multiple cookies are set in .htaccess?

I am checking a single cookie named "user" in my .htaccess file. If the cookie is set then redirect to index.html page while if cookie is not set then redirect to index.php page. I am not sure whether it is a proper approach of checking cookie existence but here's my .htaccess rules:
RewriteEngine On
RewriteCond %{HTTP_COOKIE} user;? [NC]
RewriteRule ^(.+)$ index.html?url=$1 [QSA,L]
RewriteCond %{HTTP_COOKIE} !user;? [NC]
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
But suppose if I have one more cookie named "login" then how do we check that in this .htaccess file?
Do I need to repeat same lines of above code for that particular cookie? Can we check multiple cookies in a single condition or line?
Could you please try following, written based on your shown samples.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteCond %{HTTP_COOKIE} user;? [NC,OR]
RewriteCond %{HTTP_COOKIE} login;? [NC]
RewriteRule ^(.+)$ index.html?url=$1 [QSA,L]
RewriteCond %{HTTP_COOKIE} !user;? [NC]
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
OR try within a single ruleset itself. Kindly make sure either you use above rules OR following rules only one at a time.
RewriteEngine On
RewriteCond %{HTTP_COOKIE} (user|login);? [NC]
RewriteRule ^(.+)$ index.html?url=$1 [QSA,L]
RewriteCond %{HTTP_COOKIE} !user;? [NC]
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Can we check multiple cookies in a single condition or line?
Yes sure you can use condition like this:
RewriteEngine On
RewriteRule ^index\.(php|html)$ - [L,NC]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# execute when either of these cookies are set
RewriteCond %{HTTP_COOKIE} (^|;)(user|login)= [NC]
RewriteRule .+ index.html?url=$0 [QSA,L]
# execute when neither of above cookies are set
RewriteRule .+ index.php?url=$0 [QSA,L]

htaccess redirection of a specific page, not whole domain

I need to redirect a page (not whole domain!) to a specific other page that's on another domain, with htaccess. I tried a couple of things but can't make it to work. Can anybody help me out?
I tried this one:
#RewriteEngine On
#RewriteCond %{HTTP_HOST} ^domain.com/example [NC]
#RewriteRule ^(.*) https://otherdomain.com/page [P]
and this one:
#RewriteEngine on
#RewriteCond %{HTTP_HOST} ^domain.com/example/$ [NC,OR]
#RewriteCond %{HTTP_HOST} ^www.domain.com/example/$ [NC]
#RewriteRule ^(.*)$ http://www.mainpage.com/pagename/$1 [L,R=301,NC]
The page that should be redirected is /example
domain.com/example >>> should redirect to: https://otherdomain.com/page
The page that should be directed is accessible with and without www. in front of it, not sure if this is relevant.
Many thanks!
Your rule doesn't work because it has an error in the RewriteCond
#RewriteCond %{HTTP_HOST} ^domain.com/example/$ [NC,OR]
This condition will never be met as %{HTTP_HOST} variabe only contains the host header ie. example.com and not the URI string /example/ . You can test URI string using %{REQUEST_URI} variable in a separate RewriteCond .
This should work for you
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} /example [NC]
RewriteRule ^ https://otherdomain.com/page [R,L]

Rewriting URL that contains multiple parameters with multi-word strings (with mod_rewrite)

Was trying to rewrite the URL of a page that has multiple parameters and one of the parameters contains multiple words (separated by spaces when submitted).
This would be the URL: www.ABCD.com/store/itemsDescr.php?categ=headbands&id=123&name=brown%20with%20black
I would like the URL to show like this:
www.ABCD.com/store/headbands/123/brown-with-black
I tried this, but it did not work:
RewriteCond %{QUERY_STRING} ^categ=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &categ=([^&]+) [NC]
RewriteRule ^store/itemsDescr\.php$ $0/%1
RewriteCond %{QUERY_STRING} ^id=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &id=([^&]+) [NC]
RewriteRule ^store/itemsDescr\.php/[^/]+$ $0/%1
RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &name=([^&]+) [NC]
RewriteRule ^store/itemsDescr\.php/([^/]+/[^/]+)$ http://www.ABCD.com/store/$1/%1/? [L,QSA,NC]
Any help would be much appreciated!
NOTE: Before this rule I want to implement, I already have this other rule for another page, in case it can create conflict:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(store)/index\.php\?categ=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^store/(.+?)/?$ /store/index.php?categ=$1 [L,QSA,NC]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^categ=([^&]+)&id=([^&]+)&name=([^&]+)$ [NC]
RewriteRule ^(store)/itemsDescr\.php$ /$1/%1/%2/%3? [R=302,L,NC,NE]
RewriteRule ^(store)/([^\s]+)\s([^\s]+)$ /$1/$2-$3 [R=302,L,NC]
RewriteRule ^(store)/([^\s]+)\s(.+)$ /$1/$2-$3 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(store)/([^/]+)/([^/]+)/ /$1/itemsDescr.php?categ=$2&id=$3 [L,QSA,NC,NE]

htaccess + redirect users depending on the browser language

I was reading all questions related to this topic and I couldn't find anything.
First, I have this domain: www.example.com
My purpose is to redirect users depending on the language of the browser:
ex: www.example.com => www.example.com/es
www.example.com => www.example.com/en
I followed this rule but here is not the source url:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteCond %{HTTP_REFERER} !^*\.domain\.com.ar/ [NC]
RewriteRule ^$ http://www.example.com/es / [L,R]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteCond %{HTTP_REFERER} !^*\.domain\.be/ [NC]
RewriteRule ^$ http://www.example.com/en / [L,R]
</IfModule>
In this piece of code, where is establish the destination website?
Here:
RewriteRule ^$ http://www.example.com/es / [L,R]
and here:
RewriteRule ^$ http://www.example.com/en / [L,R]
No idea if that's a typo or if this is what you have in your htaccess file, but this will produce 500 internal server errors because you are giving RewriteRule 4 parameters, when it only wants either 2 or 3.
The other problem is with your %{HTTP_REFERER} regular expression. Apache's probably going to puke here: ^*\.domain\.com.ar/, you probably meant: ^[^/]*\.domain\.com.ar/ or something. So you probably want your rules to look like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteCond %{HTTP_REFERER} !^[^/]*\.domain\.com.ar/ [NC]
RewriteRule ^$ http://www.example.com/es/ [L,R]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteCond %{HTTP_REFERER} !^[^/]*\.domain\.be/ [NC]
RewriteRule ^$ http://www.example.com/en/ [L,R]
</IfModule>
Of course, you'd be replacing the instances of domain.com.ar and domain.be and www.example.com with the correct hostnames.
Also note: the Accept-Language header is a complicated string of qualifiers. It isn't as simple as an en or es. A spanish webbrowser could contain both an en and es simply because both are supported languages. Determining an exact language to redirect to based on this header isn't really in the scope of mod_rewrite and htaccess.
If you want to check domain and browser language, this is what you could do:
# Check domain (1), browser language (2) and redirect to subdirectory (3)
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ http://%{HTTP_HOST}/en/ [L,R=301]
# ... copy block above for other languages ...
# Fallback for any other language to spanish
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteRule ^$ http://%{HTTP_HOST}/es/ [L,R=301]

Resources