.htaccess file on the localhost - apache

I am trying rewrite my url by .htaccess file. I tried to find out solution before I ask but nothing to seem work. Could you please help out? Thank you.
Here is my .htaccess file (In the root folders):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
RewriteRule ^film/([^/]*)/([^/]*)\.html$ /html/movie/film.php?film=$1&genreID=$2 [L,QSA]
</IfModule>
I did set these lines (in etc/apache2/sites-available/ 000-default.conf):
DocumentRoot /var/www
<Directory "/var/www/html/movie">
AllowOverride All
</Directory>
I am using Linux mate Apache 2.4.7 and in error.log is nothing wrong.

Do you allow mod_rewrite in php.ini?

You have an .htaccess in the document root folder, which is /var/www according to 000-default.conf.
But you don't allow .htaccess files in /var/www. You allow .htaccess files in /var/www/html/movie, again according to 000-default.conf.
To allow .htaccess files in /var/www or /var/www/html, you must say so, e.g.
<Directory /var/www>
AllowOverride All
</Directory>
or
<Directory /var/www/html>
AllowOverride All
</Directory>
N.B. you don't need AllowOverride All, AllowOverride FileInfo is sufficient for using mod_rewrite, see AllowOverride

Related

.htaccess not working on apache24

I am trying to rewrite the url for a rest call (backend written in php).
The problem is, that the .htaccess file is not working, or the file in general is ignored by the server.
What i've tried
I've edited the httpd.conf file and changend following lines:
<Directory />
Options All
AllowOverride All
</Directory>
<Directory "${SRVROOT}/htdocs">
Options All
AllowOverride All
Require all granted
</Directory>
i also added a Directory Tag for my specific folder:
<Directory "${SRVROOT}/htdocs/my/path/backend/">
Options All
AllowOverride All
</Directory>
My .htaccess file looks like this:
#.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) index.php/$1 [L,QSA]
Because i am using the RewriteEngine i had to enable this module in the httpd.conf file:
LoadModule rewrite_module modules/mod_rewrite.so
Infos
I'm working on Windows 10 machine.
My Apache version is: 2.4.16 Win32
Problem
The problem is that the server ignores the .htaccess file. I know that because i tried to change the DefaultIndex with following line:
DirectoryIndex startPage.html
without any success.

Apache2 rewrite doesn´t work

I am making a test with codeigniter and I make this .htaccess:
#Options +FollowSymLinks
#Options -Indexes
DirectoryIndex index.html index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|css|Scripts|images|uploads)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
On the config files of apache where is necessary make many changes, I have this (only what I think is important for this case):
/etc/apache2/apache2.conf
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
/etc/apache2/site-available/000-default.conf
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
<Directory />
#Options FollowSymLinks
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order deny,allow
Allow from all
</Directory>
The path where the codeigniter is installed is in /var/www/html/test, and if on browser I make http://ejemplo.com/test the wellcome controller of igniter works, but if I try to access to other function on this controller I get a 404 (make by apache, not the 404 from codeigniter).
Do I forget something?? What am I doing wrong??
Thank you.
PS:The codeigniter is an empty and new installation (only a helloworld function on the wellcome controller).

Enable .htaccess file in OS-X Mavericks

I have my website code at /Library/WebServer/Documents/edu and I'm using the following .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
which I've put under /Library/WebServer/Documents/edu, but apache server seems to be ignoring this file and whatever changes I'm doing here are not taking effect.
Do I need to do some additional changes ?
By default Apache has .htaccess disabled, so you would have to enable it.
Go to and edit /etc/apache2/httpd.conf
You will find a section that looks like this:
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
You have to change AllowOverride None to AllowOverride All and then find the section right after which should start with: <Directory "/Library/WebServer/Documents"> and you'll have to change the same thing there to AllowOverride All
The answer is obsolete post Apache 2.4
The right way is:
DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
AllowOverride all
Allow from all
</Directory>

Laravel Routes not working, Apache configuration only allows for public/index.php/route

Example:
Route::get('/get', function() {
return 'get';
});
To view the route above, I must navigate to public/index.php/get.
I've viewed quite a few SO posts and googled around trying different things and it hasn't made a difference (yes I restart apache every time).
Here is my .htaccess in the public directory:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
# For all files not found in the file system, reroute the request to the
# "index.php" front controller, keeping the query string intact
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
What could be causing this still? I'm running Ubuntu.
Two most common causes of this behavior are:
mod_rewrite not enabled
sudo a2enmod rewrite && sudo service apache2 restart
AllowOverride is set to None, set it to All, assuming Apache2.4
sudo nano /etc/apache2/apache2.conf
search for <Directory /var/www/> and change AllowOverride None to AllowOverride All, then save the file and restart apache
Do you have mod_rewrite installed and enabled on apache? Try to remove the if lines ( and ) and see if it throws an error when you try to load the website. If it does, run sudo a2enmod rewrite and restart apache.
This is the .htaccess I have on my public/ directory:
<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>
In the Apache Configuration file may be located at /etc/httpd/conf/httpd.conf (location and names varies on server), provide AllowOverride permission for .htaccess file by updating the directory as follows:
<Directory "/var/www/laravel-sample-project/public">
AllowOverride All
</Directory>
This may solve your problem on laravel routes.
Also ensure that you're providing access "AllowOverride All" limited to directories, such as mentioned above and limited to the directory /var/www/laravel-sample-project/public (mentioned here is the root directory for my project which may varies for you)
Changing AllowOverride None to AllowOverride All and sudo a2enmod rewrite && sudo /etc/init.d/apache2 reload did help.
I know it's an old question, but for people coming from search engines:
Although the apache site config I had enabled (/etc/apache2/sites-enabled/sitename.conf) had AllowOverride All
But for some reason the /etc/apache2/apache2.conf file had AllowOverride None for /var/www/ directory.
Solution:
Run nano /etc/apache2/apache2.conf
Find Directory /var/www/ (on mac you can ctrl+w, paste the text and hit enter)
Inside it, change AllowOverride None to AllowOverride All
So, this
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
becomes this:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
If you do not use a .htaccess file, you need to specify the directory for your redirections:
<IfModule mod_rewrite.c>
RewriteEngine On
<Directory /home/vitela/laravel/public>
RewriteBase /public
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L,QSA]
</Directory>
</IfModule>
In MacOs. The Main problem of .htaccess not working is there is not mod_rewrite.so enabled in httpd.conf file of apache configuration.
i have solved this by uncomment the line :
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Remove the # from above line httpdf.conf. Then it will works. enjoy!

Turning RewriteEngine on creates 403 error--how to turn on FollowSymLinks?

I am working with the built-in Apache2 on OSX. I moved the document root to a folder on my desktop and made sure that _www and everyone have read access. It works fine and great, PHP works, everything works until I add a .htaccess with this line only:
RewriteEngine on
As soon as I do that, everything in the directory with the file is 403 Forbidden:
Forbidden
You don't have permission to access /dir/file.txt on this server.
Apache logs show this error:
[error] [client 127.0.0.1] Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive is forbidden: /Users/uname/Desktop/localhost/dir/filename.txt
I've gone through httpd.conf and made sure that I've enabled FollowSymLinks to no avail:
DocumentRoot "/Users/uname/Desktop/localhost"
<Directory />
Options FollowSymLinks
Options SymLinksIfOwnerMatch
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "/Users/uname/Desktop/localhost">
Options FollowSymLinks
Options SymLinksIfOwnerMatch
Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
If I comment out RewriteEngine on in the .htaccess file, then everything works fine, except rewrite rules, obviously. On OSX 10.8.5, httpd.conf is located in /etc/apache2 which also has the folder users containing individual files for uses, e.g. uname.conf, but I've added symlinks in here same as in the other. I noticed there's also a folder extra with files like httpd-userdir.conf, but they don't seem to disable anything.
Where else can FollowSymLinks be turned on (or off)?
You have to either put your Options on one line, or add a + sign in front of your Options so Apache understands you want merge them. At the moment only the last Options directive ('Options Indexes MultiViews') is being applied since it is overwriting all the prior Options.
Try this (which will overwrite the '/' Options):
<Directory "/Users/uname/Desktop/localhost">
Options Indexes MultiViews FollowSymLinks SymLinksIfOwnerMatch
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I had an issue with getting a 403 the solution for me was changing my rewrite rules. My complete directives are as follows:
<Directory "/var/www/">
<LimitExcept GET POST HEAD>
Order Allow,Deny
Deny from all
Satisfy all
</LimitExcept>
Require all granted
AllowOverride None
# Disable includes and cgi
Options -Includes
Options -ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirdect to HTTPS
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule ^(.*)$ - [F,L]
# Put your installation directory here:
RewriteBase /
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# remove index.php
RewriteRule ^(.*)$ /index.php/$1 [L]
# If your host requires forcing query strings.
# Notice the question at the end of index.php
# on the last rule
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
</Directory>
ServerName www.example.com
ServerAlias www.example.com