CodeIgniter Backend Frontend .htaccess router - apache

I'm trying to develop a little base CMS with CodeIgniter for my own use on projects but got stuck on this. Also, I'm very new to CI, but got some years with ZF and OOP PHP.
First of all let me show you my file structure:
index.php (frontend bootstrap)
backend.php (backend bootstrap)
.htaccess
system ( CI core )
application
backend
[...] MVC related files and folders (config, controllers, models, views...)
frontend
[...] MVC related files and folders (config, controllers, models, views...)
codeigniter
[...] (cache, database, scaffolding...)
Ok. I can get to work the index.php or backend.php routing with an .htaccess, but can't get it to work with both. Here's the .htaccess code:
RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php (and throwing a 404 error)
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
Well, what I need is the following:
www.domain.com/backend/controller/action/parameters (backend.php routing)
www.domain.com/controller/action/parameters (index.php routing)
Hope I explained well.
Can anyone help, please? :)
Best regards.

After some more searching, I've found a very nice article documenting how to do what I need. It also explains the 3 ways to backend/frontend in CI:
separate applications
sub-directories
HMVC (Hierarchical Model View Controller)
HMVC fits my needs perfectly, but i'll give a try to sub-directories first. :)

Basically you want to run two CodeIgniter applications side by side, one in the root and one in backend/. There are a few ways to do this, but the simplest is to add a rewrite rule for your second instance (removing the Last flag):
RewriteRule ^backend/(.*)$ /backend/index.php/$1 [QSA]
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

Related

Setting up .htaccess with apache and laravel

This seems to have been asked hundreds of times, and I read it and tried different things. Nothing seems to work, so after reading about 5 google pages about this I gave up and I want to see if anyone can give me an answer.
I want to remove the /public from my URL
I'm using an apache 2.4 server on windows. I just installed laravel.
I made sure the rewrite module is on in apache (php's get_apache_modules() shows it) and now I'm trying to write the .htaccess - and I can't understand how it works.
I don't want to change anything in my apache configurations (I'm using this machine to develop multiple apps)
my current .htaccess files are :
in the app route directory
Options -MultiViews
RewriteEngine On
RewriteRule ^ public/index.php [L]
and in the public folder
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /texteditor/public/index.php [L]
Right now localhost/texteditor/ - and anything after it that isn't public/ gives me 404
Also can anyone recommend an easy .htaccess tutorial for dummies?
I have done this thing by doing following and working fine.
Please do this if it is helpful to you.
Move public/index.php to www/texteditor/index.php.
also Move public/.htaccess to www/texteditor/.htaccess.
then you can do it.
Please replace your app root directory's .htaccess with public/.htaccess.

htaccess codeigniter mod-rewrite, rewritecond to ignore subdirectory within main app directory

I have an install of codeigniter up, running and all is good. Except I want to now offer my users a means of uploading images. The only place I could think to put the folder was in the applications directory (aptly renamed to "app" in my case). Anyway. Since its an MVC I need to run rewrite conditions all over the place. And I believe this to be one of the scenarios.
What I need is to have a condition that works for this subfolder within the app folder
my original rewrite condition is
RewriteCond $1 !^(index\.php|static|robots\.txt)
which works great for those but not for this one case so I need a new condition that works with the above but supports app/upload (is this even possible)?
Life's a lot easier if you keep your assets outside of application/ (as Robert mentioned).
There are a ton of good asset management Sparks you can pick from to assist with this too - I authored one for my CI projects called sk-asset which provides a lot of view helpers & is structured:
/CIAppRoot
assets/
css/
download/
img/
js/
less/
swf/
upload/
xml/
application/
system/
... htaccess settings can simply bypass CI routing for existing files/dirs/etc:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# block hidden directories
RewriteRule "(^|/)\." - [F]
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
</IfModule>
The upload folder does not have to be in your application directory. In fact, it's better if it's not in it. This way you can separate the application from the content (uploads).
You also don't need to rewrite the conditions if you do it this way.
This is how my CI directory looks like:
/application
/assets
/uploads
/system

Invisibly rewriting root to subdirectory using mod_rewrite (ajaxplorer)

I've been looking on various sites reading mod_rewrite examples for a few hours, and have used it many times before successfully... but I'm trying something new with it now and can't for the life of me get it working!
I'm using a PHP file manager tool called AjaXplorer, I have it in a subdirectory on the root of my server /ajaxplorer-core-4.0.4. When I go to the root of my site http://domain.com/ I want it to invisibly redirect to the /ajaxplorer-core-4.0.4 folder (but still show the root domain in the address bar).
I still want to be able to access the other files/directories on the root as normal just typing in the path.
I assume this is possible? Seems relatively simple but I just can't get it working.
AjaXplorer seems to load js files and images etc from /ajaxplorer-core-4.0.4/plugins, I have a feeling that's where it's tripping me up.
Any pointers would be massively appreciated! Thanks
Found it on another answer luckily :)
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteRule ^(/)?$ subdir/index.php [L]
Here's the wiki of serverfault.com
The howto's htaccess official guide
The official mod_rewrite guide
And if that's not enough:
Two hints:
If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

.htaccess mod_rewrite subdirectory to URL parameter

I hope this was not asked over and over again before, but I didn't get further to an answer using google, w3schools and so on. So here is my question:
I'm writing a script that creates kind of an index of projects that I have on my homepage and makes a nice list with images and teaser text based on an info file. I mostly have my projects on github and the readme is in markdown so I thought I could dynamically generate the HTML from the markdown of the latest blob from github on demand using PHP so it gets updated automatically.
My directory structure looks like this:
projects
project1
.remoteindex
.info
project2
.remoteindex
.info
index.php
.htaccess
So when just domain.tld/projects/ is requested I get the info from .info and make a nice index of all projects. But if domain.tld/projects/project1/ is request it, I want to internally redirect it to domain.tld/projects/?dir=project1 to do my markdown parsing and so on. But domain.tld/projects/project1/image.png should not be redirected.
This is what I tried:
.htaccess
RewriteEngine on
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [R,L]
I made it redirect instead of rewrite so that I can see what the error is because I just got an 404. The URL that I get redirected to is domain.tld/home/www/web310/html/projects/index.php?dir=project1 so obviously there is something going wrong with the internal structure of the web server an paths an whatever.
I hope you can understand my problem an I would be very pleased if someone could help me, because I'm totally lost on .htaccess anyway.
Edit:
See my answer below for the used .htaccess.
The strange thing is that if I have an index.html in on of the subdirectories, my local web server (Apache with XAMPP for Mac OS X 1.7.3) does not rewrite and the index.html gets displayed, without one it works correctly.But on my real web server that serves my homepage it rewrites both with and without index.html (which is what I want). Any hints on that?
Thanks for all the help so far! You guys are just awesome!
I figured out that a symbiosis of both of your solutions works well for me:
RewriteEngine on
RewriteBase /projects
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [QSA,L]
Of course only without [R], this was my fault. (See my question edit for another question please).
You need to add a RewriteBase /projects to the htaccess.
That way the redirect will work properly.
Edit:
RewriteEngine on
RewriteRule ^index.php - [L]
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [R,L]
Following what you ask, this is important not to do a redirect, but let the rewriterule modify internally the URL i.e. the RewriteRule should not end with [R,L] but rather [L] and maybe the "query string append" directive to keep what's after the ? so this should probably be [QSA,L].
Now here's how I'd do to avoid rewriting static files: if it's not a file then (and only then) test it:
# if it's not a file...
RewriteCond %{SCRIPT_FILENAME} !-f
# ... and it's a dir
RewriteCond %{SCRIPT_FILENAME} -d
# ... then rewrite it internally and stop further processing:
RewriteRule projects/([^/]+)(/?)$ index.php?dir=$1 [QSA,L]
And now two hints:
Please try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

How to modify .htaccess for web app (in combination with WordPress 3.0.1)

My question is about .htaccess
I am using WordPress 3.0.1 as the front end of my web application.
I want most HTTP accesses to follow the normal WordPress flow.
However, I have created a special "Page" in WordPress that has embedded PHP code that powers my web app.
URL's of this form "http://site.com/app/" already go to the correct page.
URL's of this form "http://site.com/app/?a=alpha&b=beta" go to the same page, and pass parameters to my web app. This is correct, but the URL looks ugly.
Here is my question:
I want nice looking URLs of this format:
http://site.com/app/alpha/beta
to be rewritten this way ->
http://site.com/app/?a=alpha&b=beta
I have tried adding various things in .htaccess, but I can't get it to work. I typically end up with a WordPress 404 page being displayed.
Here is the .htaccess file that was created when installing WordPress 3.0.1
# .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I am sure there must be lots of folks using WordPress as a front end for their web app, and others with expertise in .htaccess syntax. Any answers or pointers to solutions would be appreciated.
thanks in advance,
David Jones
dxjones#gmail.com
http://dxjones.com
David, don't know the answer for sure, but here's a thought.
You can use plugins like this: http://wordpress.org/extend/plugins/exec-php/
It let's you execute code from within pages or posts.
If you look into the WP documentation, you could easily tie another PHP app into WP.
Firstly, to use rewrite rules in .htaccess, two things are required:
Options +FollowSymlinks
RewriteEngine on
Then this rule will do what you describe:
RewriteRule ^app/([^/]+)/([^/]+) /app/?a=$1&b=$2 [L]
FYI ... [^/] matches any character except for "/"
and the [L] at the end will make this rewrite invisible to the user --- it's only passed to your application.
UPDATE
To make visitors using old links automatically redirect to the nicer URL (and possibly improve search engine mapping of webapp pages), add an additional rule --- like this one:
RewriteCond %{QUERY_STRING} a=([^&]+)&b=([^&]+)
RewriteRule ^app/?$ /app/%1/%2 [R=301]
The rule above turns the a and b parameters into your nice URL -- however, it would not pass on additional query parameters. (i.e. site.com/app?a=me&b=you&tag=zebra would redirect, but 'tag' parameter would be discarded in the process.) Here is a variation that allows for additional parameters:
RewriteCond %{QUERY_STRING} (^|.*&)a=([^&]+)&b=([^&]+)(&.*|$)
RewriteRule ^app/?$ /app/%2/%3?%1%4 [R=302]