How to check multiple cookies are set in .htaccess? - .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]

Related

Clean URLs in a subfolder

I've read and followed guides and looked for answers to other people's questions, but I'm struggling with url rewriting and htaccess.
I have a subfolder on my site with an index page which handles query strings to bring up dynamic content. I'd like the variable to appear to be a subfolder name e.g.
http://mywebsite.com/subfolder/index.php?v=variable
to
http://mywebsite.com/subfolder/variable
The .htaccess file I've made is at http://mywebsite.com/subfolder/ i.e. the same folder as index.php
How can I get this to work? Any help gratefully appreciated.
You can use these rules inside your /subfolder/.htaccess
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php?v=$1 [L]
Update (passing two values)
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)&v2=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)\s [NC]
RewriteRule ^ %1? [R=301,L]
# Don't touch to existing files/folders
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite /xxx to /index.php?v=xxx
RewriteRule ^([^/]+)$ index.php?v=$1 [L]
# Rewrite /xxx/yyy to /index.php?v=xxx&v2=yyy
RewriteRule ^([^/]+)/([^/]+)$ index.php?v=$1&v2=$2 [L]
Use this in your .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} v=(.+)$ [NC]
RewriteRule ^ subfolder/%1? [R=301,L,NE]
This grabs the variable using %{QUERY_STRING} and then appends it to the rewrite using %1. You'll see I've added a ? onto the end of the rewrite. That is to stop the original query from appearing on the end of the URL.
I've used R=301 which is a permanent redirect. You might want to changes this to R=302 while you're testing, as this is temporary.
You can view a test of this rule working here: https://htaccess.madewithlove.be?share=7b6832e9-2c05-5d1d-916c-e4dd0f5b1da6
Make sure you clear your cache before testing this.

How to redirect from non-www to www with correct page?

I am trying to 301-redirect example.com to www.example.com by .htaccess:
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Problem: my-site.com/contact.html redirects to www.example.com/index.php instead of www.example.com/contact.html.
How can I accomplish that?
UPDATE: this is the complete .htaccess for my Joomla-site (comments stripped):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
###### this is the code in question...
RewriteCond %{HTTP_HOST} ^example$ [NC]
RewriteRule ^(.*)$ http://www.example/$1 [R=301,L]
# RewriteBase /
## Begin - Joomla! core SEF Section.
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
# take care of sitemap.xml
RewriteCond %{REQUEST_URI} ^/sitemap.xml
RewriteRule .* /index.php?option=com_xmap&view=xml&tmpl=component&id=1&format=html [L]
I moved my two lines up just below RewriteEnigine On - but had the same problem again. Clueless.
Problem: example.com/contact.html redirects to www.example.com/index.php instead of www.example.com/contact.html.
I'm going to guess that you have other rules in your htaccess file, one that routes everything to index.php. You need to make sure that your redirect rule is before any other rule that you have.
After checking your htaccess file, you need to add an L flag at the end of your first rule:
RewriteRule .* index.php [F,L]
Because without it, the redirect gets applied even if the first rule gets applied (e.g. it gets rewritten to index.php with a 403 response waiting in the queue).
Firstly uncomment line
# RewriteBase / //Remove #
Below the above line write this code
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I guess I'm already in the wrong for asking for clarification but you can just delete this.
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
I see you added an exclamation point and I've seen this redirect elsewhere without it. What does the exclamation point do in this case or is that incorrect?
Have a great day.

Adding options to existing .htaccess

I have this .htaccess code which I have put together from different sources and I am having trouble adding a couple of extra options to it.
<Limit GET POST PUT>
order deny,allow
deny from all
allow from XXX.XXX.XXX.XXX
</Limit>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/css/" [OR]
RewriteCond %{REQUEST_URI} "/js/" [OR]
RewriteCond %{REQUEST_URI} "/images/"
RewriteRule .* - [L]
RewriteRule ^([a-zA-Z0-9-/_]+)/([a-zA-Z0-9-/_%]+)/([a-zA-Z0-9-/_]+)/([a-zA-Z0-9-/_]+)$ index.php?primary=$1&secondary=$2&tertiary=$3&quaternary=$4 [QSA]
RewriteRule ^([a-zA-Z0-9-/_]+)/([a-zA-Z0-9-/_%]+)/([a-zA-Z0-9-/_]+)$ index.php?primary=$1&secondary=$2&tertiary=$3 [QSA]
RewriteRule ^([a-zA-Z0-9-/_]+)/([a-zA-Z0-9-/_%]+)$ index.php?primary=$1&secondary=$2 [QSA]
RewriteRule ^([a-zA-Z0-9-/_]+)$ index.php?primary=$1
Firstly I would like to add the forced use of www. in the URL. When I have tried to add this, the resulting URL becomes the output of the current RewriteRules, containing the GET variables. I believe this is because the rules work on a loop?
Another thing I would like to do is catch anything that doesnt meet the criteria of the current rules and send that to something like index.php?primary=error. The way it works now is almost perfect for my use because if there is a malformed URL or illegal character, the site is not going to even attempt to display the page. The site will create all URLs safely, so any bad URLs would be the result of experimenting in the address bar, but it would be nice to have an error page rather than a page not found.
Thanks in advance.. And sorry for the muddle-through use of RewriteRule!
Your regex are incorrect since hyphen can be unescaped only when it appears as first or last symbol in a character class, it needs to be escaped otherwise.
So instead of: [a-zA-Z0-9-/_%]
Use: [\w%/-]
Your complete set of rules:
RewriteEngine On
RewriteBase /
# add www in the URL
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} ^/(css|js|images)/ [NC]
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?primary=$1&secondary=$2&tertiary=$3&quaternary=$4 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?primary=$1&secondary=$2&tertiary=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?primary=$1&secondary=$2 [QSA,L]
RewriteRule ^([^/]+)/?$ index.php?primary=$1 [QSA,L]

mod-rewritre has no effect:

my .htaccess file does nothing: any ideas?
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^(.+)\.css$ [NC]
RewriteRule ^http://127.0.0.1(.*)$ http://127.0.0.1/getcss.php?$1/$2 [L,NC]
RewriteCond %{HTTP_COOKIE} ^.*fsite-cookie=([^;]+)$ [NC]
RewriteRule ^http://127.0.0.1(.*)$ http://127.0.0.1/cloked.php?$1/$2 [L,NC]
RewriteCond %{http_COOKIE} ^.*site-cookie=!([^;]+) [NC]
RewriteRule ^(.*)$ htp://127.0.0.1/noaccess.php?$1 [NC]
As far as I know you can only rewrite the path of the request (unless you send a redirect, but the first part will still have to match the path).
You're trying to match the protocol and host name as well, which will fail.
Try to remove every instance of http://127.0.0.1 from that configuration.

.htaccess redirecting errors

First off I am using the Codeigniter Framework so this issue is a workaround the way CI process URLs along with the current redirects I have set up using mod_rewrite.
I am trying to get a URL like this /?gclid=somestringgoeshere to redirect to /index.php?/home/gclid/somestringgoeshere.
The current .htaccess I have set is below
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6_$7 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4 [L]
RewriteRule ^([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3 [L]
RewriteRule ^([^_]+)-([^.]+)$ index.php?/$1_$2 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# This last condition enables access to the images and css folders, and the robots.txt file
# Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I am trying to use the following code right above the first set up rewrite conditions and rule's to catch it before it try's anything else
RewriteRule ^?gclid=(.*)$ index.php?/home/gclid/$1 [L]
and
RewriteRule ^\?gclid=(.*)$ index.php?/home/gclid/$1 [L]
and
RewriteRule ^/?gclid=(.*)$ index.php?/home/gclid/$1 [L]
All either don't show the correct page or come up with a 500 internal error.
The URI’s query can only be tested with the RewriteCond directive:
RewriteCond %{QUERY_STRING} ^gclid=(.*)
RewriteRule ^$ index.php?/home/gclid/%1 [L]
Or more general (will consider further query parameters):
RewriteCond %{QUERY_STRING} ^([^&]*&)*gclid=([^&]*)
RewriteRule ^$ index.php?/home/gclid/%2 [L]
Oh, by the way: RewriteCond directives only correspond to the first following RewriteRule directive.

Resources