.htaccess file with PHP MVC Framework - php

I am building a website using CodeIgniter PHP MVC Framework, but am having troubles with the routing of the site. I believe the problem is with the .htaccess file that I am using. I have my site at localhost/MyWebsite and the home controller (default) is calling the index action correctly. However, when I try to go to any other Controller/Action (e.g. localhost/MyWebsite/register) route I get a Forbidden Access error. I have a Register Controller with an index action but they were not being found or something. Here is the .htaccess file I am using.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
What do I need to do to make it so all requests are routed to the index.php file in CodeIgniter so that the proper Controllers and Actions can be called?

Are you allowing override within Apache for your .htaccess file to take effect?
Paste the following code within httpd.conf configuration file from Apache:
<Directory "PATH_TO_FRAMEWORK">
Option -Indexes +FollowSymLinks
AllowOverride all
Require all granted
</Directory>

Make share that your files or folder are owned by www-data (whether owner user/group), check with: ls -l
assuming that your OS is ubuntu
if it's not correctly owned then execute:
sudo usermod -a -G your-username www-data
sudo chown your-username:www-data

Related

Running a core php project from apache instead of php -S localhost:4000

I'm trying to run a php project using apache configurations in LAMP but its not working*, whereas when I run it as php -S locahost:4000 its working really great.
Here is the link to the project if you need some info about the files or working of it Project
Here is my apache configuration -
<VirtualHost localhost:4000>
ServerAdmin root#localhost
ServerName localhost
ServerAlias localhost
DocumentRoot /var/www/html/dir
<Directory "/var/www/html/dir">
AllowOverride None
Options None
Require all granted
</Directory>
*not working means - when running it through apache i can only access the index page and when going to some other page of the project like localhost:4000/about It shows The requested URL /department was not found on this server. ie. Error 404.
I think that you expect "index.php" to receive all requests.
Now, Apache is trying to find "about" directory and "department" directory.
In order for Apache to run index.php on any URL, we need to use the Rewrite rule.
Although I have not verified it in detail, I guess it will work with the following rules.
RewriteEngine on
RewriteRule ^/(.*)$ /index.php
It now working after enabling a2enmod rewrite from apache and updating the contents of .htaccess file as follows
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]

Laravel 5 - Internal Pages giving Not Found Error

Actually I am deploying my laravel 5 Application on Linode Server.
My client create space for me in such type of path.
var/www/html/abcproject
I point out my godaddy domain to abcproject directory.
Now I have uploaded my all Laravel 5 files on abcproject directory.
My issue is that , my home page is working fine but my internal pages
are giving Not Fount Error.
Suppose when I try to logged in it is giving below error.
Not Found
The requested URL /abcproject/login was not found on this server.
Secondly Laravel 5 URL is getting url like that.
Supposed URL.
http://15.30.19.61/abcproject/index.php/registers
I did not understand where is and what is the issue.
One thing is my project is working fine when I try such type of URL.
http://example.com/index.php/ But In this case my URL Rewriting did not work but project is working like I can logged in, go in different pages, Logout etc.
My HTACCESS FILE
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Thanks
If you are powering your laravel project from a sub folder such as you mentioned above:
http://15.30.19.61/abcproject
You should add this to your .htaccess file under RewriteEngine On.
RewriteBase /abcproject
I also faced a similar issue with LAMP configuration and I resolved by enabling rewrite rules of apache. If some needs details then it can be referred at the below blog notes
http://ankgne.com/post/setting-up-digital-ocean-for-laravel-application
Step 1: Enabling mod re-write using sudo a2enmod rewrite
Step 2: Allowing the laravel .htaccess (in public directory of laravel page)
file to apply rewrite rules
sudo vi /etc/apache2/sites-available/000-default.conf and adding below lines
Note: Change "/var/www/html" to "path of laravel public folder"
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
. . .
Step 3: Restarting web server using sudo systemctl restart apache2

Avoid public folder of laravel and open directly the root in web server

I was going through all possible sample on internet to solve this. Still it is an headache.
I just want to avoid the 'public' in www.mylaravelsite.com/public/
and make it like www.mylaravelsite.com for the root directory.
Now I do not want to avoid the security concern,So I learned .htaccess would be the best way.
Any solution friends ?
& advance thanks for interacting !
Let's assume you have this folder structure in your server
.cpanel/
public_html/
public_ftp/
..
And the laravel folder structure is
app/
bootstrap/
public/
vendor/
composer.json
artisan
..
You can create a folder name mylaravelsite on your server inline with public_html and public_ftp folder, and copy to it the whole laravel application except the public folder because you will paste all of it contents on the public_html, so you have now:
.cpanel/
public_html/
public_html/packages
public_html/vendor
public_html/index.php
public_html/.htaccess
...
public_ftp/
mylaravelsite/
mylaravelsite/app
mylaravelsite/bootstrap
...
On your public_html/index.php change the following line:
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
to
require __DIR__.'/../mylaravelsite/bootstrap/autoload.php';
$app = require_once __DIR__.'/../mylaravelsite/bootstrap/start.php';
and also don't forget to change /mylaravelsite/bootstrap/paths.php public path, you might use it.
'public' => __DIR__.'/../public',
to
'public' => __DIR__.'/../../public_html',
Your site should be running.
Create .htaccess in root folder and write these line
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
For production server it is recommended that you point your domain directly to the public folder
It sounds like the information you are missing is:
Documentroot
This is also called the "web root". Apache specifically calls it the DocumentRoot - Click that link for the official explanation.
Basically what it does is set which directory the server pulls files from. In Laravel, the document root should be the public folder.
Setting the DocumentRoot to public means that going to http://mylaravelsite.com in the browser will infact be "pointing" to the public folder as you want.
In the .htaccess file (actually, more likely in the virtual host configuration), you can set the DocumentRoot for your site:
DocumentRoot /path/to/laravel-app/public
How you set this up depends on your hosting. Each hosting has different ways to setup a website and so we cannot answer that specifically for you - you should consult your web hostings tech support. The key point is that the public directory should be the web root, while everything else should be "behind the web root".
Fair warning tho: some hosting providers do not give you enough access to accomplish that.
Just add .htaccess file on Laravel root if it's not present and add following lines in it, that's it,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
create .htacess file in the root directory of your laravel project and put this code in.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
point your web directory to public/. If you are using apache this will work:
<VirtualHost *:80>
DocumentRoot /var/www.mylaravelsite.com/public/
ServerName www.mylaravelsite.com
</VirtualHost>
Here is the solution, But Must not do it in real projects, it is just for starter to test Laravel and i have tested it with laravel 5.0 and it is ,cut index.php and .htaccess files from public folder and paste it in the root directory and change these two lines as
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
It is Highly Recommended not to use the above method without testing environment, and should use virtual host instead.
All you just need to change .env file in your laravel project files. if your laravel project inside in
Root/domain/public_html/laravel/
Go to .env file and edit
you need to edit app url path which should be
APP_URL=http://www.domainname.com/laravel/public/
Thats it your project will run now. if your laravel files is in public_html, then your app url must be like this
APP_URL=http://www.domainname.com
Thanks
For Lumen
Let's think you have a folder path like: /var/www/project/microservice(lumen src)
/var/www => document root for localhost/
Target you want:
localhost/project/microservice[/foo...] => localhost/project/microservice/public/foo...
I do this by .htaccess (placed at /project folder) like below:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*/public/.*
RewriteCond %{REQUEST_URI} ^(/[^/]+/[^/]+/).*
RewriteRule ^[^/]+(.*)$ %1public$1 [L,R=301,P]
On Server(Apache)
Create .htaccess in the root folder and write below line
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
Lean more on Gist
On Local Development
You can create a virtual host by adding the below line to your apache vhost config file.
Generally, you can find this file in C:\xampp\apache\conf\extra\httpd-vhosts.conf file
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/mysite/public/"
ServerName www.mysite.local
</VirtualHost>

Laravel 4 - 403 Permissions error when trying to access css/js files

I'm running on an Ubuntu 13.10 dev environment with Apache2 and after hours of trying to figure out permission errors to access routes, I was able to fix it. Now I can successfully browse through my application, but the problem that now exists is that I cannot access my css/js files within my public directory - it kicks back with a 403.
I've tried modifying the .htaccess file, the virtual host config file, and ran chmod on the entire site directory.
Here's a copy of my .htaccess file within the Public folder:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
And here's a copy of my virtual host file:
<VirtualHost *:80>
DocumentRoot "/home/casey/Sites/caseyhoffmann.me/public"
ServerName caseyhoffmann.me.dev
<Directory "/home/casey/Sites/caseyhoffmann.me/public/">
AllowOverride all
Require all granted
DirectoryIndex index.php index.html
Options -MultiViews
</Directory>
</VirtualHost>
Any ideas?
Solved. Reapplied chown and redid chmod on the directory. File system permissions issue and had nothing to do with Apache.
Solution 1: (Recommended)
use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
Solution 2:
You can try adding this in your page's header:
<base href="/" />

htaccess rewrite - not rewriting to index.php

I have three web servers on three separate computers. I am trying to install a php application that rewrites all url requests to index.php (front controller pattern). The following .htaccess works on two out of the three web servers:
RewriteEngine On
# Redirect everything to the Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(css|images|files)/ index.php [NC,L]
So, when I visit http://example.com/hithere, index.php is called and I can then parse the originally requested url (hithere) to figure out what controller to send it to.
However, for some unknown reason, the above .htaccess file doesn't work on the third webserver. Instead, when I access http://example.com/hithere, I get a 404 not found error in my browser that says this:
The requested URL /full/path/to/application/index.php was not found on this server.
So, based on the above error, I can tell that it's actually trying to redirect to index.php. But the file that the browser lists in the /full/path/... below is actually on my web server - so I have no idea why it can't find it. Could someone please help me and tell me what I'm doing wrong?
Edit: I don't know if something in the httpd.conf could be conflicting, but here are the settings I have in httpd.conf for the directory that this application is in
Alias /myapp /full/path/to/my/app
<Directory /full/path/to/my/app>
Order allow,deny
AllowOverride All
Allow from all
</Directory>
Edit 2: I noticed something peculiar in the Apache logs. It looks Apache is appending index.php, which I am trying to redirect traffic to, to the DocumentRoot:
File does not exist: /document/root/full/path/to/my/app/index.php
When it should be going to:
/full/path/to/my/app/index.php
So I basically need to tell htaccess to ignore the document root. I tried this by specifying this in my .htaccess file:
RewriteBase /full/path/to/my/app/
But it's still searching the document root for index.php :/ Is there a way to reset the DocumentRoot or tell htaccess to ignore it when rewriting to index.php? Even when I use the absolute path in the RewriteRule, it still appends it to the document root.
RewriteEngine On
# Redirect everything to the Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(css|images|files)/ /full/path/to/my/app/index.php [NC,L]
It doesn't make sense to me... Can someone please help?
Thanks
So, based on the above error, I can tell that it's actually trying to
redirect to index.php. But the file that the browser lists in the
/full/path/... below is actually on my web server - so I have no idea
why it can't find it.
Assuming it's a *nix alike system, you might want to check file permissions and make sure that the webserver has read access to the file.
You also need to check the DocumentRoot parameter in httpd.conf
Look for something like this in the file and make sure it's correct. That is also where apache will be getting it from.
DocumentRoot "/var/www/html/path/to/whatever"
Also I would disable SELinux if it's on and you really don't need it because it causes weird problems too.
First enable rewrite module in apache by using $ sudo a2enmod rewrite then restart web server by $ sudo systemctl restart apache2
Put following code in your virtual host.
<Directory /var/www/html/your_app_folder>
Order allow,deny
AllowOverride All
Allow from all
</Directory>
then add following code in your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Categories