Apache 2 Upgrade Killed Our .htaccess, Can't fix, please help! - apache

We were forced to upgrade to Apache 2 today and as soon as we did our rule set that was working for months stopped working.
The behavior it shows is completely ignoring the .htacess, even after we delete it (.htacess) the server seems to use a "phantom" rule.
IE: site/stuff/ without .htacess should show a 404... but instead it goes to site/stuff.php (no .htacess at all!)
With
.htacess enabled
site/stuff/1/ should go to site/stuff.php?var=1 instead it goes to site/stuff.php
Any help appreciated, it's driving us crazy.

I'm going to guess it's an Apache configuration problem.
Your main httpd.conf has likely set a default for the AllowOverride directive, to None. This is a restrictive set of permissions that improves performance and security, but it means that Apache completely ignores any .htaccess files.
You need to enable AllowOverride for your serving directory, either in the main Apache config file or inside the VirtualHost directive. You can do this by specifying
AllowOverride All
inside your <VirtualHost> or <Directory> block.
EDIT (in response to comments)
Without more information, it will be pretty difficult to diagnose. If you can provide some more details, it would probably help. It does seem that your URLs are being rewritten, so you can try enabling mod_rewrite debugging to see how the rules are getting applied:
<IfModule mod_rewrite.c>
RewriteLog "/path/to/rewrite.log"
RewriteLogLevel 3
</IfModule>
2nd Edit - MultiViews
After re-reading the description of your problem, I think you might have an issue with MultiViews. It sounds very similar to this thread I found. Try disabling MultiViews under your <VirtualHost> or <Directory>, they're probably mucking with your rewrite rules.

Related

mod_rewrite doesnt work if file with same name as argument exists

I'm experiencing some issues with mod_rewrite in htaccess.
Let's for instance say I request example.com/foo/, it works perfectly if I don't have a file starting with "foo.*" in the root directory.
Let's say I have news.php, sitemape.xml, style.css in root, I can't use /news/ or /sitemap/ or /style/ , it will give a 404 like /news.php/ etc.
Here's my rewrite string. It works locally with my Apache 2.2.22 but not at my web-host with the same Apache version.
RewriteRule ^([A-Za-z0-9]+)/?$ index.php?category=$1 [NC,L]
Anyone has a clue?
This sounds like Multiviews rearing its ugly head when its not wanted. It may be that your host automatically turns on the Multiviews option by default, and mod_negotiation then tries to "guess" what the request is for, and if it's close enough (like with /news/ and /news.php), it will automatically serve it, and disregard whatever mod_rewrite rules you may have.
Try turning off multiviews. You can do this in your htaccess file using the Options directive (assuming your host has allowed Options):
Options -Multiviews

Rewrite not working

I am absolutely lost, trying anything silly. Probably you know the situation. I tried hints from many threads here and on Google as well, but nothing helped. I have rewrite module loaded on my Apache (checked by phpinfo). For a site I have .htaccess created, where currently simple rule is stored:
RewriteEngine on
RewriteBase /
RewriteRule ^/certifikaty\.html$ certifikaty.php
So, I expect http://www.contech.cz/certifikaty.html will show http://www.contech.cz/certifikaty.php. Unfortunatly not. So, I set in my httpd.conf these lines:
RewriteLogLevel 9
RewriteLog "logs/rewrite.log"
The file (rewrite.log) is created, but it's empty even after server restart.
Please, could you help me to get out of this?
Marek
I found the solution - I had directive AllowOveride None in my httpd.conf. It means all httpd.conf are ignored ....

Serving a directory outside DocumentRoot with Apache

I want exampleapp.com/clientapp/ to execute the index.php in /usr/local/apache/htdocs/clientapp.
This stackoverflow question outlines something similar to what I want to do: https://stackoverflow.com/a/8454/173630
I'm having some trouble getting this working. Here's the start of my VirtualHost setup:
<VirtualHost exampleapp.com:80>
DocumentRoot /home/platform/src/serverapp/public
RewriteEngine On
Alias /clientapp/ "/usr/local/apache/htdocs/clientapp"
<Directory "/usr/local/apache/htdocs/clientapp">
FallbackResource index.php
<IfModule mod_suphp.c>
suPHP_UserGroup nobody nobody
</IfModule>
</Directory>
RewriteRule ^/clientapp/(.*)$ /clientapp/$1 [PT]
I'm using Apache 2.2.22. I know this is kind of a confusing setup -- the reason I'm doing this is to avoid cross-domain AJAX requests from clientapp to serverapp.
With this configuration I'm not getting any errors, it's just falling through to the server app.
Update
The problem was that I had a BasicAuth set up on /usr/local/apache/htdocs/clientapp, and the password wasn't being prompted for when visiting exampleapp.com/clientapp/ and it was just silently failing. I took off the BasicAuth for now, which gets it to work.
After trying a few things out, it looks like this is just mod_alias being retarded and either blindly mashing together file-path and URI-path or mistaking the file-path (/usr/local/apache/htdocs/clientapp) for a URI-path (not sure how). Either way, you can do one of two things, it looks like.
Add a trailing slash to your file-path:
Alias /clientapp/ "/usr/local/apache/htdocs/clientapp/"
Remove the trailing slash from your URI-path:
Alias /clientapp "/usr/local/apache/htdocs/clientapp"
Both seems to do the trick, but I would suggest doing the second option, as it would match requests for exampleapp.com/clientapp (no trailing slash), and mod_dir will properly recognize it as a directory and redirect you to exampleapp.com/clientapp/. Whereas if you go with first option, going to exampleapp.com/clientapp would just give you a 404 (or something in the document root ends up handling it).
Change <Directory ...>...</Directory> to <Location /clientapp>...</Location> and keep everything inside it.

mod_rewrite 'add path info postfix:'

Why is my mod_rewrite doing this?
add path info postfix: /home/mobelluk/public_html/about.php -> /home/mobelluk/public_html/about.php/
which results in an unwanted trailing slash on EVERYTHING.
I have disabled all my .htaccess rules so they're out of the equation.
apparently there's been an issue with mod_rewrite re-appending post-fix part in certain cases
https://issues.apache.org/bugzilla/show_bug.cgi?id=38642
The problem:
If multiple RewriteRules within a .htaccess file match, unwanted copies of PATH_INFO may accumulate at the end of the URI.
If you are on Apache 2.2.12 or later, you can use the DPI flag to prevent this
http://httpd.apache.org/docs/2.2/rewrite/flags.html
In searching for "add path info postfix", this question comes up first and while it eventually did solve my problem, it took me almost 2 hours to understand what was going on.
In working on a site, I needed this rewrite:
/resources/band/ -> resources.html?section=band
Accomplished with this mod_rewrite:
RewriteRule ^resources/(.*)/$ resources.html?section=$1 [L]
Changing that to [DPI] did nothing... The code on my resources.html page was 100% for sure being called but the argument of section=band was not being sent to it.
Get this... in case you find Apache's documentation impossible to read, Multiviews is the problem. When the browser sees that multiviews is on the server sees /resources/band/ and say "Oh, I'm so smart, I know what that means!" and redirects:
/resources/band/ -> /resources.html/band/
True story! I changed the +Multiviews to -Multiviews on the virtual host - problem instantly solved.
Is it possible the new server has mod_dir loaded, with DirectorySlash On where the old one did not and that is leading to this problem?
(Note that DirectorySlash On is the default if mod_dir is loaded and nothing is overriding it)
I solved this issue by disabling MultiViews in my virtual host Options configuration. I was rewriting something similar to below:
Desired rewrite:
/dir/ -> /dir.html
Actual translations:
/dir/ -> /dir.html (MultiViews)
/dir.html -> /dir.html/ (mod_rewrite: 404, didn't exist)
Disabling MultiViews kept the initial translation from taking place. I could have probably adjusted the rewrite rule to compensate for this, but I wasn't using MultiViews for anything else anyway.
The following post tipped me off on this issue:
https://velenux.wordpress.com/2012/07/17/apache-mod_rewrite-multiple-add-path-info-postfix/#comment-1476

Do you have to restart apache to make re-write rules in the .htaccess take effect?

I have pushed my .htaccess files to the production severs, but they don't work. Would a restart be the next step, or should I check something else.
A restart is not required for changes to .htaccess. Something else is wrong.
Make sure your .htaccess includes the statement
RewriteEngine on
which is required even if it's also present in httpd.conf. Also check that .htaccess is readable by the httpd process.
Check the error_log - it will tell you of any errors in .htaccess if it's being used.
Putting an intentional syntax error in .htaccess is a good check to make sure the file is being used -- you should get a 500 error on any page in the same directory.
Lastly, you can enable a rewrite log using commands like the following in your httpd.conf:
RewriteLog "logs/rewritelog"
RewriteLogLevel 7
The log file thus generated will give you the gory detail of which rewrite rules matched and how they were handled.
No:
Apache allows for decentralized management of configuration via special files placed inside the web tree. The special files are usually called .htaccess, but any name can be specified in the AccessFileName directive... Since .htaccess files are read on every request, changes made in these files take immediate effect...
From the apache documentation:
Most commonly, the problem is that AllowOverride is not set such that your configuration directives are being honored. Make sure that you don't have a AllowOverride None in effect for the file scope in question. A good test for this is to put garbage in your .htaccess file and reload. If a server error is not generated, then you almost certainly have AllowOverride None in effect.
Only if you have not added the mod_rewrite module to Apache.
You only need to restart Apache if you change any Apache ".conf" files.
I have the same issue and it seems PiedPiper post about AllowOverride were most helpful. Check your httpd.conf file for "AllowOverride" and make sure it is set to All.
In case of .htaccess restart is not required if it is not working probable reasons include.
AllowOverride May not be set which user can set inside httpd.conf or might have to contact server admin.
Check the file name of .htaccess it should be .htaccess not htaccess.txt see here for guide how to create one.
Try to use Options -Indexes or deny all kind of simple directive to see if it is working or not.
clear browser cache everytime if having rule for redirects or similar if previous redirect is cached it appears as if things are not working.
What's in your .htaccess? RewriteRules? Check that mod_rewrite is installed and enabled.
Other stuff? Try setting AllowOverride to 'all' on that directory.