0

I have a codeignitor application running on Apache2, I have managed to remove the index.php from the urls with this .htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 

now I want to make certain parts of the site redirect to https, I tried this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 
RewriteRule ^/?cpanel/(.*) https://%{SERVER_NAME}/cpanel/$1 [R,L]
RewriteRule ^/?login/(.*) https://%{SERVER_NAME}/login/$1 [R,L]

But it doesn't work. I have to say when it comes to Apache rewrites im a noob. I can't find any tutorials on how to remove index.php and rewrite/redirect certain parts of the site to https.

Any ideas,

Thanks.

squareborg
  • 592
  • 3
  • 14
  • Also, I think you also have a bug in your regexps. Note that `https://example.com/cpanel` matches the cPanel regexp, which will redirect to `https://.../cpanel/cpanel`, which matches the regexp, etc. – nickgrim Oct 28 '12 at 19:23
  • @nickgrim Thanks, that was a cut and paste problem when doing the question the .htaccess is correct in that manner, edited to show this – squareborg Oct 28 '12 at 19:27
  • Ah, no, you missed my point. `http://.../cpanel/foo` will redirect (correctly) to `https://.../cpanel/foo`; however, *that* will then redirect to `https://.../cpanel/cpanel/foo`, etc. The problem is that your redirect-to-https rules should only run if it's not *already using* https. I think you need a `RewriteCond %{HTTPS} "off"` before each of those rules. – nickgrim Oct 28 '12 at 19:32
  • @nickgrim Nick you just nailed it man! nice one!. Working a treat. – squareborg Oct 28 '12 at 19:35

1 Answers1

0

The [L] means "last" i.e. stop processing further rules if this rule matches. I guess that CPanel uses "virtual" URLs (i.e. not pointing to Actual Files), and thus URLs like /cpanel/some-action-that-is-not-actually-a-file are getting caught by the first rule rather than the later ones.

Try moving the last two rules between RewriteEngine and your first RewriteCond ?

(EDIT: And also fix the redirect loop as noted in comments under the question)

nickgrim
  • 4,466
  • 1
  • 19
  • 28
  • Thanks just done as you said when I visit anything under /cpanel or /login I get "The page isn't redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete." the rest of the site works fine any ideas? – squareborg Oct 28 '12 at 19:18