.htaccess Is Not working in Linux(Debian) Apache2 - apache

I am using apache2 (my dummy server) which is already install with my Debian. Every thing goes fine, but now the problem with my .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
It's not working
I think its because of apache2 version which am I using & maybe problems with my code or something I have to config on my server
I want to redirect my url to main index page if its a wrong entry or unavailable

After spending a whole day, I got my answer
In Folder
apache2>>sites-available>> There is file called default
In default we have to change it
From:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
TO:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Its working. It's enabled use of .htaccess files.

I'd like to add that /etc/apache2/mods-available/rewrite.load needs to be enabled:
a2enmod rewrite
On Debian I thought it was enabled by default, but mine wasn't.

This code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,R]
will redirect http://example.com/test.php to http://example.com/index.php?url=test.php if the file doesn't exist. The only difference between my code here and your one is I have [R] instead of [QSA,L] If it still doesn't work for you and you have the htaccess file in the root folder then, I don't think it's a htaccess file problem

Important to note that AllowOverride only works in <Directory> directives and will be ignored if placed inside <Location ...> section; this was my issue and it took me for a nice ride.
Only available in sections AllowOverrideList is valid only
in sections specified without regular expressions, not in
, or sections.
https://httpd.apache.org/docs/2.4/mod/core.html#allowoverride

Related

Too many redirects error and browser shows

With Reference htaccess RewriteRule redirecting to parent directory?
Thank you Francesco Casula, your answer was very helpful, application now redirects from one document root to other. I am facing infinite redirects issue.
I have following repositories with different version.
/var/www/portal/version/1.1.1/public/ binded with (api.somedomain.com)
/var/www/portal/version/1.1.2/public/
/var/www/portal/version/1.1.3/public/
/var/www/portal/version/1.1.4/public/
I want to execute api in following order (by version)
api.somedomain.com/qr ---> /var/www/portal/version/1.1.1/public/
api.somedomain.com/v2/qr ---> /var/www/portal/version/1.1.2/public/
api.somedomain.com/v3/qr ---> /var/www/portal/version/1.1.3/public/
api.somedomain.com/v4/qr ---> /var/www/portal/version/1.1.4/public/
My httpd.conf is
AliasMatch ^/v2/(.*)$ "/var/www/portal/version/1.1.2/public/"
<Directory "/var/www/portal/version/1.1.2/public">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
</Directory>
.
.
AliasMatch ^/v4/(.*)$ "/var/www/portal/version/1.1.4/public/"
<Directory "/var/www/portal/version/1.1.4/public">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
</Directory>
and .htaccess in /var/www/portal/version/1.1.4/public/ contains
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} ^/v2/(.*)$
RewriteRule ^(.*)$ v2/index.php [QSA,L] # p2 is the symlink name!
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
When I hit https://api.somedomain.com/v2/invitebysms
I get too many redirects error and browser shows
https://api.somedomain.com/v2/invitebysms/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/
I think what you're going for is the following:
Alias /v4/ /var/www/portal/version/1.1.4/public/
<Directory /var/www/portal/version/1.1.4/public/>
Require all granted
FallbackResource /v4/index.php
</Directory>
In as far as I understand what you're trying to accomplish, the above, in your server config (delete the .htaccess file entirely) will do what you want. (Repeat for v2, v3, and so on.)
I am assuming here that you're running at least 2.2.16 or later, and preferably 2.4. If not, let me know, and we'll try again.

Rewrite from subdomain to file in .htaccess

this htaccess code work genially on old server, but on new is work perfectly without last RewriteRule. After put adress in web explorer for example sub.domain.com it load index.php. It may be by wrong setting of Apache? Or other?
Thanks a lot
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^hra/([0-9]+)/?$ /game2.php?id=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com [NC]
RewriteRule ^$ /nacitanie.php [L]
in file 000-default.conf on new server in sites-enabled is part of enabling htaccess:
<Directory /home/juraj/WWW>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
in apache.conf on new server is part about htaccess too:
<Directory /home/juraj/WWW>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
The error was in condition of last RewriteRule, where in regular condition on new server must be added option with index.php
RewriteCond %{HTTP_HOST} "^sub\.domain\.com"
RewriteRule "^(index\.php|\?)$" "/nacitanie.php" [L,QSA]

Apache httpd.conf issue with Rewrite

I am trying to add the following lines to my Apache httpd.conf file (I have version 2.2 on Windows 7)
Alias /my_ember_proj "C:\my_ember_code\dist"
<Directory "C:\my_ember_code\dist">
Order allow,deny
Allow from all
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_ember_proj/index.html#$1 [L]
</Directory>
However the following lines are failing
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_ember_proj/index.html#$1 [L]
I know this because the server starts without these 4 lines (I don't know where I can find any specific failure details. I do not see any specific error in the logs folder).
As already mentioned in comment, the Require directive, provided by mod_authz_host is not available in Apache 2.2. The module was introduced in Apache 2.3 and later version.
So, first check if the Apache server is indeed 2.2.
Next, confirm whether the rewrite module has been loaded or not. In the same httpd.conf file, search for the following:
LoadModule rewrite_module modules/mod_rewrite.so
If there is a # preceeding it, remove the # symbol, save file and restart the server.

default cakePhp .htaccess file does not work with redirection

I just installed an Ubuntu web server with apache2. I upload a CakePHP project. I also activated rewrite_mod.
# sudo a2enmod rewrite
When navigating to www.mysite.ch/pierre/contacts it states:
Not found
The requested URL /contacts was not found on this server.
In my local webserver, it works and I can see the contact page
If I remove my project and add a file phpinfo.php with the function phpinfo(), it show information about the server configuration and I do not have a message "not found"
I suspect that my redirection does not work.
What do you think?
Which are the basic step to make an Apache2 server working with an .htaccess file?
Here are is my .htaccess file (it is the default cakephp file, I have not changed it)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Here is my httpd_conf file
<Directory /var/www/vhosts/metauxch/httpdoc>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order Allow,Deny
allow from All
</Directory>
Alias /pierre /var/www/vhosts/pierre/httpdoc
<Directory /var/www/vhosts/pierre/httpdoc>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order Allow,Deny
allow from All
</Directory>
Do you have some idea?
Change AllowOverride None to AllowOverride All in httpd_conf file
Enable mode rewrite,
sudo a2enmod rewrite
restart apache,
sudo /etc/init.d/apache2 restart
I am sorry, I forgetten to write but I did it
sudo a2enmod rewrite
sudo servive apache2 restart
This is done.
I also change this AllowOverride All
The probleme, now is when I enter
http://eflumpc38.epfl.ch/pierre/contacts
It state that te controller pierre (CakePHP) is missing. While pierre is the root and contacts is the controller.
Second point, when I clink on the Contact (root/contacts/)
I have a such URL
http://eflumpc38.epfl.ch/vhosts/pierre/httpdoc/contacts
instead of
http://eflumpc38.epfl.ch/pierre/contacts
Does some thing is wrong here
Alias /pierre /var/www/vhosts/pierre/httpdoc
<Directory /var/www/vhosts/pierre/httpdoc>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order Allow,Deny
allow from All
</Directory>
When I upload the same CakePHP projet on my usual provider, I do not have this issue.
Thank
try this solution.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ /app/webroot/ [L]
RewriteRule (.*) /app/webroot/$1 [L]
</IfModule>
CakePHP app directory (will be copied to the top directory of your application by bake):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ /webroot/ [L]
RewriteRule (.*) /webroot/$1 [L]
</IfModule>
CakePHP webroot directory (will be copied to your application’s web root by bake):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [QSA,L]
</IfModule>

CodeIgniter remove index.php apache mod_rewrite

I'm using CodeIgniter on a Windows machine using the Zend Comunity Server with apache. My apache is properly configured to handle .htaccess directives and mod_rewrite is enabled (httpd.conf):
<Directory />
Options FollowSymLinks
AllowOverride FileInfo
Order deny,allow
Deny from all
</Directory>
My root is C:\www\ and my site is located in C:\www\david\ and when I type http://localhost/david/, the index.php is loaded along with the correct controller. I want to access my things directly through http://localhost/david/Articles/ instead of http://localhost/david/index.php/Articles/. To do this I have to use apache rewrite module.
To do so, I've placed a .htaccessfile in the C:\www\david\ folder. This file contains code I found on here :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /david/
#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} ^core.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
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
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>
I made a couple of changes to the code since my configuration is a bit different from the default. I'm not on the root so : 'RewriteBase /davidfrancoeur.com/' and I've renamed the CodeIgniter system folder for additional security so :
RewriteCond %{REQUEST_URI} ^core.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
With this done, anything should work properly, I should be able to access http://localhost/david/Articles/ without a problem and I think I shouldn't be able to reach http://localhost/david/core/config/config.php directly.
Unfortunately, it doesn't work, it doesn't even look like any rewriting is done. Do you see if I'm doing something wrong here? Is there a way to know if apache actually rewrite anything?
Thanks a lot. And yes I looked at the millions of article I could found about this... :(
EDIT
CI 2.0.2, PHP 5.3, Apache 2.2
It will work.....actually your first two rewrite conditions and rules are working but in third rewrite condition you are allowing user to access request file ,even if it is core or system file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^core.*
RewriteCond %{REQUEST_FILENAME} !^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
Edit: Fixed 'rewrite' typo
After looking carefully into the httpd.conf of my Zend Community Server, I realized that there was two <Directory />instruction. The first one was empty as shown in the question and the second one looked like that :
<Directory "C:\Dropbox\www">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# 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
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
</Directory>
I changed the following line AllowOverride None to AllowOverride All` and it worked. For those who would like to know more about URL rewrite with CodeIgniter look there : http://codeigniter.com/wiki/mod_rewrite/
And to understand properly what AllowOverridedoes read : http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride
Thanks again to ever helped!
ppet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
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
RewriteRule ^(.*)$ index.php?/$1 [L]