mod_rewrite works in .htaccess files but not in apache2.conf - apache

I have the following in a .htaccess file as a test:
RewriteEngine on
RewriteRule ^(.*)$ /backend/$1
This works as expected. I was able to get rewrite logging working with the following in apache2.conf:
<IfModule mod_proxy.c>
RewriteLog "/tmp/REWRITE.log"
RewriteLogLevel 9
</IfModule>
The log file is created and logs debug info as expected. However, when I delete the .htaccess file, change the apache2.conf directive as follows, and restart apache to do the equivalent globally, it doesn't work.
<IfModule mod_proxy.c>
RewriteLog "/tmp/REWRITE.log"
RewriteLogLevel 9
RewriteEngine on
RewriteRule ^(.*)$ /backend/$1
</IfModule>
I'm using Apache/2.0.55 on Ubuntu.
Help!

Have you tried this ?
RewriteEngine on
RewriteRule ^(.*)$ /backend/$1
<IfModule mod_proxy.c>
RewriteLog "/tmp/REWRITE.log"
RewriteLogLevel 9
</IfModule>
Because this way I was able to do rewriting, but log didnt worked hence a issue in mod_proxy.c

Have you considered turning RewriteEngine on before any other rewrite directives?

Are your RewriteRule directives contained within a particular server definition? Do you have virtual servers at work?
The reason I ask this is that you say this works in an .htaccess file which is directory specific; although officially the documentation says that RewriteRule can apply to server config, virtual host, directory, .htaccess.

Related

Deploy a CakePHP website fails. An apache2 config issue?

I'm deploying a CakePHP (1.3.2) website for the first time. It was hosted on an older server. I received the project as a zip file. I managed to install it on my localhost and made the changes I needed.
Now I have to deploy it to a new server, but I face a problem.
The routing doesn't seem to work. I guess it's an .htaccess issue.
When I access the root folder, it redirects me to /login but then I have a 404:
The requested URL /login was not found on this server.
My 3 main .htaccess files (/, /app and /app/webroot) are the following. (CakePHP is installed at the root of my virtual host)
Root
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
/app
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
/app/webroot
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
mod_rewrite seems to be activated on my server as it responds with this when I try to add it:
Module rewrite already enabled
But when I try something simple like that on top of my root .htaccess, it doesn't do anything:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^.*$ test.php
</IfModule>
(I was following this guide: https://docs.bolt.cm/howto/making-sure-htaccess-works)
Among a lot of things, I also tried to add that to all my .htaccess:
Option Indexes
But it didn't help.
Here is my website conf file too:
<VirtualHost xx.x.xx.xx:80>
ServerAdmin xxx#company.com
ServerName xxx.company.com
DocumentRoot /var/www/xxx.company.com
DirectoryIndex index.html index.php
php_value error_log "/var/log/apache2/xxx.company.com-phperror.log"
php_flag register_globals off
<Directory "/var/www/xxx.company.com">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/xxx.company.com-error.log
CustomLog /var/log/apache2/xxx.company.com-access.log common
</VirtualHost>
(I added the Directory section that was not here in the first place)
After a lot of trials, I still haven't found anything that seems to solve my problem.
As I'm definitely not used to work on server side, It might be a simple thing that you will immediately spot. I hope so.
Thanks
I finally got it to work. Here are the two mistakes I made:
I had my document root set cake's root folder instead of the webroot folder. I added /app/webroot after DocumentRoot /var/www/xxx.company.com in my .conf file.
Also, I was using apache's reload function, which is actually not properly reloading. Using service apache2 restart instead does the job.

AllowOverride and RewriteCond/RewriteRule interactions

I'm trying to set up a site running on Apache 2.4.16 to redirect all www URLs to non-www URLs. I'm using HTML5 Boilerplate's Apache configs to do this (as well as everything else they provide).
https://github.com/h5bp/server-configs-apache/blob/master/dist/.htaccess
This happens on line 380, seen below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
</IfModule>
I'm using Include to add the whole file to my vhost config for the site, as well as an AllowOverride All for another .htaccess file at my doc root (same one that comes with Laravel 5):
production.vhost.conf (relevant part)
<Directory /var/www/hostname/production>
AllowOverride All
# Include H5BP server configs
Include server-configs-apache/dist/.htaccess
</Directory>
.htaccess (at doc root)
<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]
</IfModule>
Now, almost everything from H5BP's .htaccess was working, except for the redirect from www to non-www. After poking around I noticed that the redirect was only working when I'd remove AllowOverride All from the <Directory> block in the vhost. So the doc root .htaccess was somehow overriding the rewrite conditions.
I've actually already fixed my initial issue by moving the doc root .htaccess contents into the vhost file and removing the AllowOverride, but I'm more curious as to why this was happening; more specifically how AllowOverride interacts with RewriteCond and RewriteRule.
My hunch is that the .htaccess in my doc root was overriding the www to non-www redirect, but I'm not sure why that one specifically. For example, the http -> https redirect worked without issue (line 352 of H5BP, uncommented out in mine), it seemed to be just that one redirect. I didnt even think that those rules could be overridden since RewriteCond/RewriteRules feel unique to me.
If there are any, what are the rules that determine how an .htaccess can override a rewrite rule?
If there are any, what are the rules that determine how an .htaccess
can override a rewrite rule?
Conditions and rules don't dictate how .htaccess works. AllowOverride is what allows .htaccess usage. If you have AllowOverride All then .htaccess is allowed, if you have AllowOverride None, then it's not and it will be ignored. In 2.4 None is the default.
.htaccess is per directory so it will take precedence if it's located in a directory that has rules applied as long as .htaccess file usage is allowed there. Which is confgiured in the server config in VirtualHost or Directory directives.
Also using an include for .htaccess in a vhost is a very bad configuration. If you have access to the vhost file or the config, you should create another config and include it with the .htaccess contents.
You should not be using .htaccess files at all actually with access to the server config. See this apache recommendation on not using .htaccess.
https://httpd.apache.org/docs/2.4/howto/htaccess.html#when

Mod_ReWrite doesn't work on cakephp

I have recently deployed CakePHP however mod_Rewrite is not currently working:
I have the following in my apache2.conf
<Directory "/path/to/the/app">
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
i have run
sudo a2enmod rewrite
which stated the module is already enabled and i have also checked the .htaccess file which has
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
has anyone got any other ideas as to why mod_rewrite isn't working?
Please note i have restarted apache with no sucess
There must be 3 .htaccess files in these locations
/.htaccess
/app/.htaccess
/app/webroot/.htaccess
Do you have them?
Turns out mod_rewrite was working, however I was using cakephp 1.3 which doesn't detect if the mod_rewrite works but applys a CSS style which wasn't applying to the div. it works now anyways

redirecting (.htaccess) is not working (NOT RELEVANT ANYMORE)

My .htaccess file is not working.
It looks like the mod_rewrite module is not loaded.
One way to check this to run <?php phpinfo() ?>.
But when I do this the phpinfo pages appeart, but there's no part with 'apache2handler'.
So I can't check if the mod_rewrite module is loaded. I can't see which apache config file is loaded.
Is there anyone who can help me? I'm stuck with this problem for hours now.
This is my .htaccess
RewriteEngine On
#exclusions
RewriteCond %{REQUEST_URI} !images/
RewriteCond %{REQUEST_URI} !external/
RewriteCond %{REQUEST_URI} !css/
#redirect everything else
RewriteRule ^(.*) index.php
Options -Indexes
It is placed in a subfolder of a website, like www.example.com/subfolder/
This .thaccess file works on localhost and on an other website.
This doenst result in an 500 error.
<IfModule mod_rewrite.c>
if mod_rewrite is enabled, you'll get 500 error on the server
</IfModule>
EDIT
I discoverd that here is no apache running on the server, its IIS....
Thanks for help anyway.
If you doubt that mod_rewrite is not working on your apache, and you can't check it in phpinfo() there are plenty more ways to check it. In case you don't have terminal access to the server, you can check by .htaccess
<IfModule mod_rewrite.c>
if mod_rewrite is enabled, you'll get 500 error on the server
</IfModule>

Making pretty permalinks work in WAMP

I am not able to switch to pretty permalinks in WAMP. Changing to any form other than default gives 404 error.
I have switched on the rewrite_module in Apache. I Googled the problem and found that following changes should be made to httpd.conf file. My httpd.conf file stands as
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
I also checked that the .htaccess file is getting created. It reads as
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /vit%20web/events/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /vit%20web/events/index.php [L]
</IfModule>
# END WordPress
All you need to do is turn on the mod_rewrite in the wamp settings tab.
Click Wamp -> Apache -> Apache Modules -> rewrite_module
If you select that then turn on %postname% it should work
Did you reboot Apache after editing httpd.conf?
A bulletproof check for mod_rewrite is to remove the <IfModule>...</IfModule> tags and try running WordPress - if you get a 500 Server Error, mod_rewrite isn't installed.
Also I would recommend changing <Directory /> to <Directory "C:/path/to/server/root"> (note the forward slashes too, even on Windows).
And the deny order should be switched if you're only on a development server;
Order allow,deny
Allow from all
The problem may be:
Whenever you have permalinks in subfolder of your main www folder (i.e.
RewriteBase /subdirectory/
you may need to have a .htaccess file in your main c:\wamp\www folder too.