Multiple sites from one FuelPHP installation - .htaccess

I would like to run multiple sites from one FuelPHP installation. The main driver behind this is the ability to share models and database configuration.
I've seen a couple of suggestions requiring changes to the FuelPHP directory structure, or through the use of modules.
I don't really want to mess with the directory structure.
I don't think I completely understand how modules can help me, but I think the resulting configuration would be more complex than I need.
The solution I've come up with involves a simple change to the .htaccess file located in /public. Whereby I modify the rewrite rule based on the host name. Each site's controllers and views sit in subdirectories of the main structure.
Original .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Multiple site .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} main.loc
RewriteRule ^(.*)$ index.php/main/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} admin.loc
RewriteRule ^(.*)$ index.php/admin/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} api.loc
RewriteRule ^(.*)$ index.php/api/$1 [L]
</IfModule>
My question is, is this a viable solution? Do you foresee any pitfalls that I might encounter down the line?

My working solution has been to create three FuelPHP applications side by side, then symlink key directories like core, some packages and obviously the model directory.
One gotcha was that my FTP client ignored the symlinks, so I had to recreate them on the staging server. This will not be an issue when I finally get around to automating deployment using SSH.

Related

Deny access to files inside folder but allow a REST API

I am using this .htaccess to simulate a folder full of files (a simple REST API):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule files/(.*)$ api.php?file=$1 [QSA,NC,L]
Let's call this folder magicfolder where I am using this .htaccess file.
In fact, there is only api.php that is creating every file that is requested.
So, an url like mysite.com/magicfolder/files/image.jpg is converted into mysite.com/magicfolder/api.php?file=image.jpg.
This is great. But I want also to add some rules that makes any real file that exists in magicfolder (logs and even api.php) to be inaccessible for users.
With your shown samples, could you please try following. Please clear your browser cache before testing your URLs. Make sure your htaccess rules file is present inside magicfolder folder.
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^files/(.*)/?$ api.php?file=$1 [QSA,NC,L]

How to rewrite two GET values on htaccess and mod-rewrite

I just want to make the GET information that I sent:
http://mywebsite.com/folder/index.php?category=category-alias-here
http://mywebsite.com/folder/index.php?category=category-alias-here&page=5
To be rewrited on the URL bar as:
http://mywebsite.com/folder/category-alias-here/
http://mywebsite.com/folder/category-alias-here/5/
And I cant. Its very simple, I saw several similar questions. Tested several examples on stackoverflow and they all give me 404 for both category and both pages.
Here is a code that I tested:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?category=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?category=$1&page=$2 [L]
I understand that this code be wrong for my structure specifically, I tried several changes placing and removing ([^/]+) or changed the fist [L] to [N]. And several other things. But no luck with that.
Please notice, the whole website is inside a real directory (/folder/) and the .htaccess is also there (its not on root), once I launch the website everything will be on public_html, but I do not think this is the reason of this issue.
And yes, mod_rewrite is on. There is actually a .htaccess that I use, it works anywhere I place and it gets the category correctly but it is a bit messy.
Found a solution:
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-z-]+)\/([0-9-]+)\/$ index.php?category=$1&page=$2 [NC,L]
RewriteRule ^([a-z0-9-]+)\/$ index.php?category=$1&page=$2 [NC,L]

Separate .htaccess for folder, ignoring previous one

I have this .htaccess in my root folder:
root:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1&%{QUERY_STRING} [NC,L]
So that incoming GET requests come through http://www.example.com/somethingsomething instead of ?page=somethingsomething. That's all fine.
However, in the root folder I have another folder named /i. Id like this one to handle another type of request, which looks like this: http://www.example.com/i/somethingsomething with the ending actually meaning http://www.example.com/i/index.php?img=somethingsomething. Problem is that the .htaccess in the root folder is still in use. What I need is the following:
/i:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?img=$1&%{QUERY_STRING} [NC,L]
, but somehow exclude the .htaccess in the root folder.
Is this possible?
EDIT:
Tried what I found on this site, for instance using RewriteCond %{REQUEST_URI} !^/(i) and similar in the root .htaccess.
Yes, you can do it like this, using a single rule in the root .htaccess. The root .htaccess is always going to get processed, so you might as well do it there with one rule. Otherwise it would be more complicated and the root would need modifying with an exception for /i anyway.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(?:i/)?index\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(i/)?(.*)$ /$1index.php?img=$2 [QSA,NC,L]
Using the [QSA] flag which is a better way to pass on any existing query string. RewriteBase is not needed. You perhaps don't need the directory check and it would be better for performance without it. The index.php check is there to improve performance by avoiding another file-system check after a successful rewrite.
Update
Sorry, I hadn't noticed that the parameter you are passing to index.php has a different name in the second case. These rules in root should work for you:
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !^/i/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1 [QSA,NC,L]
RewriteCond %{REQUEST_URI} !^/(?:i/)?index\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^i/(.*)$ /i/index.php?img=$1 [QSA,NC,L]

Redirecting all requests from a subdirectory to a specific file

I wanted to redirect all requests from a specific subdirectory (/portfolio/*) to a specific file in the root of my website (portfolio.php). This way I want to be able to create project-specific pages within the same file as where the overview is being created.
I tried the following, but it didn't work since it stopped images from loading (images were located in the /images/portfolio/* directory), and it also disables my custom ErrorDocument...
RewriteRule /portfolio/(.*) /$1 [PT]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /portfolio.php [L]
Anyone has an idea on how to fix this?
Try getting rid of the first rule and adding the pattern to your last rule:
RewriteRule ^portfolio.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^portfolio/(.+)$ /portfolio.php [L]
And for good measure, make sure multiviews is turned off:
Options -Multiviews

Moving codeigniter project to a new hosting provider - .htaccess and index.php

I just moved my functional codeigniter project to a new web hosting provider and am now having challenges removing the index.php from the URL using a standard .htaccess mod-rewrite. Here is the .htaccess that was working fine:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I found this discussion but I do not have root access to the apache server to make the suggested configuration change.
Using the provided .htaccess file above,
Works fine: http://www.mysite.com/index.php/plans
Doesn't work: http://www.mysite.com/plans
Any suggestions are appreciated.
If you really have to accept both forms of URLs likewise then you need two RewriteRules. Once accepting variants with then 'index.php' part and one without.
Something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index\.php/(.*)$ index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Your question's not very clear, but a common problem with mod_rewrite rules like this is that some servers need the request to be passed as a query. I.e. you'd need to change your last line to:
RewriteRule ^(.*)$ index.php?/$1 [L]

Resources