htaccess excluding 1 folder from rewriterules - http

Depending on my value IS_HTTP, I decide after the code below, whether URL should be https or http.
I want always http (redirected from https) for http://mydomain.com (has an empty QUERY_STRING), http://mydomain.com/?q=home, http://mydomain.com/?qq=home.
And the code below works fine for such URLs, it sets up IS_HTTP to 1. Everything is perfect. But I want also URLs to administration folder always to be httpS, so I want to exclude such URLs from that block.
That's why I added the second string to the code below, but it doesn't stop such URLs from applying RewriteRule ^ - [E=IS_HTTP:1]. Why?
#determine if page is supposed to be http
RewriteCond %{REQUEST_URI} !^administration [NC]
#if it has q=home or qq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(q=home|qq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]
Again, I want https://mydomain.com/administration/index.php and all other files from the folder /administration to be stopped before RewriteRule ^ - [E=IS_HTTP:1]
Why the code above does NOT stop them?
The second string I've tried was:
RewriteCond %{REQUEST_URI} !^administration [NC]
RewriteCond %{REQUEST_URI} !^/administration [NC]
but none of them works. (I believe / is not needed here as REQUEST_URI doesn't start from /, but I could be wrong).
Thank you.

Instead of this condition:
RewriteCond %{REQUEST_URI} !^administration [NC]
have this condition:
RewriteCond %{REQUEST_URI} !^/+administration [NC]
Remember that %{REQUEST_URI} always starts with a slash and can have multiple slashes if it is typed that way in browser.

Related

How can I remove multiple slashes? (from htaccess) [duplicate]

How come that none of these solutions work on my Apache servers:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/{2,} [NC]
RewriteRule ^(.*) $1 [R=302,L]
or
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R=302,L]
or
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=302,L]
among others that I tried.
I tried ALL solutions from this page: Issue In Removing Double Or More Slashes From URL By .htaccess
and other pages as well.
The problem is that the rule in the htaccess does not match the double slashes within these above patterns.
I tried also "literal" patterns, with exact urls without regex patterns. Still nothing. But with a single slash - all work.
It seems like Apache has a problem when it finds: "//" - the url is clearly not recognized and the rule is ommited.
The idea is simple: to get rid of double slashes and replace them with one slash:
http://demo.codesamplez.com/html5//audio -> http://demo.codesamplez.com/html5/audio
Do you know how can I redirect URL with double slash "//" to a single onen "/"?
Here's htaccess (removed the longest comments in the file):
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test//slash
RewriteRule ^(.*)$ /test/slash [R=302,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite all other queries to the front controller.
RewriteRule ^ %{ENV:BASE}/app.php [L]
</IfModule>
Try the following instead, near the top of the root .htaccess file, before any existing rewrites:
# Remove multiple slashes anywhere in the URL-path
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule (.*) /$1 [R=302,L]
This uses the fact that multiple slashes have already been reduced in the URL-path matched by the RewriteRule pattern. And the check against THE_REQUEST (which contains the first line of the request headers and does not change throughout the request) ensures that multiple slashes were initially present somewhere in the URL-path (excluding the query string).
Another potential issue is if you have a proxy server (or load balancer) in front of your application (Apache) server and this is perhaps normalizing the request (reducing multiple slashes, removing trailing space, etc) as it forwards the request to your application (Apache) server. The application server then never sees the original request (with multiple slashes) that you see in the browser.
Looking at your attempts...
RewriteCond %{REQUEST_URI} ^/test//slash
RewriteRule ^(.*)$ /test/slash [R=302,L]
This "should" work, with the limited example as posted. However, the REQUEST_URI server variable is modified throughout the request, so if the URL has already been modified (perhaps in the server config) then this may not match.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/{2,} [NC]
RewriteRule ^(.*) $1 [R=302,L]
This only matches multiple slashes at the start of the URL-path, not anywhere in the URL-path. This would also result in a malformed redirect if used in .htaccess (unless you also had a RewriteBase directive set). Without the slash prefix on the substitution string this rule is probably intended for a server or virtualhost context, not .htaccess.
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R=302,L]
The same issue with the use of REQUEST_URI as mentioned above. Otherwise, this should work. However, it results in multiple redirects if there are more than 1 group of multiple slashes. eg. //foo//bar.
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=302,L]
The same as above, except this only matches double slashes, rather than groups of two or more slashes. So if there are more than two slashes in a group it will result in multiple redirects.

Permanent redirect specific url query string

I searched on the site, but i didn´t find an exact answer.
I have multiple urls in a wordpress site with different parameters like this:
http://www.example.com/folder/?filter_color=353&orderby=price-desc
http://www.example.com/folder/?filter_material=345&orderby=date
http://www.example.com/folder/?filter_size=43&price-asc
I need to permanent redirect all of them to http://www.example.com/folder/
Is it possible with htaccess? Thanks!
EDIT 1:
I tried with this code but it doesn´t work. I want to redirect those old urls because they lead to a nothing found page. So after i copy this code and save the htaccess, nothing changes. The nothing found page still appear and the old url is shown in the browser. So it does nothing.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder$
RewriteCond %{QUERY_STRING} ^filter_material=([0-9]*)$
RewriteRule ^(.*)$ http://www.example.com/folder/ [R=301,L]
EDIT 2:
I finally achieved what i wanted with this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^filter_([\w]*) [NC]
RewriteRule ^folder/$ http://www.example.com/folder/? [R=301,NE,NC,L]
The rewrite condition for the query string is a regular expression, which is a tool for matching against search patterns.
The regular expression you are using – ^filter_material=([0-9]*)$ would match against URLs with a query string that starts with filter_material=, followed by an arbitrary number of digits.
The URL you want to redirect, however, does not match here, because it also contains &orderby=date, which is not all digits.
Since all the URLs you want to redirect start with filter_, you can simply use a regular expression that matches any string that starts with that, like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder/?$
RewriteCond %{QUERY_STRING} ^filter_.+$
RewriteRule ^(.*)$ http://www.example.com/folder/ [R=301,L]
The period means “any character”, the plus means “none, one, or more than one”.
Note that I've also added /? to the rewrite condition for the request URI, which means “there could or could not be a slash after folder”.
You can test this rewrite rule here: http://htaccess.mwl.be?share=2329d6f6-4fd5-5ff2-9035-a1957a242a42
I finally achieve what i wanted with this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^filter_([\w]*) [NC]
RewriteRule ^folder/$ http://www.example.com/folder/? [R=301,NE,NC,L]

htaccess and WordPress problems

I wanna redirect all my wordpress pages from one domain to another, such as:
from: http://domain1.com/page/
to: domain2.com/page/
So I added to htaccess of domain1.com:
RewriteRule (.*)$ http://www.domain2.com\/$1 [R=301,L]
But I want to make certain files exceptions to the above rule, so I also added:
RewriteCond %{REQUEST_URI} ^(images|javascripts|stylesheets|css|images-globa|js-global|js|htm|html).*
but the second rule doesn't seem to work. :(
Those aren't exceptions, those are "the request MUST be images/javascripts/stylesheets/etc in order to redirect", so you've got it backwards. You want a ! in there:
RewriteCond %{REQUEST_URI} !(images|javascripts|stylesheets|css|images-globa|js-global|js|htm|html)
RewriteRule (.*)$ http://www.domain2.com\/$1 [R=301,L]
Though I'm assuming you're way overreaching with the regular expression there, some of those look like you are matching against only the extension, so:
RewriteCond %{REQUEST_URI} !(images|javascripts|stylesheets|css|images-globa|js-global)
RewriteCond %{REQUEST_URI} !\.(js|html?|png|je?pg|gif)$ [NC]
RewriteRule (.*)$ http://www.domain2.com\/$1 [R=301,L]

htaccess rewriterule: redirect http to https or https to http (both ways) depending on URL typed

Could anybody please help me to write rewriterule, that redirects http<->https (back and force depending of URL typed) with these conditions:
1) http://www.mydomain.com , http://www.mydomain.com/?p=home , http://www.mydomain.com/?p=home1 , http://www.mydomain.com/?qqq=home are always http, even if https are typed instead of http.
2) all the rest pages are always https, even if http was typed instead of https.
The code below redirects all urls (and keeps parameters), except http://www.mydomain.com , to https.
#redirects http to https if there are parameters
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} !^$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
I've tried to add the code below right after the code above to redirect https to http as well (if there are no parametres), so that all pages are always https, except www.mydomain.com, but I had no luck. Also I missed ?p=home, ?p=home1, ?qqq=home - I don't know how to add them.
RewriteCond %{HTTP} off
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
Try adding the following to your htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#determine if page is supposed to be http
#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]
#all pages that are supposed to be http redirected if https
RewriteCond %{HTTPS} on
RewriteCond %{ENV:IS_HTTP} 1
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]
#all other pages are sent to https if not already so
RewriteCond %{HTTPS} off
RewriteCond %{ENV:IS_HTTP} !1
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]

How to redirect HTTPS to HTTP for some pages using .htaccess

I am facing URL rewriting problem using .htaccess.
I have to redirect all the URLs to HTTPS except 4 URLs. Below is the code in my .htaccess file:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_REFERER} !^https(.*)/(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{REQUEST_URI} ^/?index\.php$ [NC]
RewriteCond %{REQUEST_URI} ^/?index\.php?view=default [NC]
RewriteCond %{REQUEST_URI} ^/?index\.php?view=news [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301, NC, L]
# Require SSL on all other pages
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}r%{REQUEST_URI} [R=301, NC, L]
I need to disable HTTPS for following URLs:
mydomain.com/
mydomain.com/index.php
mydomain.com/index.php?view=new
mydomain.com/index.php?view=new&abc=xyz&aaa=bbb…
mydomain.com/index.php?view=default
For above 5 URLs, I want to redirect in http, but it does not work. It redirects all the URLs to HTTPS only.
The RewriteCond conditions act like an "and", so it's not going to match all 4 file patterns at the same time. One option is separate sets of rules - I like to do that when figuring it out. Another option is to get an "or" pattern working.
The querystring is handled in the RewriteCond, instead of down in the RewriteRule.
I'm not sure what you needed to test on HTTP_REFERER, so I left it out.
This separates out to two rules to keep it simpler. The first does the plain slash or optional index.php with no parameters. The second checks the querystring for view = default or new.
RewriteCond %{HTTPS} on
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/(index\.php)?$ [NC]
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301, NC]
RewriteCond %{HTTPS} on
RewriteCond %{QUERY_STRING} ^view=(default|new(&.*)?)$ [NC]
RewriteCond %{REQUEST_URI} ^/?index\.php$ [NC]
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301, NC]
The querystring condition checks for view=default exactly, or view=new or view=new&any=other... The optional &.* means the next thing has to be an ampersand or nothing, which prevents view=new2 from matching by mistake.
Note the user might get a browser warning about redirecting to an "insecure" page, when you redirect them from https to http. Redirect warning isn't a big concern anymore; I just remember it from IE6 days.
How to redirect from HTTPS to HTTP without annoying error messages
EDIT:
I added a condition to the first rule for an empty querystring. I realized that was matching any querystring without it.
I was also faced that issue and for me this help
RewriteCond %{HTTPS} !^on$
RewriteCond %{REQUEST_URI} ^(/check-out|/payment)$
RewriteRule (.*) https://www.my_site.com/$1 [R,L]
You need to change pages name & your site url.

Resources