CodeIgniter Forums

Full Version: [SOLVED] .htaccess redirect from https to http shows the "index.php?"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Joakim_[/eluser]
Hi.

I trying to redirect all https to http, except one auth controller.

The problem is that the "index.php?" shows up when doing a redirect.

So if you enter:

https://example.com/test/

You are redirected to http but the address will look like this:

http://example.com/index.php?/test

The .htaccess file looks like this:

Code:
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (auth)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(auth)
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301]

Any help are very appreciated! Smile

Thanks!

El Forum

[eluser]PowerCode[/eluser]
In your application/config/config.php, change $config['index_page'] = "index.php?"; into $config['index_page'] = "";.

EDIT: I just noticed that you're doing a redirect from .htaccess itself, so above solution won't work. Lemme have a closer look at the problem...

EDIT 2:

Code:
RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (auth)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(auth)
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1

I think you'll need to change the /index.php?/$1 to /$1 as above. This is because once the redirect has been done from or to HTTPS/HTTP, the rule to rewrite everything to /index.php?/$1 would be executed regardless.

On a side note, I've just changed the ordering of the rewrite rule above too, since I think it looks more logical (I believe rewrite rules are traversed in order they've mentioned). Though it might not make any difference.

El Forum

[eluser]Joakim_[/eluser]
Thank you very much for your reply PowerCode.

I changed to:

Code:
RewriteRule ^(.*)$ /$1

It now shows a "Internal Server Error" on all pages but the redirection from https http don't show the index.php no more.

The "RewriteRule ^(.*)$ /index.php?/$1" code is also used to remove the index.php from all pages. Do you think that might be the problem?

Thank you so much!

El Forum

[eluser]PowerCode[/eluser]
Oops, I'm really sorry. You actually need the index.php or index.php? (as required by some hosts). So your previous query was right. That's the reason for the internal server error. So you need to change it back to RewriteRule ^(.*)$ /index.php?/$1. My bad.

Just make sure you've set $config['index_page'] I mentioned above to "", though it may not be the problem.

Anyways, I just did a small test,

Code:
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1

RewriteCond %{REQUEST_URI} (auth)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301]

I had this in a folder called HTTPS, and thus tried http://localhost/HTTPS/auth, which matched the second condition. Now instead of redirecting to https://localhost/HTTPS/auth, it redirects to https://localhost/HTTPS/auth?/auth. I don't know what the problem is Sad.

El Forum

[eluser]Phil Sturgeon[/eluser]
Edit: Post removed due to the content of the message being entirely useless.

El Forum

[eluser]Joakim_[/eluser]
[quote author="PowerCode" date="1230634122"]Oops, I'm really sorry. You actually need the index.php or index.php? (as required by some hosts). So your previous query was right. That's the reason for the internal server error. So you need to change it back to RewriteRule ^(.*)$ /index.php?/$1. My bad.

Just make sure you've set $config['index_page'] I mentioned above to "", though it may not be the problem.

Anyways, I just did a small test,

Code:
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1

RewriteCond %{REQUEST_URI} (auth)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301]

I had this in a folder called HTTPS, and thus tried http://localhost/HTTPS/auth, which matched the second condition. Now instead of redirecting to https://localhost/HTTPS/auth, it redirects to https://localhost/HTTPS/auth?/auth. I don't know what the problem is Sad.[/quote]

Thank you very much for all your help!

The strange thing is that it used to work when I had the CI installation in a folder like /ci/. When I moved it to root and combined the .htaccess files from root and /ci/ it didn't work no more. Before, the .htaccess code to remove the index.php? was in a separate .htaccess file in the /ci/ directory and the rest was in the root .htaccess file.

Like this:

/.htaccess (root)

Code:
RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (auth)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(auth)
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301]

/ci/.htaccess

Code:
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /ci/index.php?/$1


Thanks! Smile

El Forum

[eluser]Joakim_[/eluser]
Anyone? Smile

El Forum

[eluser]Joakim_[/eluser]
Okey I got it working now by moving the system to a new folder in root named "ci" and placed the .htaccess to remove index.php in that directory.

The folder is rewritten as root with this .htaccess code:

Code:
RewriteRule ^(.*)$ /ci/$1