Member Avatar for iamthwee

I have a doubt with my codeigniter .htaccess file. I was hoping there was something I was missing.

My question is I need a different .htaccess file for my live server and one for localhost. Locally, I'm using macos and linux, on my live server I am using centos with plesk(not sure if that matters).

The issue is I need a different htaccess file for the different environments. This seems stupid! As eventually I want to release my code so the end user doesn't have to manually edit the .htaccess files to get rid off the index.php in the url!

I must be doing something wrong as surely it must be standard here is what works on my local home server.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA] 

and below is my live production server

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

What gives?

I am using this for both production and development. The only difference with yours is the PassThrough

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

$0 or $1, don't really matter :).

Member Avatar for iamthwee

Hmm, what's the difference between rewriteEngine on and rewriteBase /?

I have a feeling this may be due to plesk and its default settings for php ini() allowing rewrites?

rewritebase is defining the base directory or "Sets the base URL for per-directory rewrites".

For example if I want to rewrite and redirect all request on my mydomain.com to /simplemvc/ directory, for the purpose of routing request and others, my .htaccess will be something like this

RewriteEngine On
    Options -Indexes

    RewriteBase /simplemvc/
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l

    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

the above will apply the rule in simplemvc directory. This one however has to be located inside the /simplemvc/ directory.

Another scenario is if the user wants ONLY ONe specific directory accessible to the public. Then all request for any files located in the /simplemvc/ directory and all the files that don't exists in the root can be routed like this from the root directory

RewriteEngine on
RewriteRule    ^$    simplemvc/   [L]
RewriteRule    (.*) simplemvc/$1    [L]

By implementing the above rewrite rule on the root directory, public access are always routed to /simplemvc/ directory for whatever controller/method requests.

By the ways if someone is reading this, they maybe wondering whatever happens to the

<IfModule mod_rewrite.c>

</IfModule>

its there, but I am just lazy typing them on this post.

you can cheat the viewers by making them to think that they are on your root directory eventhough they are like two direcotries down by elevating the working directory of the bootstrap.

something like this

//index file from /simplemvc/ directory conducting the controllers calls as if it is located in the in the root.

chdir(dirname(__DIR__));

include_once('/application/controllers/test.php/');

So, eventhough this file is located inside the /simplemvc/ directory is is being viewed by PHP as if it is in the root. Not to mention that the access through this page is being redirected by .htaccess.

Not so relevant but worth mentioning ( i think :)).

on my first .htaccess directives above, the one with a PT or passthrough. The reason I am using this type of directives is purely motivated by laziness (wow an excellent pasitive and negative contrast).

I am so darn lazy to keep on defining my routes on application/config/routes.php. A good friend of mine ( a coffee drinking buddy) had asked me to write a search controller and search model utilizing the codeigniter's active records. So, as lazy as I can be. I want the search query in shorthand uri form. Something like .. friendDomainDotCom/search/query/page_number. Most importantly he does not want the index.php? to be part of the url.

So, with the .htaccess directives above (PT), I can have an alias. and my route configuration will be one only using the generalized :any

$route['search/:any'] = 'search';

The above routing will cover all uri segments related to the search controller

domainDotCom/search/happy/3

which assigned the search controller to segment 0, the query is in segment 1, and the current page (3) being in the segment 2.

this

RewriteRule .* index.php/$0 [PT,L]

had helped me to be more lazy :).

people may asked, what will happen to the form processor on submit during search? Don't worry, I took care of it as a session variable, including the search result count (this can cause a double query). Those things should not be carried over when users requests for the next page.

By the way, my friends database that I mentioned here have more than 1.2 million rows at 2GB and they are hosting the site at mid-grade VPS hosting plan. I was amazed, I took screenshot. Never seen like that in my life.

On the side notes, I really tested and pushed CI to the limit. At 20 items per page with 348,000 search results containing images and descriptions, titles, tags, category and cross category names, CI's held its self pretty stable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.