Forum Moderators: phranque
RewriteEngine On
RewriteCond %{SERVER_PORT}!^443$ [OR]
RewriteCond %{HTTP_HOST}!^www\.domain\.com [NC]
RewriteRule ^(.*)$ https://www.domain.com/secure/$1 [R,L]
I goto http://www.domain.com/secure/ and it takes me to https://www.domain.com/secure/
However, when I go to http://www.domain.com/secure
It redirects me to https://www.domain.com/secure//home/username/public_html/secure
Also, when I goto http://www.domain.com/secure/anotherpage
It takes me to https://www.domain.com/secure/anotherpage
which is correct.
I basically want to make all URLs on the /secure folder to use https instead of http.
--
Also, I would like to make a condition, where when the address is http://www.domain.com/secure/another-page-thats-not-secure.php to not rewrite to https
This is just one page that needs to be unsecure.
.htaccess file inside the folder with the trailing slash. You will need an additional
.htaccess file outside the folder /secure/ to catch requests for that folder without the trailling slash, as without the trailing slash the web server will probably not look inside that folder at all. Hope this is clear... Otherwise i'll try an example:
Requests for
example.com/secure/
example.com/secure/something
example.com/secure/something-else
- etc.
will be catched by the
.htaccess file inside the folder "/secure/". A request like this, without the trailing slash: example.com/secure
- is not a request for the folder, but for the filename "secure" which is something else. As that filename does not exist Apache tries to find the closest match, which is the long path you have seen.
So to rewrite from A to B:
A) example.com/secure
B) example.com/secure/
- you will need an
.htaccess file placed one level up from the folder "/secure/", like this: example.com/.htaccess
What code should I use in the .htaccess file if it is going to be placed outside the /secure folder?
Also, I would like to make a condition, where when the address is [domain.com...] to not rewrite to https
This is just one page that needs to be unsecure.
I think this one-liner will do the trick, but do test it:
-----------------------------------------
RewriteRule ^secure$ [domain.com...] [R=301,L]
>> not secure
You current rule should be modified like this, but do test:
-----------------------------------------
RewriteEngine On
RewriteCond %{SERVER_PORT}!^443$ [OR]
RewriteCond %{HTTP_HOST}!^www\.domain\.com [NC]
RewriteRule ^([^secure/another-page-thats-not-secure.php])$ [domain.com...] [R=301,L]
Another way of writing it is this, perhaps that syntax is easier, plus it will allow you to exclude more than one URL if you should get the need:
-----------------------------------------
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/secure/another-page-thats-not-secure.php
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
Two URL's excluded:
-----------------------------------------
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/secure/another-page-thats-not-secure.php [OR]
RewriteCond %{REQUEST_URI} !^/secure/yet-another-page-thats-not-secure.php
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
I took the liberty of changing your R flags to R=301, that way the browser address bar will also display the change.
Ie:
http //www.example.com
- will not get forwarded to
httpS //www.example.com
(unless it's on port 443 of course, but perhaps all traffic will be that?)
[edited by: claus at 2:09 am (utc) on April 17, 2005]
I want all traffic [domain.com...] and [domain.com...] to be treated as [domain.com...]
Let me re-ask my question, and give this example of what I want done, I think this will be the easiest way to accomplish what I want.
When you access [domain.com...] , you should be directed to [domain.com...]
When you access [domain.com...]
You should be redirected to [domain.com...]
When you access [domain.com...] you should not be redirected to a secure connection.
Also, there is one other folder that needs to be secure. So when you access [domain.com...] it should redirect you to [domain.com...]
Hope this helps explain what I am looking for.
1) In
www.example.com/.htaccess: ----------------------------------------
RewriteEngine On
RewriteRule ^secure$ [domain.com...] [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
----------------------------------------
1) In
www.example.com/secure/.htaccess: ----------------------------------------
RewriteCond %{REQUEST_URI} !^/secure/another-page-thats-not-secure.php
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
----------------------------------------
Remember that there should be a space before each "!" - this forum eats them sometimes. Hope this will work :)
Here is what happens now.
[domain.com...] > [domain.com...]
- Meaning this works -
[domain.com...] > [domain.com...]
- Meaning this works -
[domain.com...] > [domain.com...]
- Meaning this does not work -
[domain.com...] > [domain.com...]
- Meaning this works -
So the only issue is, when you goto secure/ with a trailing slash, you are not re-directed to the secure URL.
Here is what I am using now:
------------------------------------
public_html/.htaccess
------------------------------------
RewriteEngine On
RewriteRule ^secure-folder1$ [domain.com...] [R=301,L]
RewriteRule ^secure-folder2$ [domain.com...] [R=301,L]
RewriteCond %{HTTP_HOST}!^www\.domain\.com [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
------------------------------------
public_html/secure-folder1/.htaccess
------------------------------------
RewriteEngine On
RewriteOptions inherit
RewriteCond %{REQUEST_URI}!^/secure-folder1/another-folder/non-secure-page.php
RewriteCond %{SERVER_PORT}!^443$ [OR]
RewriteCond %{HTTP_HOST}!^www\.domain\.com [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
------------------------------------
public_html/secure-folder2/.htaccess
------------------------------------
RewriteEngine On
RewriteOptions inherit
RewriteCond %{SERVER_PORT}!^443$ [OR]
RewriteCond %{HTTP_HOST}!^www\.domain\.com [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
<Files .htaccess>
order allow,deny
deny from all
</Files>
DirectoryIndex index.php
Options -Indexes
php_value "magic_quotes_gpc" "1"
php_value "register_globals" "1"
php_value "error_reporting" "2039"
-------------------------------------
-------------------------------------
Here are the tests:
[domain.com...] > [domain.com...]
[domain.com...] > [domain.com...]
-------------------------------------------
[domain.com...] > [domain.com...]
[domain.com...] > [domain.com...]
[domain.com...] > [domain.com...]
--------------------------------------------
[domain.com...] > [domain.com...]
[domain.com...] > [domain.com...]
--------------------------------------------
I just double checked everything.
--- Btw, there are spaces bwteeen the } and the!
--- If it makes things easier, I can send you the URL via PM/Email and you can take a look at what happens.
(1) Try this in
/.htaccess (almost the same as now): ------------------------------------------
#turn on rewrite engine
RewriteEngine On
#if there's no trailing slash, rewrite it
RewriteRule ^secure-folder1$ [domain.com...] [R=301,L]
RewriteRule ^secure-folder2$ [domain.com...] [R=301,L]
#if there's a host field, AND
RewriteCond %{HTTP_HOST} .
#it does not have www prefix
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
#rewrite it
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
------------------------------------------
(2) And this in
/secure/.htaccess ------------------------------------------
#inherit rewrite options from folder above
RewriteOptions inherit
#put www prefix on non-secure url
RewriteCond %{REQUEST_URI} /secure-folder1/another-folder/non-secure-page.php [NC]
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
# if access is not SSL
<IfModule !mod_ssl.c>
# if it's not the non-secure url, AND
RewriteCond %{REQUEST_URI} !^/secure-folder1/another-folder/non-secure-page.php [NC]
#if there's a host field, AND
RewriteCond %{HTTP_HOST} .
#if it hasn't got www prefix
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
#rewrite it
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
</IfModule>
------------------------------------------
In (2) i stripped the port condition, i'm not sure it's required. If it is it can just be re-inserted. In (1) the dot in the host field is a tip i got from Jim, it makes the rule work only for user agents that do provide the host field so you don't get loops.
(NOTE: Rules have been edited since first post and once more since first edit)
Now the secure-folder1 doesn't rewrite to https no matter how you go their.
[domain.com...] > [domain.com...]
Everything else doesn't work.
Then take the next step in making it so that the one page that remains unsecure, stays unsecure.
anyway, in msg #10 you had most of it set up right, it seems. What you need could be as easy as change these two in
/.htaccess: -----------------------------
RewriteRule ^secure-folder1$ [domain.com...] [R=301,L]
RewriteRule ^secure-folder2$ [domain.com...] [R=301,L]
-----------------------------
Here's a suggestion for the three
.htaccess files: -----------------------------
www.example.com/.htaccess
-----------------------------
#turn on the engine
RewriteEngine On
#first, correct the www if necessary
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ [example.com...] [R=301]
#then, rewrite paths for secure folder 1
RewriteCond %{REQUEST_URI} !^/secure-folder1/another-folder/non-secure-page.php [NC]
RewriteCond %{REQUEST_URI} ^/secure-folder1 [NC]
RewriteCond %{REQUEST_URI} !^/secure-folder1/ [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
#then, rewrite paths for secure folder 2
RewriteCond %{REQUEST_URI} ^/secure-folder2 [NC]
RewriteCond %{REQUEST_URI} !^/secure-folder2/ [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
-----------------------------
-----------------------------
-----------------------------
www.example.com/secure-folder1/.htaccess
-----------------------------
(no content necessary - just delete it)
-----------------------------
-----------------------------
-----------------------------
www.example.com/secure-folder2/.htaccess
-----------------------------
RewriteOptions inherit
<Files .htaccess>
order allow,deny
deny from all
</Files>
DirectoryIndex index.php
Options -Indexes
php_value "magic_quotes_gpc" "1"
php_value "register_globals" "1"
php_value "error_reporting" "2039"
-----------------------------
- i sure would think this should do it all. Then again, i thought so above as well :)
[domain.com...] > [domain.com...]
[domain.com...] > [domain.com...]
[domain.com...] > [domain.com...]
[domain.com...] > [domain.com...]
I am going to go put this on another domain, and I will post the details for that domain, that way we can list actual domains -_-
-----------------------------
www.example.com/.htaccess
-----------------------------
#turn on the engine
RewriteEngine On
#first, correct the www if necessary
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ [example.com...] [R=301]
#then, rewrite secure folder 1
RewriteCond %{REQUEST_URI} !^/secure-folder1/another-folder/non-secure-page.php [NC]
RewriteCond %{REQUEST_URI} ^/secure-folder1 [NC]
RewriteCond %{HTTPS} !on [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
#then, rewrite secure folder 2
RewriteCond %{REQUEST_URI} ^/secure-folder2 [NC]
RewriteCond %{HTTPS} !on [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
#then, rewrite slashes for secure folder 1
RewriteCond %{REQUEST_URI} ^/secure-folder1 [NC]
RewriteCond %{REQUEST_URI} !^/secure-folder1/ [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
#then, rewrite slashes for secure folder 2
RewriteCond %{REQUEST_URI} ^/secure-folder2 [NC]
RewriteCond %{REQUEST_URI} !^/secure-folder2/ [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
-----------------------------
[/pre]
I found RewriteCond %{HTTPS} [apache2docs.paradoxical.co.uk] - i think that should take care of the things taht were not missing.
-----------------------------
www.example.com/.htaccess
-----------------------------
#turn on the engine
RewriteEngine On
#first, correct the www if necessary
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ [example.com...] [R=301]
#then, rewrite secure folder 1
RewriteCond %{REQUEST_URI} !^/secure-folder1/another-folder/non-secure-page.php [NC]
RewriteCond %{REQUEST_URI} ^/secure-folder1 [NC]
RewriteCond %{HTTPS} !on [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
#then, rewrite secure folder 2
RewriteCond %{REQUEST_URI} ^/secure-folder2 [NC]
RewriteCond %{HTTPS} !on [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
#then, rewrite slashes for secure folder 1
RewriteCond %{REQUEST_URI} ^/secure-folder1 [NC]
RewriteCond %{REQUEST_URI} !^/secure-folder1/ [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
#then, rewrite slashes for secure folder 2
RewriteCond %{REQUEST_URI} ^/secure-folder2 [NC]
RewriteCond %{REQUEST_URI} !^/secure-folder2/ [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
-----------------------------
I found RewriteCond %{HTTPS} [apache2docs.paradoxical.co.uk] - i think that should take care of the things taht were not missing.
-----------------------------
www.example.com/.htaccess
-----------------------------
#turn on the engine
RewriteEngine On
#first, correct the www if necessary
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ [example.com...] [R=301]
#then, rewrite secure folder 1
RewriteCond %{REQUEST_URI} !^/secure-folder1/another-folder/non-secure-page.php [NC]
RewriteCond %{REQUEST_URI} ^/secure-folder1 [NC]
RewriteCond %{HTTPS} !on [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
#then, rewrite secure folder 2
RewriteCond %{REQUEST_URI} ^/secure-folder2 [NC]
RewriteCond %{HTTPS} !on [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
#then, rewrite slashes for secure folder 1
RewriteCond %{REQUEST_URI} ^/secure-folder1 [NC]
RewriteCond %{REQUEST_URI} !^/secure-folder1/ [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
#then, rewrite slashes for secure folder 2
RewriteCond %{REQUEST_URI} ^/secure-folder2 [NC]
RewriteCond %{REQUEST_URI} !^/secure-folder2/ [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]
-----------------------------
I found RewriteCond %{HTTPS} [apache2docs.paradoxical.co.uk] - i think that one should take care of the things that were not working.
accounttest.info/cpanel
u/p [] accountt/password
These are what should be tested I believe:
[accounttest.info...] [should add ww before domain]
[accounttest.info...] [should stay http]
[accounttest.info...] [should goto https ]
[accounttest.info...] [should goto https ]
[accounttest.info...] [should stay http]
[accounttest.info...] [should goto https ]
[accounttest.info...] [should goto https ]
---------------------------
public html / .htaccess
---------------------------
#turn on the engine
RewriteEngine On
#first, correct the www if necessary
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.accounttest\.info [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301]
#then, rewrite paths for helpdesk
RewriteCond %{REQUEST_URI}!^/helpdesk/admin/parser.php [NC]
RewriteCond %{REQUEST_URI} ^/helpdesk [NC]
RewriteCond %{REQUEST_URI}!^/helpdesk/ [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
#then, rewrite paths for modernbill
RewriteCond %{REQUEST_URI} ^/modernbill [NC]
RewriteCond %{REQUEST_URI}!^/modernbill/ [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
---------------------------
public_html / helpdesk / .htaccess
---------------------------
--nothing--
---------------------------
public html / modernbill/ .htaccess
---------------------------
RewriteOptions inherit
------------------------------------------------
Using the above code, here is the outcome for the tests:
[accounttest.info...] > [accounttest.info...]
[accounttest.info...] > [accounttest.info...]
[accounttest.info...] > [Error: connection refused]
[accounttest.info...] > [accounttest.info...]
[accounttest.info...] > [accounttest.info...]
[accounttest.info...] > [accounttest.info...]
[accounttest.info...] > [accounttest.info...]
------------------------------------------------
#turn on the engine
RewriteEngine On
#first, correct the www if necessary
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.accounttest\.info [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301]
#then, rewrite secure folder 1
RewriteCond %{REQUEST_URI}!^/helpdesk/admin/parser.php [NC]
RewriteCond %{REQUEST_URI} ^/helpdesk [NC]
RewriteCond %{HTTPS}!on [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
#then, rewrite secure folder 2
RewriteCond %{REQUEST_URI} ^/modernbill [NC]
RewriteCond %{HTTPS}!on [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
#then, rewrite slashes for secure folder 1
RewriteCond %{REQUEST_URI} ^/helpdesk [NC]
RewriteCond %{REQUEST_URI}!^/helpdesk/ [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
#then, rewrite slashes for secure folder 2
RewriteCond %{REQUEST_URI} ^/modernbill [NC]
RewriteCond %{REQUEST_URI}!^/modernbill/ [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
-----
Ok, I get a ton of connection refused.
Maybe I messed up on the spacing a bit?
[accounttest.info...]
[domain.com...] > [domain.com...]
[domain.com...] > [domain.com...]
------------------------
#turn on the engine
RewriteEngine On
#first, correct the www if necessary
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.accounttest\.info [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301]
#then, rewrite secure folder 1
RewriteCond %{REQUEST_URI}!^/helpdesk/admin/parser.php [NC]
RewriteCond %{REQUEST_URI} ^/helpdesk [NC]
RewriteCond %{SERVER_PORT}!^443$
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
#then, rewrite secure folder 2
RewriteCond %{REQUEST_URI} ^/modernbill [NC]
RewriteCond %{SERVER_PORT}!^443$
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
#then, rewrite slashes for secure folder 1
RewriteCond %{REQUEST_URI} ^/helpdesk [NC]
RewriteCond %{REQUEST_URI}!^/helpdesk/ [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
#then, rewrite slashes for secure folder 2
RewriteCond %{REQUEST_URI} ^/modernbill [NC]
RewriteCond %{REQUEST_URI}!^/modernbill/ [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
Like this?
#turn on the engine
RewriteEngine On
#first, correct the www if necessary
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.accounttest\.info [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301]
#then, rewrite secure folder 1
RewriteCond %{REQUEST_URI}!^/helpdesk/admin/parser.php [NC]
RewriteCond %{REQUEST_URI} ^/helpdesk [NC]
RewriteCond %{SERVER_PORT}!^443$
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
#then, rewrite secure folder 2
RewriteCond %{REQUEST_URI} ^/modernbill [NC]
RewriteCond %{SERVER_PORT}!^443$
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
#then, rewrite slashes for secure folder 1
RewriteCond %{REQUEST_URI} ^/helpdesk [NC]
RewriteCond %{REQUEST_URI}!^/helpdesk/ [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
#then, rewrite slashes for secure folder 2
RewriteCond %{REQUEST_URI} ^/modernbill [NC]
RewriteCond %{REQUEST_URI}!^/modernbill/ [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
but doesn't loop when I goto [domain.com...]
It also does not add the https..
Okay, i can see there might be a loop. Try adding a slash to the end of these two conditions, as shown below (i've included the comment so that you can see which ones). That way the URL without the slash will only get caught by the "slash" rules at the end:
---------------------------------------
#then, rewrite secure folder 1
RewriteCond %{REQUEST_URI} !^/helpdesk/admin/parser.php [NC]
RewriteCond %{REQUEST_URI} ^/helpdesk/ [NC]
(...)
#then, rewrite secure folder 2
RewriteCond %{REQUEST_URI} ^/modernbill/ [NC]
---------------------------------------
I really, really hope that this will do it...
[domain.com...] > [domain.com...]
[domain.com...] > [domain.com...]
Same thing with the ModernBill directory.
I actually think that I can get around having to have the /helpdesk/admin/parser.php page unsecure. So if it makes things easier, we can remove that.
----------------------------------------
#turn on the engine
RewriteEngine On
#first, correct the www if necessary
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.accounttest\.info [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301]
#then, rewrite secure folder 1
RewriteCond %{REQUEST_URI} !^/helpdesk/admin/parser.php [NC]
RewriteCond %{REQUEST_URI} ^/helpdesk/ [NC]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
#then, rewrite secure folder 2
RewriteCond %{REQUEST_URI} ^/modernbill/ [NC]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
#then, rewrite slashes for secure folder 1
RewriteCond %{REQUEST_URI} ^/helpdesk [NC]
RewriteCond %{REQUEST_URI} !^/helpdesk/ [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
#then, rewrite slashes for secure folder 2
RewriteCond %{REQUEST_URI} ^/modernbill [NC]
RewriteCond %{REQUEST_URI} !^/modernbill/ [NC]
RewriteRule ^(.*)$ [accounttest.info...] [R=301,L]
----------------------------------------
Now when I goto [domain.com...] it takes me to [domain.com...]
But when I goto [domain.com...] it loops (and gives redirection error).