0

I am trying to do two things here:

  1. redirect to a sub-folder

    redirect http://www.something.com/some/page.html or https://www.something.com/some/page.html to https://www.something.com/subfolder/some/page.html

  2. redirect http to https

    redirect http://www.something.com/subfolder/some/page.html to https://www.something.com/subfolder/some/page.html

And I want to do both of them in the same .htaccess file

I have been able to redirect to subfolder by the following code:

Options +FollowSymLinks -MultiViews
RewriteEngine on 
RewriteBase / 
RewriteCond %{HTTP_HOST} something.com [NC] 
RewriteRule ^(.*)$ https://www.something.com/subfolder/$1 [R=301,NC]

And then I am trying to do both of them; i.e. http to https(only if http request comes) and redirect to subfolder by the following code:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteCond %{HTTP_HOST} something.com [NC] 
RewriteRule ^(.*)$ https://www.something.com/subfolder/$1 [R=301,NC]

But it's not working. What am I doing wrong here?

EDIT

When using @starkeen's solution; i.e.

Options +FollowSymLinks -MultiViews
RewriteEngine on 
RewriteBase / 

RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ https://www.example.com/subfolder/$1 [R=301,NC,L]

I am expecting the following as result:

https://www.example.com/subfolder/brands/omega.html

when I give any of the following:

http://example.com/brands/omega.html OK
https://example.com/brands/omega.html OK

http://www.example.com/brands/omega.html OK
https://www.example.com/brands/omega.html OK

http://example.com/subfolder/brands/omega.html WRONG
http://www.example.com/subfolder/brands/omega.html WRONG

But the last two are redirecting to

https://www.example.com/subfolder/

Subrata
  • 2,216
  • 3
  • 24
  • 38
  • Here is an answer for redirection to https => http://stackoverflow.com/questions/13977851/htaccess-redirect-to-https-www and here for subfolder => http://stackoverflow.com/questions/990392/htaccess-rewrite-to-redirect-root-url-to-subdirectory – ADreNaLiNe-DJ Mar 15 '16 at 10:15
  • Do you want to redirect html files only or everything from root to sub? – Amit Verma Mar 15 '16 at 10:36
  • My redirection is working fine, what I need is to make both of them to work together in a single .htaccess file – Subrata Mar 15 '16 at 10:39
  • @starkeen everything from root to sub with the original parameters – Subrata Mar 15 '16 at 10:40

2 Answers2

1

Here is a rule to do both the tasks in a single rule:

RewriteEngine On

RewriteCond %{HTTPS} off [OR]
RewriteCond %{REQUEST_URI} !^/subfolder [NC]
RewriteRule ^(?:subfolder)?(/.*)?$ https://www.example.com/subfolder$1 [NE,L,R=302,NC]

Make sure to clear your browser cache before testing this rule.

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

Try :

Options +FollowSymLinks -MultiViews
RewriteEngine on 
RewriteBase / 
#--Http ==>https--#
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
#--exclude the destination to avoid redirect loop--#
RewriteCond %{REQUEST_URI} !^/subfolder
#--redirect /foo to /subfolder/foo--#
RewriteRule ^(.*)$ https://www.something.com/subfolder/$1 [R=301,NC,L]

Clear your browser'cache before testing this redirect.

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • http : // www.example.com/subfolder/some/page.html redirects to https : // www.example.com/subfolder/ – Subrata Mar 15 '16 at 12:09
  • http : // www.example.com/subfolder/some/page.html should redirect to https : // www.example.com/subfolder/some/page.html notice the change to https – Subrata Mar 15 '16 at 12:43
  • http://example.com/brands/omega.html OK https://example.com/brands/omega.html OK http://www.example.com/brands/omega.html OK https://www.example.com/brands/omega.html OK http://example.com/subfolder/brands/omega.html WRONG http://www.example.com/subfolder/brands/omega.html WRONG – Subrata Mar 15 '16 at 17:24
  • @Subrata what do you mean by "wrong"? Isnt the redirection working as expected? – Amit Verma Mar 15 '16 at 17:34
  • Check this in pastebin: http://pastebin.com/gQzhi81x In the last two cases it is redirecting to https : / / example.com/subfolder/ – Subrata Mar 15 '16 at 17:49