Codeigniter doesn't work without index.php - .htaccess

I'm using MAMP with alias named works.
Codeigniter is at localhost/works/project/web
controllers doesn't work without index.php before them (localhost/works/project/web/index.php/auth/register)
$config['base_url'] = 'localhost/works/project/web/'; //with http://
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
(I tried all of them for uri_protocol)
I created and edit .htaccess file at /User/me/works/project/web/
I tired all the .htaccess files about this issue.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Users/me/works/project/web
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#‘system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don’t have mod_rewrite installed, all 404’s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
creates error, which is:
[error] [client ::1] File does not exist: /Users/me/works/project/web/auth
it is same when I use RewriteBase /
mod_rewrite is active in phpInfo.
I couldn't find the solution.

Your RewriteBase is wrong. It should not be the relative file path for your server, it should be the relative URL path for your rewrite rules and such. More info here.
If you've ever used HTML's <base> tag, it's pretty much the same principle. If your main localhost URL is http://localhost, and your project is in the subfolder http://localhost/my-project, then you would use RewriteBase /my-project/.
If you didn't use RewriteBase, and your project is in a subfolder, you'd have to add the subfolder to every one of your URLs and rewrites, like this:
RewriteRule ^(.*)$ /my-project/index.php/$1 [L]
Food for thought:
Start your .htaccess files as simple as possible. The more you throw at it right away, the more that can go wrong, and the harder it is to debug. Especially if you're a noob to .htaccess files. Don't blindly copy and paste -- figure out what things actually do.
The system folder access part is redundant now -- the system folder has its own .htaccess to restrict requests, just like the application folder.

1.Make below changes in application/config.php file
$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
2.use this in .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
and enable rewrite mode using below command
a2enmod rewrite
and Edit the file /etc/apache2/sites-enabled/000-default
change the AllowOverride None to AllowOverride All.
and finally Restart your server

You certainly don't have mod_rewrite on or you did not install it with MAMP
Open your httpd config file and change
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
To
AllowOverride All
uncomment this line:
#LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Also make some changes to users here: /etc/apache2/users/username.conf
LoadModule php5_module libexec/apache2/libphp5.so
DocumentRoot "/Users/username/where/ever/you/like"
<Directory "/Users/username/where/ever/you/like">
Options Indexes MultiViews +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Get more info here http://adam.merrifield.ca/2010/08/09/apache-mod_rewrite-php-and-os-x-10-6-snow-leopard/
Restart MAMP
To find out if mod_rewrite is on, follow this answer in Stackoverflow:
How to check if mod_rewrite is enabled in php?
Just to be sure your .htaccess is not the issue, could you try this one. It's been tested:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /Users/me/works/project/web/
# If the start of the URL doesn't match one of these...
RewriteCond $1 !^(index\.php|robots\.txt|assets|cache|themes|uploads|css|images|js)
# Route through index.php
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Access to certain folders are granted above and the rewritebase has a trailing slash.
Make sure you have any other nested .htaccess overriding this very one.

I fixed the problem by instead of using .htacces, I write them to httpd.conf.
I checked the AllowOverride is All but I'm not sure why .htaccess isn't working. Because of I lost too much time with this issue, I'm going to use this way.
Thanks for the answers.

Use AllowOverride All on your vhost:
<Directory "/path/to/your/site">
....
AllowOverride All
</Directory>

Open your httpd config file
LoadModule rewrite_module modules/mod_rewrite.so then AllowOverride All
<Directory "C:/xampp/htdocs"> //example for xampp
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>

I have installed codeigniter in a url like the format: http://11.12.34.45/~project/
I tried different suggestions and finally this following url provided the solution:
https://ellislab.com/expressionengine/user-guide/urls/remove_index.php.html

You need to change two files of Apache,
httpd.conf
httpd-vhosts.conf
change this in httpd.conf
<Files ".ht*">
Require all denied
</Files>
to,
<Files ".ht*">
Require all granted
</Files>
and add below code in httpd-vhosts.conf,
<VirtualHost *:8000>
ServerName localhost
DocumentRoot c:/wamp64/www
<Directory "c:/wamp64/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Allow from all
</Directory>
</VirtualHost>

Related

Codeigniter 4 Multisite

I have installed Codeigniter 4 on Raspbian and everything seems to be working fine.
My web directory is /var/www/html
Inside there are two folders containing two different Codeigniter-4 apps that I would like to invoke with:
blue.ddns.net -> /var/www/html/blue/public/index.php
black.ddns.net -> /var/www/html/black/public/index.php
So I'm creating a .htaccess file to put in the folder /var/www/html/ to handle the two requests
Well, I'm still at step 0 because I can't get the .htaccess file to work properly.
Below I attach a copy of the file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /var/www/html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ blue/public/index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
The Rewrite module is activated
the error that appears in /var/log/apache2/error.log is:
/var/www/html/.htaccess: Expected </IfModule> before end of configuration, referer: https://blue.ddns.net/
That shouldn't really be done in your htaccess. You should setup that in your apache virtual hosts.
Go into your sites-avaiable folder.
$ cd /etc/apache2/sites-available
Create a new virtual host called blue.ddns.net.conf
$ touch blue.ddns.net.conf
Open that file with nano or any other text editor you might like and add the following.
<VirtualHost *:80>
ServerAdmin your#email.com
DocumentRoot /var/www/html/blue/public
ServerName blue.ddns.net
ServerAlias blue.ddns.net
<Directory "/var/www/html/blue/public">
allow from all
AllowOverride All
Options None
Require all granted
</Directory>
RewriteEngine on
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteCond %{SERVER_NAME} =blue.ddns.net
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Save and close the file.
Then add this to your apache with the following:
$ sudo micro blue.ddns.net.conf
Repeat the same process for the other codeigniter install and reboot apache.
$ sudo service apache2 restart
That should do it.
Now if you want to override some config that would be something that you might do in your htaccess file for each codeigniter 4 install.
I've always worked in hosting and never had to do Apache configurations.
But now I'm discovering the Raspberry/Linux world.
Thanks to your answers I was able to better understand the problem and solve it (for the moment) in the following way:
Now my configuration files 000-default.conf (port 80) and 000-default-le-ssl.conf (port 443) both contain the following code:
<Directory "/var/www/html">.
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
in /var/www/html instead I created a .htaccess file with the following code:
# Disable directory browsing
Options All -Indexes
<IfModule mod_rewrite.c>.
RewriteEngine On
RewriteCond "%{HTTP_HOST}" "black\.ddns\.net"
RewriteRule ^(.*)$ black/public/index.php?/$1 [L]
RewriteCond "%{HTTP_HOST}" "blue\.ddns\.net"
RewriteRule ^(.*)$ blue/public/index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
apps are located in
/var/www/html/blue
and
/var/www/html/black
It's a very simple solution that serves my purposes.
Although I think it's not very elegant
What do you think about it?

Symfony4 routing inside a subfolder

I have a symfony app in my /var/www/html/app/ and I'd like to access it via host/app/ but I can't make it work.
I have a conf file with this
<Directory /var/www/html/app>
AllowOverride All
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
</Directory>
It's commented because I tried to do the redirect here but it didn't worked so I made the following .htaccess.
And this htaccess in /var/www/html/app/.htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
With this .htaccess, When I go to ://host/app I go to the symfony app (not the apache directory structure) and that seems to be a good start.
But the problem is I'm getting an Exception No Routes found for /app/.
That's normal because /app/ is the subfolder in my server, not the start of symfony routes.
How can I make the symfony routes beginning after the /app/ folder path ?
I know there is some questions about symfony2/3 routing in subfolder but the answer requires to separate src and public files, that's not what I'd like to have.
I just want my routes working when accessing ://host/app.
For information, It works great when I access ://host/app/public/*Symfony routes*
Edit:
New app.conf
Alias /rapp "/var/www/html/app/public"
<Directory /var/www/html/app>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
#RewriteBase /app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!public/index.php)$ public/index.php$1 [L]
</IfModule>
</Directory>
I managed to resolve the problem !!!
I'm not sure the config is "proper" though but it seems to work.
Here is my working app.conf file (in /etc/apache2/sties-enabled/app.conf)
Alias /app "/var/www/html/app/public"
<Directory /var/www/html/app>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule (/public) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
</Directory>
The first RewriteRule is to prevent from internal Rewrite loop when I rewrite url like example.com/app/page-1 to example.com/app/public/index.php/page-1.
As page-1 is not a directory or a file it will loop infinitely.
So I assume as soon as I have a /public part, the rewrite worked and we stop the rewrite by doing nothing and end the rewrite with the [L] flag.
I'm not suire why I had to remove the public/ part in the "final" RewriteRule (RewriteRule ^(.*)$ index.php/$1 [L]) though.
If someone have ideas to improve this config, feel free to leave comment.
The problem is the that your web server's document root (the directory that apache will make public via the url) and your project's document root (the public directory) are not the same. Instead your server points to the project directory. That is why you have to manually go into the project's document root by adding the public/ to the url.
You should avoid making the project directory accessible as this could expose your configuration and thus make your website vulnerable, e.g. by giving people access to your database through the credentials inside the config.
You might be able to achieve your goal by storing the project somewhere else and then only symlink your project's public dir to /var/www/html/app. This will probably require some changes to your apache config, e.g. adding Options FollowSymLinks.
Another, more elaborate, approach could be using mod_proxy. The configuration will roughly look like this:
# Your public server reachable from the outside
<VirtualHost *:80>
ProxyPass /app http://127.0.0.1:8080/
</VirtualHost>
# Your internal server where the requests are proxied to
<VirtualHost 127.0.0.1:8080>
<Directory /var/www/html/app/public>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
...
</VirtualHost>
As you can see the internal web server points to your project's document root (public dir). You might have to do additional both in the apache setup and app, e.g. make sure trusted proxies are set correctly or adding ProxyPreserveHost On to the apache setup, but roughly this should point you in the right direction.
edit: I missed another obvious solution, using mod_alias to point your symfony app to /app:
Alias /app /var/www/html/app/public

laravel virtualhost 404 error

I have apache2 on my ubuntu server.
There is my /etc/apache2/sites-available/laravel.conf
<VirtualHost *:80>
ServerName http://laravel.mysite.com/
DocumentRoot "/var/www/laravel/public"
<Directory "/var/www/laravel/public">
AllowOverride all
Order allow,deny
 Allow from all
</Directory>
</VirtualHost>
and I have open the apache's mod_rewrite function.
There is my /var/www/laravel/app/routes.php
<?php
Route::get('/', function()
{
return View::make('home/index');
});
Route::get('/hehe', function()
{
return "hehe';
});
There is my /var/www/laravel/public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
and now, I can visit the http://laravel.mysite.com, but when I visit the http://laravel.mysite.com/hehe, it gives the 404 error. Is there anything I config wrong?
Although the question is very old, for anyone still interested (as was me, but managed to solve the problem), I created a checklist to go through if Laravel routes throw a 404 or 403 error.
I assume that Laravel's originally installed public/.htaccess file is unmodified.
Be sure to insert the <Directory> section into the virtual host's apache config file with the proper path.
Be sure to have AllowOverride All, and not None in the <Directory> section to allow routes (avoiding 404 error).
Be sure to have Require all granted, and not denied in the <Directory> section to give permission visitors to see the site (avoiding 403 error).
It may also be necessary to include Options FollowSymLinks in the <Directory> section if you use symlinks in your app.
Be sure to have the rewrite module enabled in apache: $ sudo a2enmod rewrite
And don't forget to restart the apache server for the changes to take effect.
The only thing i do is to devise the apache2.conf, add this:
<Directory /var/www/laravel/public>
Options FollowSymLinks
AllowOverride All
</Directory>

How to get .htaccess files working with Apache VirtualHost

My problem is that my .htaccess file on my local server is not being read. The settings in the VirtualHost file seem to always take precedence.
I have tried the following:
Enabled mod_rewrite
Changed the AllowOverride to All but this causes a HTTP Error 500 Internal server error. I have tried it with various options but it always causes a 500 error.
I am using a VirtualHost file on Ubuntu which looks like the following:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /web/website
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /web/website>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
In my .htaccess file under /web/website I have the following rules (which are not being read):
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_USER_AGENT} ^facebookexternalhit
RewriteRule ^(.*)$ ogtags.php?$1 [L,QSA]
ErrorDocument 404 /404
ErrorDocument 401 /401
One thing I tried which did work was appending these rules directly into the VirtualHost file, but I would like my .htaccess file to work! Is that such a big ask? :(
Edit: So I looked in my apache error.log and it says Invalid command 'Action', perhaps misspelled or defined by a module not included in the server configuration referring to my .htaccess file. There doesn't seem to be a module called Action which I can enable. Any ideas?
Edit 2: I noticed that my httpd.conf file is blank. Should this matter since I am using VirtualHost files?
After looking at my apache error.log I realised I just had to enable the Apache actions module:
sudo a2enmod actions
And no more 500 Internal Server Errors!
Hope this helps somebody down the line :)
You miss the RewriteBase / directive
Add it after the RewriteEngine directive :
RewriteEngine On
RewriteBase /

htaccess problem

I am not able to get URL redirection to work. It appears to be a problem with my .htaccess file. I am using WampServer.
Following is the code in .htaccess:
RewriteEngine on
RewriteRule ^contactus index.php?file=c-contactus
RewriteRule ^/?contactus(/)?$ index.php?file=c-contactus
put a / before index.php?file=c-contactus
so it becomes:
/index.php?file=c-contactus
or
RewriteRule ^contactus$ index.php?file=c-contactus
Try:
RewriteRule ^/contactus(.*) /index.php?file=c-contactus
You must enable RewriteRule at first step.
Look at here
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
Change the AllowOverride None to AllowOverride All
in /etc/apache2/sites-enabled/000-deafult
Please make sure you have mod_rewrite module enabled in your Apache server configuration first. One of the easiest way is to run a phpinfo() and look in Loaded Modules section as below:
If you did't find the mod_rewrite there, edit the httpd.conf file in /wampserver/bin/apache/conf path and uncomment the following line:
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine on
RewriteBase /your-project-folder/
RewriteRule ^contactus/?$ index.php?file=c-contactus [L,QSA]
If you working with localhost then please provide root folder name(In which your project avaiable) for url rewritebase
For example: If your index.php file under the firstproject folder then set like this
RewriteEngine on
RewriteBase /firstproject/
RewriteRule ^contactus/?$ index.php?file=c-contactus [L,QSA]

Resources