Apache 2.4 exact redirection matching strings - redirect

I have a URL :
https://www.example.com/login/login.cgi?user=option
Want to exactly match login.cgi
If user hits https://www.example.com/login/login.cgi need to redirect to external site say www.redirect.com
If anyone hits https://www.example.com/login/login.cgi?user=option it should not cause any redirection.
I tried various option of query string and as well Rewrite rules seems to not work.
I tried using :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^login.cgi$
RewriteRule ^login/$ https://www.redirect.com? [R=302,L]

This should work for you.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?login/login\.cgi$ https://example.com [R,L]
This will redirect yoursite.com/login/login.cgi to example.com .
The condition RewriteCond %{QUERY_STRING} ^$ matches empty querystring meaning that when the the URI is /login/login.cgi without any querystring the Rewriterule will be applied.
The /? in the beginning of RewriteRule pattern makes it possible to use the rule in both contexts server.config or in htaccess.

Related

How to make a redirect in .htaccess for pages from different subdomains

I'm not very advanced at programming and I need to make a 301 redirection from one specific subdomain page to another. The tricky part, at least for me, is that the pages are on two different subdomains. So far I've only managed to 301 ALL pages to the target or crashing the whole site.
I've used many, but are currently at:
RewriteCond %{HTTP_HOST} !^spiele.deutschedownloads.de/?DriverScanner/Diverse//download/4770$ [NC]
RewriteRule ^(.*)$ http://programme.deutschedownloads.de/?DriverScanner/Drivers/Werkzeuge-Diverse/download/4413 [L,R=301]
What I want to do is simply to 301 redirect
spiele.deutschedownloads.de/?DriverScanner/Diverse//download/4770
To
http://programme.deutschedownloads.de/?DriverScanner/Drivers/Werkzeuge-Diverse/download/4413
Any help is most appreciated!
This should work:
RewriteCond %{HTTP_HOST} ^spiele\.deutschedownloads\.de$ [NC]
RewriteCond %{QUERY_STRING} ^DriverScanner/Diverse//?download/4770/?$ [NC]
RewriteRule ^ http://programme.deutschedownloads.de/?DriverScanner/Drivers/Werkzeuge-Diverse/download/4413 [R=301,L]
First condition matches the domain in case they are both on the same root folder.
Second condition matches the query string.
If both conditions are OK it redirects.

JBoss and RewriteCond and RewriteRule

I need to redirect all requests that don't come from this domain ti-passis.rhcloud.com
and use https to http to avoid certificate error.
Example:
https://ti-passis.rhcloud.com/contato (should'n be redirected)
https://tetraimoveis.com/contato (should be redirected because of certificate error, once the certificate is for "*.rhcloud.com")
RewriteCond %{HTTP_HOST} !^ti-passis.rhcloud.com$ [RC]
RewriteCond %{HTTP:X-Forwarded-Proto} https
RewriteRule .* http:// %{HTTP_HOST}%{REQUEST_URI} [R,L]
The RewriteCond works alone, but if I put then together, nothing works.
Am I doing something wrong?
The correct way to do it is (though you likely figured it out by now)
RewriteCond %{HTTP_HOST} !^ti-passis.rhcloud.com$ [RC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} https
RewriteRule .* http:// %{HTTP_HOST}%{REQUEST_URI} [R,L]
You're basically just "Or"ing the expression. As per JBoss "'ornext|OR' (or next condition)
Use this to combine rule conditions with a local OR instead of the implicit AND."

htaccess redirect not working with some browsers

here is the htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NCL]
the first rule checks if you have www , and if not redirects to the www + https, the next rule checks to see if the client accesses the website via https, everything works, except on opera, opera redirects me to https://www.domain.com/https://domain.com/ like it puts the domain + request uri in {request_uri}, and i can't seem to make it work.Any suggestions on how to make this work correctly?
Edit :
IE is not working too.It just gives server error.
The redirect is done by Apache, so Opera is not guilty in this error. I tested your rules and got an error:
RewriteRule: unknown flag 'NCL'
Try this:
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,L]

redirecting just a single non-www homepage to www version, without redirecting any other pages

I need to redirect the non-www version of just a single page, the homepage, to www.
I want to leave the rest of the site alone. Due to a need to keep a specific page non www.
So if i try a rewrite rule of
/index.html http://www.example.com/
I get a infinite redirect type error.
There's probably a way to achieve what you want, although I must say that this has a bit of a "smell" to it. It sounds like you are trying to treat a single symptom rather than fix a problem (which, admittedly might be beyond your control). Is it possible that your organization needs to setup a domain pointer so that www.example.com and examples.com are the exact same thing?
So you want
http://example.com/ to go to http://www.example.com/,
but you want
http://example.com/index.html,
http://example.com/otherstuff, and
http://example.com/subdir/path/file.php
to stay the same?
For Apache:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/$ http://www.example.com/ [R=301,L]
If you want any non-www subdomain on the site to get redirected to the www version:
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^/$ http://www.%{HTTP_HOST}/ [R=301,L]
If you want to redirect everything except a specific page:
RewriteCond %{REQUEST_URI} !^/subdir/path/file.php$
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

How do I use .htaccess to force www. while using Zend Framework

I want to force a www. prefix on my website by using a .htaccess 301 redirect. I am currently trying:
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
Which normally works, but I am using Zend Framework which causes all requests to be redirected back to http://www.mysite.com/index.php regardless of the initial request.
For example...
http://mysite.com/blog,
http://mysite.com/contact,
http://mysite.com/blog/this-is-my-article,
Will all be redirected to http://www.mysite.com/index.php
However, if I initially request a specific file, such as...
http://mysite.com/some-file.htm
The redirect works properly, redirecting to http://www.mysite.com/some-file.htm
In first time, don't forget to enable the rewriting ("RewriteEngine on").
The last line is important if you use Zend Framework.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteRule !\.(pdf|php|js|ico|txt|gif|jpg|png|css|rss|zip|tar\.gz)$ index.php
Now the url...
http://mysite.com/some-file.htm
... redirect to http://www.mysite.com/some-file.htm but use the index.php
Don't forget to ignore sub-domains when re-directing to www.
http://www.theblogaholic.com/2011/01/16/force-www-using-htaccess-except-for-subdomains/