mod_rewrite doesnt work if file with same name as argument exists - apache

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

Related

Apache caching urls, RewriteEngine causing problems

I was trying out the Apache Rewrite Engine, but was having problems because it cached it's responses. Now if I change the file, it does not do what the file says, but does what it did when I tried the same url before.
For example, I tried to type in
localhost/api
but I had not yet set a rule that would match that. The server gave me a 404 error. I tried to add a rule that would work with this url:
RewriteRule api/? api.php [L]
but it still gave me the same error. If I try an address like localhost/lapi which I didn't enter before I added the rule, it works. Is there a way to clear this cache and restart? I tried restarting apache using apachectl -k restart and apachectl -k graceful but I still had the same problem. Does anyone know how to fix this problem?
First of all, depending on the context of your rule, you may need a leading slash as in:
RewriteRule api/? /api.php [L]
Regarding the caching, do you have mod_cache enabled? If so, disable it and restart your server. mod_rewrite does not do any caching on its own. Also be sure to completely clear your browser cache and be aware that if you are not accessing your server over the same local network, there may be other caching taking place along the way.
This problem can also be caused by the MultiViews option, see Apache doc. Try omitting Option MultiViews from httpd.conf or add Options -MultiViews to .htaccess.
(The MultiViews option enables Apache to choose automaticaly between several versions of the file based on the file name extension and content preferences present in the HTTP request (i.e. something you probably don't want to happen). It can lead to this kind of bizzare failure when a rewrite rule just removes the script's extension.)

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.

Apache Mod_ReWrite Suddenly Stopped Working

I had mod_rewrite set on my server to rewrite a url like the following
http://www.example.com/1
to
http://www.example.com/index.php?show=1
In order words a URL shortern. Everything was working fine when the system was running under a sub-domain on my development site, but now it just generates a Not Found error, although if I manually enter the url /index.php?show=1 it works fine.
So the only changes is the urls switching from
http://www.site.example.com
to
http://www.site.com
however it's still running on the same server and the same sub-folder inside public_html on the server just the new domain name has been pointed to that folder.
The folder it's stored in is /public_html/paste
The full .htaccess file running in the directory is
# Set Default File
DirectoryIndex index.php
# Turn ReWrite Engine On
RewriteEngine on
# Create Rule To Write URLs To Shorter Versions
RewriteRule /([a-z0-9]+) /index.php?show=$1
I can't enable RewriteLog as the hosting doesn't allow it for some reason.
It sounds like the AllowOverride directive is not properly set for that folder. In your Apache configuration, you should make sure that the Directory or Vhost you're using for the primary domain has the AllowOverride set to All
http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride
You probably need to specify the RewriteBase directive.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
I'll also note that Options +FollowSymlinks would be good to have in there too in case you ever turn it off further up the config chain (rewrite wont work without it).

Is it necessary to set [DirectoryIndex] while not using index.php?

My sites root access is managed by htaccess: it redirects various aliases to their own home files /en/home for english /de/home for Deutsch etcettera. Previously, I used index.php to route and redirect all that, and hence the DirectoryIndex had something like this:
DirectoryIndex /index.php
Now, however, there is no index.php file, so I commented it
# DirectoryIndex /index.php
Would it be better to uncomment is and set it to the default /en/home (with or without .php because in this case? I have set up rules sohat my pages in browser also work when no extension is given)
DirectoryIndex /en/home
In all the above cases, my websites work fine and I don't see ANY change when I set either of the three instances as above. but ... "there's gotta be one best ain't it?"
Thanks!
If you have the rules written in .htaccess it is best not to repeat the rules in whatever php config and routing functions you are using. Routing through apache (your .htaccess) is much faster than subverting routes through php, though you will not realize the gains without a pretty high volume of traffic.

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