mod_rewrite with codeigniter - codeigniter

im using shared hosting. i build my web application with codeigniter and my new web directory will be as follows:
/home/projName/public_html/proj_v3/
under this dir, there are index.php, .htaccess. as shown above, i separated different project version in different directories (ie: proj_v1,proj_v2, proj_v3). the web applications allows photo upload and it's stored in /home/projName/public_html/proj_v3/application/uploads. proj_v2 currently has the latest uploaded photo since it's still in use. because i have different version of projects, i want to place directory uploads in a common directory such as /home/projName/public_html. so that for any new version of projects, i won't have to move the upload folder to the new project folder. this might cause downtime.
currently, the .htaccess file in /home/projName/public_html/proj_v2 is:
Options -indexes
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1/ [L]
i have another .htaccess in /home/projName/public_html/ is:
Options All -Indexes
RewriteEngine On
Rewriterule ^(.*)\.*$ proj_v3/index.php/$1/
RewriteOptions MaxRedirects=3
basically how should i go about editing the 2nd .htaccess file in /home/projName/public_html/ so that if the web directory is domain.com/uploads/image.png, it gets the image from /home/projName/public_html/uploads. currently, the 2nd .htaccess file tells domain.com to point /home/projName/public_html/proj_v3

RewriteRule \.(jpe?g|gif|png|wmv|bmp)$ /uploads/$1
Might work for you. You can add other formats as needed.

What you want to do is allow the paths to real files and folders to exist, while redirecting everything else to CodeIgniter, and you can achieve this using this in your second .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Definitely worth reading this article on the CodeIgniter website about mod rewrite
I actually gave a similar answer before: mod_rewrite in Users/<username>/Sites directory on OSX

Related

Unable to open subdirectory laravel installation

In my /public_html/ I have installed an WordPress site. Now I have installed an laravel application inside /public_html/app/.
Then in /public_html/app/.htaccess I have added:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
In /public_html/app/public/.htaccess I have added:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /app/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
</IfModule>
When I trying to open https://example.com/app/ I've got error 404. When I try to open directly https://example.com/app/public I've got half working site because it is searching for the css/images in https://example.com/.
What is need to be changed in the htaccesss in order to work.
The goal is to have a button on the main WP site and when I click it to load the laravel site.
RewriteBase /app/
You need to remove the RewriteBase directive. This ends up rewriting the request to /app/index.php, when it should be /app/public/index.php. The default is to rewrite to the current directory (the directory that contains the .htaccess file), so the RewriteBase directive is not required here.
(Or, you could set this "correctly" to RewriteBase /app/public - but that is not necessary and would then hardcode this installation to the /app directory.)
I've got half working site because it is searching for the css/images in https://example.com/
It depends on where your images are. If images are located at /app/public/assets/images/myimage.jpg then you should be referencing your images using a root-relative URL (starting with a slash), excluding the public directory, eg. href="/app/assets/images/myimage.jpg".
UPDATE:
is this means that I now have to manually edit all images, links, buttons, etc on the site in order to add /app/... in front of the assets?
Ordinarily, yes. In the same way you have presumably added /app/ before all your internal links to your pages. (?)
Logo for example - <img src="/assets/main/img/logo.png">
However, since you are using a root-relative URL and your Laravel assets are in a known location, then you could workaround this by implementing a rewrite in the root (WordPress) .htaccess file to rewrite your Laravel assets to the correct location (presumably /app/public/...).
However, this does mean that you cannot then have an /assets subdirectory in the root (without implementing additional filesystem checks), nor have a WordPress URL that starts /assets, since it will conflict and won't be accessible.
For example, at the top of the /.htaccess file, before any other WordPress directives you could do something like the following:
# Rewrite Laravel assets to the correct location
RewriteRule ^assets/.+ app/public/$0 [L]
# BEGIN WordPress
:
Where $0 is a backreference that contains the entire URL-path that is matched by the RewriteRule pattern.
Now, any request for /assets/<something> will be internally rewritten to /app/public/assets/<something>. /assets/ itself won't be rewritten.

Redirect laravel installation in a subfolder, hiding the subfolder

Let's say i have installed laravel on a subfolder: i placed all the app stuff outside the html root, renamed the "public" directory in "laravel" and moved under the html root.
I can see the app by connecting to www.mydomain.com/laravel
I used the following .htaccess in the root html folder:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteCond %{REQUEST_URI} !^/laravel/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /laravel/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteRule ^(/)?$ laravel/ [L]
Now i can access my site directly from www.mydomain.com
But i see that i can still access it from www.mydomain.com/laravel ... and since all the links are generated starting from the base url, the menu and every anchor href points to www.mydomain.com/laravel... even the canonical link of the pages contains "laravel".
Questions:
is there a way to avoid this??
should i worry for this?
Thanks for help.
If you are using apache, you need to configure your hosts. Either http-vhosts.conf or httpd.conf or similar file.
<VirtualHost >
...
<Directory "path/to/your/root/html/laravel">
</Directory>
</VirtualHost>
So all you need to do is change the directory path to point directly to your laravel folder inside of html directory.
If you do that, remove the lines you added in the .htaccess
RewriteCond %{REQUEST_URI} !^/laravel/
...
RewriteRule ^(/)?$ laravel/ [L]
And place the .htaccess file back in laravel directory so it can serve page directly from there.

Install Joomla in a subdirectory

As some of you may know, Wordpress has an options in settings to allow site installation in a subdirectory, while having the site URL be the main domain. It was something like "Site url" and "Wordpress url". I'm looking for something like this in Joomla. I know there is no inbuilt option for it, but I'd rather not have to move all the files if possible. And please, explain it to me like to a five year old, just in case :)
To move the whole joomla installation to a subfolder on the server (http://example.com/subdir), but still access it from the root (http://example.com) I did the following:
Move your whole installation to the subdir-folder
In configuration.php, set $live_site = "http://example.com";
Also change the tmp and log-folders in configuration.php
Add a .htaccess-file to the root-folder:
(The code is modified from this excellent answer)
RewriteEngine on
RewriteCond %{THE_REQUEST} subdir/
RewriteRule ^subdir/(.*) http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !subdir/
RewriteRule ^(.*)$ /subdir/$1 [L]
Modify the default joomla .htaccess-file, now in the /subdir-folder, to include a RewriteBase:
RewriteBase /subdir/
After these modifications it seems everything works the way it should.
You can override the file /includes/defines.php in your joomla installation. Copy this file to the root folder of your installation, and then change all folder names to how you like your setup.
In /index.php you see how it first checks if /defines.php exists. Then it goes on to load /includes/defines.php if _JDEFINES is not defined. So be sure to include
define('_JDEFINES', 'TRUE');
in your overridden /defines.php-file. Good luck :)
Below is how index.php loads folder definitions:
if (file_exists(__DIR__ . '/defines.php')){
include_once __DIR__ . '/defines.php';
}
if (!defined('_JDEFINES')){
define('JPATH_BASE', __DIR__);
require_once JPATH_BASE . '/includes/defines.php';
}
I see now that you are able to override folder locations in /administrator in a similar matter, copy /administrator/includes/defines.php to /administrator and override folders here.
I have used an extension called Virtual Domains for this before. According to them it provides
Multi-domain capability for Joomla without changing the Joomla core
files.
. Which I have made use of previously and it works well
I first tried the accepted answer. However, that answer also redirects existing files and folders to the new subdir.
For this reason I used:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdir/index.php [L]
instead.
I'm aware that this is not the best solution as it doesn't hide the sub directory properly. However, it allows to keep the existing code on that site working.

No root access for uploading Laravel Project

My hosting provider doesn't allow me to upload files on root. It only give access to public folder.
I tried to move index.php & .htaccess files from Project's public directory to Server's Public Directory. It temporarily solve the problem. But I have to change every single file structure. Otherwise it doesn't find link. So please suggest me to upload my Laravel Project on such Host.
Try this in your .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
Of course, replace domain-name.com with your actual domain.
What this does is, it internally forwards all requests to your public folder. For example www.domain-name.com/lorem-ipsum will be redirected to www.domain-name.com/public/lorem-ipsum.
However, I have not tested this but it should work well in theory.

I want to keep the assets folder inside the application folder not outside

My question as in the title
I want to keep the assets folder which contain (css, js, images) inside the application folder not outside.
If I keep the assets folder inside the application folder, I can't access to the files that inside it.
.htaccess file code
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
The standard Application structure:
Application
app
assets
sys
.htaccess
index.php
The previous structure has no errors.
I want the structure to be like the following:
Application
app
assets
config
controllers
......
sys
.htaccess
index.php
How can I do that ?
Your problem is that the .htaccess file you're using is rewriting the URL of your static assets, breaking the links, so you need to change it.
If you keep your assets/ folder inside the root of the application and amend your .htaccess to something like this, that should solve the issue:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
The change is on the second line - essentially the condition is "If the route doesn't start with index.php, assets, or robots.txt, then rewrite it to include index.php at the start". The addition of assets should solve the issue.
EDIT: By default, CodeIgniter has an .htaccess file inside the application/ folder that denies access to anything inside it. If you really, really want to be able to serve your static files from that folder, you can amend that file. However, it would be a really bad idea. Your static assets should live in the web root, and trying to serve them from the application folder will be fraught with problems - if you allow access to the application/ directory from outside, it could cause some serious security issues.

Resources