Using Apache Rewrite to go from HTTP to HTTPS and vice-versa, but I get a 302 response and my app errors out - apache

I really hope someone knows what I'm doing wrong here, cuz I sure don't!
We have a certain page on our site which has account balance information on it, and we want to make it secure with SSL. But we only want this one particular page to be secure. I have the following in the localhost:80 virtualhost, and it works perfect:
RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} \/account\.php(.*) [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [QSA,L]
However, as you might guess, we want all other pages to just use HTTP. So I stuck this little snippet into my localhost:443 virtualhost:
RewriteCond %{HTTPS} on
RewriteCond %{SCRIPT_FILENAME} !\/account\.php(.*) [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [QSA,L]
...And that's when the problem happens. I have no problems going back to HTTP, but when I click the link to go to the account page, it changes to HTTPS but I immediately get an error 302 response. I do not get this response when I remove either one of those rewrite rules, it only happens when they are both there.
I have tried replacing [QSA] with [R] and [R=301], to no avail.
(I'm aware that the %{HTTPS} on/off is a bit redundant ;))
So I have two questions:
Is there something I am forgetting or doing wrong that might be causing this?
Is using [QSA] redundant with $1? We use the GET method a lot to specify pages and what not.
We are using PHP 5.2.9 and Apache 2.
Many thanks in advance!!
Brian

Whilst it's on here (and not moved to serverfault).. try
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/account\.php https://%{HTTP_HOST}/account.php [R=301,QSA,L]
HTTP/1.1 302 = Found (and is not an error code), but temporarily at another location.
EDIT
Actually, whilst you are putting the code in separate VirtualHosts, you may as well do
(In :80)
RewriteRule ^/account\.php https://%{HTTP_HOST}/account.php [R=301,QSA,L]
(in :443)
RewriteCond %{REQUEST_URI} !/account\.php$
RewriteRule ^(.*) http://%{HTTP_HOST}$1 [R=301,QSA,L]

Related

What is the correct syntax for "if host is not foo, redirect to bar" in a .htaccess file?

This website has a ton of extra domains (note: these are not subdomains; one of them, for instance, is http://eduard.fi) that the owner (or the SEO people, rather) wants to redirect to the main domain. Instead of listing them one by one, this is what I tried:
RewriteCond %{HTTPS_HOST} !^masetti\.fi$
RewriteRule ^(.*)$ https://masetti.fi/$1 [R=301,L]
However this creates a redirect loop. Why is that? This does not produce a server error, so for that part the syntax is correct, but it does not do what I want.
You were close, but made a logical mistake. Take a look at this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^masetti\.fi$
RewriteRule ^(.*)$ https://masetti.fi/$1 [R=301]
An alternative would be that:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^masetti\.fi$
RewriteRule ^ https://masetti.fi%{REQUEST_URI} [R=301]
The RewriteCond has been slightly altered: It is the variable %{HTTP_HOST} you want to check, not %{HTTPS_HOST}which does not exist.
PS: it is a good idea to start out with a 302 redirection and only change that to a 301 once everything works as intended. That prevents issues with client side caching.

htaccess redirect to a specific url on same domain (without looping)

I really hope you can help me out (it is driving me crazy).
I've tried dozens of setups and nothing seems to work, Googled myself dizzy and tried numerous different setups, but it all seems to result in a loop or a server error.
This is what needs to happen:
I have a site with multiple domains attached to it. What I need is that when someone visits the website via the "domain.co.uk"-domain, a redirect to the correct language parameters (among others) takes place.
To be very specific: when visiting via "www.domain.co.uk" the visitor must be redirected to "www.domain.co.uk?lang=en&noredir=1&currency=3"
I've made sure that the www is present with this:
RewriteCond %{HTTP_HOST} !^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]
The trouble is (I think) the redirect within the same domain without causing a loop.
I've tried stuff like this, but with no result:
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$
RewriteRule ^$ http://www.domain.co.uk/?lang=en&noredir=1&currency=3 [L,R=301]
Hope you can help,
Cheers!
This will cause a loop:
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$
RewriteRule ^$ http://www.domain.co.uk/?lang=en&noredir=1&currency=3 [L,R=301]
Because you're only checking the host header. Every time the redirect fires it will arrive back at the server with a host header of www.domain.co.uk and redirect again. You need to also check the query string and only redirect if it doesn't already match what you sent:
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$
RewrteCond %{QUERY_STRING} !lang=en&noredir=1&currency=3
RewriteRule ^$ http://www.domain.co.uk/?lang=en&noredir=1&currency=3 [L,R=301]

apache RewriteRule not working

I have this url http://www.example.com/Courses/get/38789/my-course, i added this rule to the .htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/Courses/get
RewriteRule ^Courses/get/(.*)/(.*)$ course-$1-$2 [R=301,L]
but when i go to http://www.example.com/Courses/get/38789/my-course nothing happens, i stay on the same page, there is no redirect.
p.s the link is just an example not the actual link
A more efficient method would be to use the following:
RewriteRule ^Courses/get/(\d+)/([^/]+)/?$ /course-$1-$2 [R=301,L]
Now, keep in mind that this rule should come before any rules that may rewrite the request to, say, an index.php file. This would be naturally true if the code you posted in your question was all of your code. If not, please post your entire .htaccess file so we can be sure it is being placed in the right location.
Be sure the mod_rewrite is turned on, and the you have set AllowOverride All in your virtual host/Apache site configuration. If you're running on a shared production server, this would not apply to you.
Side note: Whilst it does work, you need not use RewriteEngine on twice - only once at the beginning of your file will suffice. You also do not need RewriteCond %{REQUEST_URI} ^/Courses/get - it is essentially redundant as you are already using an expression to test against the request.

How to avoid apache transfering urlencoded chars to lowercase when doing a 301 redirect

I ran into this problem when I tried to redirect all contents from one site to another. My http server is apache, and Im using mod_rewrite to do the job.
So I added these codes to httpd.conf:
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^/(.*) http://www.example2.com/$1 [R=301,L]
Everthing seems to be good after Ive done that, however when I visit a urlencoded url such as http://www.example.com/?word=%E5%A4%A9 , apache redirects me to http://www.example2.com/?word=%e5%a4%a9 , as you see, all the urlencoded chars are trasnfered to lowercase which makes me impossible to infrom search engine about my domain change because search engine asks for a exact match of new and old URI(case sensitive).
BTW, when I use urlencode() function in PHP, the results it returned is automatically uppercased, so the standard URL should be something like this %E5%A4%A9.
I googled a lot and got nothing so I had to come here to ask for help, any help will be appreciated, thanks in advance.
Not sure if this works, but try
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^/(.*) http://www.example2.com/$1?%{QUERY_STRING} [R=301,L]
or
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^/(.*) http://www.example2.com/$1 [R=301,L,NE]

RewriteCond to change displayed host, but not server

Typing out the title to this leads me to believe this might not be possible due to security concerns, but I will ask anyway. I have shortcode support running on my server, lets call it xx.yy
I want it so when a user sends a request to xx.yy, it just changes the displayed host to another valid domain running on the same box.
I have this so far (lets the server know to accept requests from xx.yy):
RewriteCond %{HTTP_HOST} ^.*xx.yy [NC]
RewriteRule ^/(.*)$ http://127.0.0.1:<PORT_OMITTED>%{REQUEST_URI} [P,QSA,L]
RewriteCond %{HTTP_HOST} ^.*mysite.com [NC]
RewriteRule ^/(.*)$ http://127.0.0.1:<PORT_OMITTED>%{REQUEST_URI} [P,QSA,L]
It works, and it directs the traffic into my app, but the url says http://xx.yy when I would rather it say http://mysite.com
I know i could redirect to http://mysite.com instead of 127.0.0.1, but I have 4 parallel boxes of mysite.com and going back out to DNS to maybe go to another box seems like a waste when I am already here. Also, I am not sure how POST requests would work like that.
What can I do?
If mysite.com is on the same box, and DNS is pointing there, just put R=301 in [P,QSA,L]
Example
RewriteEngine on
RewriteCond %{HTTP_HOST} ^OldDomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.OldDomain.com$
RewriteRule ^(.*)$ http://www.NewDomain.com/$1 [R=301,L]
From http://www.liewcf.com/redirect-webpages-to-another-domain-538/
Other than that, though there's no way for good reasons to do what you are asking. Imagine if someone could link to legitwebsite.com, but have it get sent to evilempire.com.
It'd be chaos, that's what.