codeigniter, force ssl on a particular page - codeigniter

I'm having hard time trying to force https (ssl) to particular page on codeiginiter framework. I tried many ways, but none worked. The only way worked for me is changing the $config['base_url'] site link to begin with https instead of http. The result was that all links were set to ssl (the entire site), which is none sense because I don't need to use the SSL everywhere. I used some php code in the login page, ut that made some troubles so I gave it up.
I want to know whether this is a good method how to do that, any idea?
Thanks,

You need add Condition on .htaccess to make use SSL port work only for selected urls.
Here is example, how to do
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} controller/function
RewriteRule ^(.*)$ https://www.yourdomain.com/controller/function[R=301,L]
RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} controller/function
RewriteRule ^(.*)$ https://www.yourdomain.com/controller/function[R=301,L]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
P.S : Your base_url must be set to “/” in your config file.
For more information check http://codeigniter.com/wiki/SSL_Handling

Related

htaccess to remove index.php, with multiple domains

This seems so basic, and there's a plethora of questions regarding .htaccess online, but after two days of research, I still can't make mine work the way I want.
What I want is:
Force https on all requests
Always use the "www" version of the url.
Work on multiple domains (but not redirect them all to a master domain). All my domains point to the same folder (so they'd use the same codebase), and in its root is the .htaccess file.
Remove the "index.php" part of the url, to make it human and SEO friendly.
This is what I have so far:
Start with the basic .htaccess code for CodeIgniter, as shown in the userguide:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
Originally, the last line had the [L] flag, but I omitted it, so it will continue to the following rules.
(Am I correct in assuming that it takes the url output in the previous RewriteRule, and perform the following matches on it?)
# for non www urls, add www and force https:
RewriteCond %{HTTP_HOST}(.*) !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R,L]
# for www urls, just force https:
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
The above code is achieving tasks 1-3 of my list above, but the index.php is still showing in the address bar.
How do I remove it?
Well, I (partially) gave up on .htaccess, and solved my problem in a different way:
I'm now using CodeIgniter hook to deal with forcing https, and leave htaccess to deal only with forcing www and removing index.php
So I removed the last line (RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]), and added an ssl hook to my application, and all is working now.
The hook function, in case anyone is interested, is this:
function force_ssl()
{
if ($_SERVER['HTTP_HOST']!='localhost' && $_SERVER['HTTP_HOST']!='10.0.2.2')
{
$CI =& get_instance();
$CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
if ($_SERVER['SERVER_PORT'] != 443) redirect($CI->uri->uri_string());
}
}

CodeIgniter and specific rewrite rule

On my CodeIgniter site, I would like to add a specific rewrite rule, so that this url
http://www.exemple.com/cache.manifest
would rewrite to
http://www.exemple.com/controller/manifest
(because Safari 7 seems to only accept .manifest files for ApplicationCache)
So I try to add this line to my htaccess
RewriteRule ^cache.manifest$ controller/manifest
I added it before the other rewrite rules :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^cache.manifest$ controller/manifest
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
But it returns a 404. If I change the line to
RewriteRule ^cache.manifest$ test.html
it works. So the first part on my rule is correct.
If I try to access directly to www.example.com/controller/manifest, it works to, so my url is correct.
I tried also
RewriteRule ^cache.manifest$ index.php/controller/manifest [L]
But it doesn't work either…
Any clue ?
Thanks a lot
I tried some tests on my local server and I think the following might work:
RewriteRule ^cache.manifest$ /index.php/controller/manifest [R,L]
I am not entirely sure if you need the leading "/" or "/index.php", so you
may need to experiment.
You need the [R] flag to force a redirect. In this situation, you want Apache
to look for the string cache.manifest in the URL, and then go to the CI page
controller/manifest.
It appears that you need to explicitly set the redirect.
Please let me know if this works. Good luck!

mod_rewrite forward shortend URL

I am looking for a way to create a short URL path for a longer URL on my page
the long url is: domain.com/tagcloud/user.html?t=1234ABCD
i would like to offer a short version of the URL to easy access it:
domain.com/t/1234ABCD
I tried a few examples but I just don't get it how I could forward these rules.
RewriteRule ^(.*)/t/$ /tagcloud/user.html?t=$1 [L]
I am also using MODX so they already use rules.
in addition my htaccess file
RewriteEngine On
RewriteBase /
# Always use www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I must keep the code snippets above in my htaccess file. The first one simply forwards http://domain.com requests to www.domain.com
The friendly URLs part is needed to translate the internal IDs of my CMS with the alias of the URL. This feature must remain because the entire site cannot be influencted by the changes I try to make in htaccess...
I simply would like to add a listener that only if the URL matches www.domain.com/t/abcd1234
Therefore I need something that identifies the www.domain.com/t/ URL
your help is much appreciated
Try this:
RewriteCond %{REQUEST_URI} ^/t/.*
RewriteRule ^t/(.*)$ /tagcloud/user.html?t=$1 [R=301,L]

localhost directs to www.domains.tld after adding .htaccess in codeigniter folder

I just started using Codeigniter to develop a simple static website using a local server (MAMP). Initially my local address to access my homepage was http://localhost/index.php/home. Even a simple localhost would redirect to my homepage. I wanted to remove the 'index.php' from the URL and hence I copy pasted the .htaccess code which I found online. The code looked like the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce www
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
#RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
#RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]
# Enforce NO www
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^(.*)$ http://localhost/$1 [L,R=301]
###
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
Initially I copy-pasted the code without changing it at all (which was foolish on my part) So the 'localhost' in the code above was initially 'www.domains.tld'. When I then ran localhost on my browser, it directed to www.domains.tld'. I noticed the blunder and changed it to what it is above and localhost still directs to 'www.domains.tld' I deleted the .htaccess file to reverse the effect but it still does the same thing.
I also changed my root folder for localhost but whatever I do localhost points to 'domains.tld'. When I type 127.0.0.1 on the address bar of my browser, it directs correctly to my website. I have spent hours reading up on the reason for this behavior but am unsuccessful to find a solution.
Any help will be greatly appreciated.
Thanks,
H.
Clear your DNS cache and restart your browser. Certain browsers cache redirects with DNS so that you end up at what it thinks is the correct site more quickly.
Firefox is particularly annoying with this, so I disable its internal DNS caching every time I install. In about:config, create the following two integer type settings (NOTE: you MUST create these, they do not exist by default):
network.dnsCacheEntries set to 0
network.dnsCacheExpiration set to 0
In Chrome, you would need to turn off DNS prefetching under your Privacy settings.
The reason they do this is to make the internet "seem" faster to casual browsers. For developers, it can be quite a hindrance.

301 Redirect Dynamic HOST includes DOCUMENT_ROOT

Based on what I read on Apache I used the following example they provided to do a 301 Redirect on all my web sites.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
This is not quite working as they said it would. If I try www.domain.com it works. If I try domain.com I get www.domain.com//home/www/public_html/www.domain.com
Looks like it wants to include the DOCUMENT_ROOT in the redirect. Am I better off to create an individual .htaccess for each web site?
What is faster to run - Apache or HTACCESS?
Try this instead. Make sure you include the RewriteBase /
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If you still get the old result, your previous 301 redirect is probably cached, retest in Private (Incognito) Browsing Mode.
Using Apaches httpd.conf is faster since accessing the .htaccess file adds a small overhead—Apache checks every directory—and parent directory—for the file and it will be loaded for every request.
Using the httpd.conf is better when you have access to it. Use .htaccess if you don't have access to the main configuration file.

Resources