force http instead of https only for a particular file - apache

For a particular file, I want to force http instead of https
Example:
https://example.com/test.php?parameter1=value1
should be send to
http://example.com/test.php?parameter1=value1
How can I achieve this?

Your question is not very clear but try this .htaccess code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{QUERY_STRING} ^parameter1=.*$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^test\.php/?$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA,NC]
This will redirect ONLY if QUERY string parameter1=something is present in the original URL. If you want to redirect all /test.php URIs to https then remove the 1st RewriteCond line.

something like this:
RewriteRule ^https://example.com(.*) http://example.com$1 [R=301,NC]

Related

How to capture the requested url and pass its substring to some other link in .htaccess

I am making some changes in .htacess file and i want to capture the requested url, create substring of it and pass it to third part url.
For example
I am working on ubuntu, so currently my .htaccess file is in folder test. So when i access localhost/test/http://www.facebook.com , it takes me to service.prerender.io/test/http://www.facebook.com but i want to eliminate the 'test' and forwrd the request like service.prerender.io/http://www.facebook.com
Here is the code of my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/script.php
RewriteRule ^(.*)$ http://service.prerender.io%{REQUEST_URI} [L,QSA]
</IfModule>
You can use the RewriteCond %{REQUEST_URI} to set capture groups and instead of rewriting to http://service.prerender.io%{REQUEST_URI} you can rewrite to something like this http://service.prerender.io/%1.
Here is an example (untested):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/script.php
RewriteCond %{REQUEST_URI} ^/?test/([.]+) [NC]
RewriteRule ^(.*)$ http://service.prerender.io/%1? [L,QSA]
</IfModule>
What you are trying to captures is not directly the requested uri but part of it. You can get it from rewrite rule
Try following
RewriteRule ^test/(.*)$ http://service.prerender.io/$1? [L,QSA]
If you're accessing a page something like,
https://www.example.com/thisdir/thisfile.php
Then you will get following as request uri and script uri
REQUEST_URI: /thisdir/thisfile.php
SCRIPT_URI: https://www.example.com/thisdir/thisfile.php
And https://github.com/prerender/prerender-apache is more information about htaccess and prerender

force one page to use HTTPS and the others to use HTTP with .htaccess

I am trying to force one page to always use https it is not working
The url http://www.example.com/bookings/
and the .htaccess that I have
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^bookings$ https://www.example.com/bookings [L,R=301]
Try this rule as your very first rule in your root .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^bookings/?$ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE,NC]

.htaccess is concatenating rules

I have a .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)\/([0-9]*)\/?$ ler.php?categoria=$1&id=$2
this is working for http://www.domain.com/frases/13571/
The point is that I need to redirect all HTTP to HTTPS after this rule is already applied.
So I write this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)\/([0-9]*)\/?$ ler.php?categoria=$1&id=$2
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But isn't working like I supposed to. I'm getting this when I perform a HTTP request:
https://www.domain.com/frases/13571/?categoria=frases&id=13571
What is the right code that just change the protocols without modify the others parts of url?
I think that my code is doing 2 requests in the first rules and that is generating this wrong url.
L flag must be applied to your rewrite rule.
You can use this code instead
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/$ /ler.php?categoria=$1&id=$2 [L]
Note:
you don't need to escape slashes (\/ can be /)
you'll need to clear your browser's cache before trying again with this code. Your old rule is now in cache and you won't see the new code working if you don't clear it.
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You rewrite from the original REQUEST_URI, the current is what you have captured, so try:
RewriteRule (.*) https://%{HTTP_HOST}$1 [R=301,L]
or
RewriteRule (.*) https://$1 [R=301,L]
If not working, rewrite log could be useful.

htaccess rewrite base url

Anyone out there good with htaccess and mod rewrite - i need your help!
I need to rewrite the base part of a url.
for example all requestst to http://domain1.com need to go to http://domain2.com
The requests will typically be in the form as follows:
http://domain1.com/main/test?q=1
i then need to go to http://domain2.com/main/test?q=2
Pleas help!
Thanks in advance
Try this in your .htaccess file:
Options +FollowSymLinks
RewriteEngine on
# redirect for http
RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^/?(.*)$ http://domain2.com/$1 [R=301,QSA,L,NE]
# redirect for https
RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^/?(.*)$ https://domain2.com/$1 [R=301,QSA,L,NE]
R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
QSA will append your existing query parameters
$1 is your REQUEST_URI
Rewriting URL's across multiple domains? I'm not entirely sure this can work, considering Apache's request-handling algorithm. You're looking for a redirect, not rewrite.

redirect http to https for some page in site in APACHE

I want to one of my site's page will use only HTTPS.
I have given manually link to all sites to https.
But I want that if user manually types that page URL with http then it should be redirected to https page.
So if user types:
http://example.com/application.php
then it should be redirected to
https://example.com/application.php
Thanks
Avinash
Here's a couple of lines I used in an .htaccess file for my blog, some time ago :
RewriteCond %{HTTP_HOST} =www.example.com
RewriteCond %{REQUEST_URI} ^/admin*
RewriteCond %{HTTPS} !=on
RewriteRule ^admin/(.*)$ https://www.example.com/admin/$1 [QSA,R=301,L]
Basically, the idea here is to :
determine whether the host is www.example.com
and the URL is /admin/*
Because I only wanted the admin interface to be in https
which means this second condition should not be useful, in your case
and https is off (i.e. the request was made as http)
And, if so, redirect to the requested page, using https instead of http.
I suppose you could use this as a starting point, for your specific case :-)
You'll probably just have to :
change the first and last line
remove the second one
Edit after the comment : well, what about something like this :
RewriteCond %{HTTP_HOST} =mydomain.com
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://mydomain.com/$1 [QSA,R=301,L]
Basically :
using your own domain name
removing the parts about admin
Try this rule:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^application\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This rule is intended to be used in the .htaccess file in the document root of your server. If you want to use it in the server configuration file, add a leading slash to the pattern of RewriteRule.
use this:
RewriteEngine On
# Turn SSL on for /user/login
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/user/login
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
# Turn SSL off everything but /user/login
RewriteBase /
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/user/login
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1 [R=301,L]
website
open httpd.conf or .htaccess file (mod_rewrite not required):
# vim httpd.conf
Append following line :
Redirect permanent / https://example.com/