Welcome Guest, Not a member yet? Register   Sign In
.htaccess remove index.php and redirect for SSL
#1

[eluser]CI Nick[/eluser]
Hello.

I already have a rewrite rule to remove index.php from my URL:

Code:
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
  ErrorDocument 404 /index.php
</IfModule>

However, I also want to redirect certain requests to the SSL(https) version of my site e.g. any request for the myaccount controller

Code:
http://www.mydomain.com/my-account/
http://www.mydomain.com/my-account/something/

I've found a few solutions on this forum (and the rest of the web) but I can't figure out how to make this work with my existing rewrite.

Any help greatly appreciated.

Thanks.
#2

[eluser]eggshape[/eluser]
You have to add more conditionals to your .htaccess file. Add something like this after your base - rewrite rules are accessed linearly (top down), so you want to do any redirects prior to your final rule:

Code:
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  RewriteCond %{REQUEST_URI} ^mydomain.com/my-account$ [NC]
  RewriteRule (.*) https://mydomain.com/my-account/$1 [R,L]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

That should remove the index.php plus redirect any requests for the my-account controller to https.
#3

[eluser]CI Nick[/eluser]
Hi eggshape.

Many thanks for the reply. Unfortunately, that doesn't seem to work? Is it because the index.php rule isn't being applied first? Basically, all the url's in my site need to have the index.php removed, but then I want any request for "my-account" to also be redirected to the https version.

I really can't get my head around mod_rewrite and I'm not exactly sure how the rules I already have work, but they just do. Can anyone point my in the direction of a good tutorial regarding this?

Many thanks,
Nick.
#4

[eluser]eggshape[/eluser]
Hi Nick - Sorry about that, I see a couple of problems with that previous rewrite condition; I think this will work if you're willing to try. Don't forget to replace "mydomain.com" with your site and "my-account" with the controller you want to match.

Replace the two lines I added with this:

Code:
RewriteRule ^my-account\/?(.+)?$ https://mydomain.com/index.php/my-account/$1 [NC,R,L]

The access rules use regular expressions. The above rule assumes that user is at domain "mydomain.com", or what ever your site domain is. It then applies a regexp rule: "^my-account\/?(.+)?$", which searches for a match of "my-account" followed by an optional slash and optional parameters (.+)? and then redirects to https://mydomain.com/index.php/my-account/$1

^ = beginning of search string
$ = end of search string
\ = backslash is used to escape a character
. = any character
+ = 1 or more occurrences
? = 0 or 1 occurences
$1 = whatever matched in (.+)
NC = any character-case
R = redirect
L = last rule (exit)

For tutorials, if you learn basic regular expressions and some directives like %{HTTP_HOST}, you can figure out how to modify htaccess rules.

Good luck!
#5

[eluser]Vik[/eluser]
An SSL newbie question: If you use an .htaccess file in this way, do you also need to install a complete copy of CI in the https folder on your web site? I guess the answer is yes, but I'd like to confirm this.
#6

[eluser]Vik[/eluser]
I think I found the answer. I bought an SSL certificate and installed it on my site. My web host, MediaLayer, uses a symbolic link so the same folder can be used for http and for https. So now, if I use https:// in the URL, the page is secured.
#7

[eluser]Bharani[/eluser]
dear where i write this code for remove the index.php..

that is ..into which file i write the above code




Theme © iAndrew 2016 - Forum software by © MyBB