Restrict HTTP access to single page with .htaccess - .htaccess

I have this .htaccess file:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Can I force all HTTP access attempts to serve a specific page (e.g. /https-required.html) while processing HTTPS access normally?
Here is an example of the behaviour I expect, with some variation tolerated (for instance, if /https-required.html can only be served over HTTP, that's acceptable):
http://www.ex.com/ => http://www.ex.com/https-required.html
http://www.ex.com/valid-page.html => http://www.ex.com/https-required.html
http://www.ex.com/invalid-page.html => http://www.ex.com/https-required.html
http://www.ex.com/https-required.html => http://www.ex.com/https-required.html
https://www.ex.com/ => https://www.ex.com/
https://www.ex.com/valid-page.html => https://www.ex.com/valid-page.html
https://www.ex.com/invalid-page.html => https://www.ex.com/404.html
https://www.ex.com/https-required.html => https://www.ex.com/https-required.html
I wish to restrict the whole site to HTTPS but for that I must necessarily account for HTTP access attempts. I think I can disable HTTP completely, with something like
SSLRequireSSL
but that would yield a miserable experience for accidental HTTP access. The more common solution to this problem is to simply redirect http:// to https:// with something like
Redirect permanent / https://www.example.com
or
RewriteCondition %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
but I am specifically looking to avoid this redirect.

You can use it like this:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule !^https-required\.html$ /https-required.html [L,NC,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

Related

Redirect to HTTPS with URI & without www

I have the same problem as the User in this thread. I basically want to redirect all http requests to https without www using htaccess.
I have implemented a solution to this problem and it does a pretty good job.
However, it does not redirect to the corresponding URI. If I visit my website on port 80 example.com/news I will be redirected to https://example.com/index.php which is basically the root page..
I really need your help guys, I totally have no clue about htaccess..
EDIT This is what my mod_rewrite looks like..
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
The issue probably is that you first rewrite to index.php and then make an external redirection. Try changing the order of the rules. Note that the L flag only terminates the current walk through the rule set. It is restarted after that. So first do the external direction to switch to the https protocol, then do the internal rewriting if that is corrected.

Redirect to a HTTPS version using .htaccess

I need to modify my .htaccess file to redirect all URLs to a HTTPS version without the "www".
http://example.com --> https://example.com
http://www.example.com --> https://example.com
https://www.example.com --> https://example.com
This is how my .htaccess file looks like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ redirect.php?id=$1&page=$2 [QSA,L]
What modifications do I need to make to .htaccess in order to forward to HTTPS without the "www"?
It depends on what you are doing with your current code. That just looks like a file that is called redirect but is trying to display a php page from a SEO friendly URL. So I think that might be confusing people.
Anyway, to do what you need to do and force https without www, you only need one more rule above.
Also your second rule needs two capture groups in the RewriteRule test string because you are wanting two different values using $1 and $2 back references.
RewriteEngine On
#rediect www to non www and/or http to https --- all combinations.
RewriteCond %{HTTP_HOST} !^example\.com [NC,OR]
RewriteCond %{HTTPS} !^on
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.+)/?$ redirect.php?id=$1&page=$2 [QSA,L]
So this second rule will work for an URL formed like this.
https://example.com/123/pagename

Redirect to HTTPS except 1 page

Been looking all over for an answer but no response found could solve my problem.
I'm using the following .htaccess file to redirect users from a site to its https version :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
# RewriteCond %{REQUEST_URI} !^/api-vop/
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e. http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I have first a rule to redirect to HTTPS then a rewrite rule for classic url rewriting to replace urls like myfile.php to /my-file
Thing is I need to let one url which is http://my-server.com/api-vop go through without being redirected to https, as seen by this line :
# RewriteCond %{REQUEST_URI} !^/api-vop/
Thing is I cannot get it to work, at best I'm trhown back to the index.php page.
Any solution anyone ?
Thanks in advance !
Keep your first rule as:
RewriteCond %{HTTPS} off
# This checks to make sure the connection is not already HTTPS
# RewriteCond %{THE_REQUEST} !\s/+api-vop/
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L,NE]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1

Codeigniter Wildcard Subdomain

How can I make a wildcard subdomain with codeigniter? I have try anything on google and this site. But it wont work, if not 500 error page it will return nothing (I mean no effect) because I got this on my .htaccess before :
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
</IfModule>
I think it always conflict with that one above, btw what I desired to be is :
example.com/page => example.com/page (without subdomain will just be normal, with no www and no index.php)
abc.example.com => example.com/abc-channel
exc.example.com => example.com/exc-channel
abc.example.com/my/foo/page => example.com/abc-channel/my/foo/page
All will generated with no redirect.

htaccess subdomain rewrite keep www

I've set up wildcard domains locally for testing on .dev
I'm trying to rewrite the following URL:
http://location.domain.dev/
to
http://www.domain.dev/site/location
I would like any requests with www in the subdomain to always go to www.domain.dev but if any request is made to location.domain.dev, I would like to keep that request in the address bar (i.e i dont want people to see the underlying change)
I currently have the following in my .htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.dev
RewriteRule ^(.*)$ http://domain.dev/site/%1 [QSA,NC]
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
Is this even possible?
You're pretty close. In order to not redirect the browser (causing the address bar to change) you need to get rid of the http://domain.dev part of the rewrite rule's target:
RewriteRule ^(.*)$ /site/%1/$1 [QSA,NC]
assuming that both *.domain.dev and www.domain.dev have the same document root. If they're different, you may have to enable mod_proxy and add a P flag so that the request gets proxied instead of redirecting the browser:
RewriteRule ^(.*)$ http://domain.dev/site/%1/$1 [QSA,NC,P]

Resources