Apache throwing 403 on www.example.com/dir/.php - apache

I've discovered from Google Webmaster Tools that Google has found a bad link somewhere that is throwing a 403 error on my server.
The url is like this:
http://www.example.com/directory/.php
I don't know how that url has come about and the site is too complicated for me to find out, but I'd like to simply place a 301 redirect to:
http://www.example.com/directory/
I've put the correct rule in .htaccess for the redirect, but it doesn't appear to be triggered. It's almost as if the 403 is being generated before .htaccess is processed. Does anyone know why this might be and how I can successfully get the user redirected to the new page?

Except for the fact that the rule may be indeed incorrect, this could also be because this involves looking for a file with a filename starting with a ..
These files are usually "hidden" from the outside world, so it might be that either your Apache (configuration) or your OS does not allow serving this file.
Without knowing more about the configuration, this cannot be analyzed though. You could test it by creating a file called ".test" and see if that's reachable.

Related

apache keeps old .htaccess settings (in rewriterule)

I am testing some basic .htaccess rewriterules and I am pretty sure that even after deleting a rule in .htaccess and saving the document, apache ignores it and acts like the rule was still there (only for earlier requested sites) - even after apache restart. It is driving me crazy, the behavior is really mad. Is there any apache cache which is about to be cleaned or the problem is elsewhere?
It's madness! Not really, it's just annoying.
When you set a redirect in Apache what you're doing is actually configuring headers that are passed to your browser. Your browser keeps a cache of 301 and 302 redirect headers (and other things like DNS) in the event you go back to those pages, so it can save you time. 301 redirects are supposed to be permanent (that's what you're indicating), so if there is no need for the browser to do something like perform a DNS lookup or visit a page based on a redirect, then it will cut corners and use the cache in order to provide you with a faster browsing experience.
You need to flush your browser's cache to prevent the store redirects from happening.
Additionally, you'll want to resubmit your page to Google (in the event it's on the web) in case they've crawled your site while the invalid headers existed.

Aliases on Dreamhost, general management of http request / server errors

I had a hard time deciding how I should manage these errors (404, 500, ...) and when I finally decided, I am encountering problems. This is a reeeeeally long question, I appreciate anyone's attempt to help!
Let me first describe how I decided to set it up. I have several sites hosted on a shared Dreamhost account. In the folder structure that I see, everything of mine on the server is under /home/username, and for example, site1.com's web root is at /home/username/site1.com
I am creating a generic error handler (php script) for errors like 404 not found, 500, etc. that I want to store above the web roots of my sites at /home/username/error_handler/index.php so that I can use an .htaccess file at /home/username/.htaccess which includes something like the following:
ErrorDocument 404 /error_handler/index.php
ErrorDocument 500 /error_handler/index.php
...and many more
When these errors occur on any of my sites, I want it to be directed to /home/username/error_handler/index.phpThis is the problem I'm having a hard time figuring out. The ErrorDocument directives above will actually cause Apache to look for /home/username/site1.com/error_handler/index.php
Anyway, the errors should be redirected to my error handling php script. The script will use $_SERVER['REDIRECT_STATUS'] to get the error code, then use $_SERVER['REDIRECT_URL'] and $_SERVER['HTTP_HOST'] to decide what to do. It will check if an error handler specific to that site exists (for example: site1.com/errors/404.php). If this custom page doesn't exist, it will output a generic message that is slightly more user-friendly and styled, and perhaps will include some contact info for me depending on the error.
Doing it this way lets me funnel all these errors through this 1 php script. I can log the errors however I like or send email notifications if I want. It also lets me set up the ErrorDocument Apache directives once for all my sites instead of having to do it for every site. It will also continue to work without modification when I move the site around, since I already have a system that scans the folder structure to figure out where my site roots are when they really aren't at the web root technically speaking. This may not be possible with other solutions like using mod_rewrite for all 404 problems, which I know is common. Or if it is possible, it may be very difficult to do. Plus, I have already done that work, so it will be easy for me to adapt.
When I am working on sites for which I don't have a domain name yet (or sites where the domain name is already in use at the moment), I store them temporarily in site1.com/dev/site3.com for example. Moving the site to site3.com eventually would cause me to have to update the htaccess files if I had one for each site. Changing the domain name would do the same.
Ex: a site stored at site1.com/dev/site3.com would have this in its htaccess file:
ErrorDocument 404 /site1.com/dev/site3.com/error/404.php
And it would have to be changed to this:
ErrorDocument 404 /site3.com/error/404.php
Obviously, this isn't a huge amount of work, but I already manage a lot of sites and I will probably be making more every year, 95% of which will be hosted on my shared DreamHost account. And most of them get moved at least once. So setting up something automatic will save me a some effort in the long run.
I already have a system set up for managing site-relative links on all my sites. These links will work whether the site exists in a subdirectory of an existing site, or in their own domain. They also work without change in a local development server despite a difference in the web root location. For example, on the live server, the site-relative http link /img/1.jpg would resolve to the file /home/username/site1.com/img/1.jpg while on my local development server it would resolve to C:\xampp\htdocs\img\1.jpg, despite what I consider the logical site root being at C:\xampp\htdocs\site1.com. I love this system, and it is what gave me the idea to set up something that would work automatically like I expected it to, based on the file structure I used.
So, if I could get it to work, I think this seems like a pretty good system. But I am still very new to apache configuration, mod_rewrite, etc. It's possible there is a much easier and better way to do this. If you know of one, please let me know.
Anyway, all that aside, I can't get it working. The easiest thing would be if I could have the ErrorDocument directive send the requests to folders above the web root. But the path is a URL path relative to the document root. Using the following in /home/username/.htaccess,
ErrorDocument 404 /error_handler/index.php
a request for a non-existent resource causes Apache to look for the file at
site1.com/error_handler/index.php
So I thought I should set up a redirection (on all my sites) that would redirect those URLS to /home/username/error_handler. I tried a few things and couldn't get any of them to work.
Alias seemed like the simplest solution, but it is something that has to be set at server runtime (not sure if that is the right terminology - when the server is started). On my local server, it worked fine using:
Alias /error_handler C:\xampp\htdocs\error_handler2
I changed the local folder to test that the Alias was functioning properly. (On the local server, the URL path specified by the ErrorDocument directive is actually pointing to the right folder, since in my local server the web root is technically C:\xampp\htdocs and I store the error handler I want to use is stored locally at C:\xampp\htdocs\error_handler\index.php)
Dreamhost has a web client that can create what I am guessing is an Alias. When I tried to redirect the folder error_handler on site1.com to /home/username/error_handler, it would seem to work right if I typed site1.com/error_handler in the browser. But if I typed site1.com/test1234 (non-existant), it would say there was a 404 error trying to use the error handler. Also, I would have to login through the web client and point and click (and wait several minutes for the server to restart) every time I wanted to set this up for a new site, even if I could get it to work.
So I tried getting it to work with mod_rewrite, which seems like the most flexible solution. My first attempt looked something like this (stored in /home/username/site1.com/.htaccess for now, though it would eventually be at /home/username/.htaccess:
RewriteEngine On
RewriteRule ^error_handler/index.php$ /home/username/error_handler/index.php
The plain english version of what I was trying to do above is to send requests on any of my sites for error_handler/index.php to /home/username/error_handler/index.php. The mis-understanding I had is that the subsitution will be treated as a file path if it exists. But I missed that the documentation says "(or, in the case of using rewrites in a .htaccess file, relative to your document root)". So instead of rewriting to /home/username/error_handler/index.php, it's actually trying to rewrite to /home/username/site1.com/home/username/error_handler/index.php.
I tried including Options +FollowSymLinks because in the Apache documentation it says this:
To enable the rewrite engine in this context [per-directory re-writes in htaccess], you need to set "RewriteEngine On" and "Options FollowSymLinks" must be enabled. If your administrator has disabled override of FollowSymLinks for a user's directory, then you cannot use the rewrite engine. This restriction is required for security reasons.
I searched around for a while and I couldn't find anything about how Dreamhost handles this (probably because I don't know where to look).
I experimented with RewriteBase because in the Apache documentation it says this:
"This directive is required when you use a relative path in a substitution in per-directory (htaccess) context unless either of the following conditions are true:
The original request, and the substitution, are underneath the DocumentRoot (as opposed to reachable by other means, such as Alias)."
Since this is supposed to be a URL path, in my case it should be RewriteBase /, since all my redirects will be from site1.com/error_handler. I also tried Rewrite Base /home/username and RewriteRule ^error_handler/index.php$ error_handler/index.php. However, the Rewrite Base is a URL path relative to the document root. So I need to use something like an alias still. The implication in the quote from the documentation above is that it is possible to use mod_rewrite to send content above the web root. One of the many things I don't know is what the 'other means' besides Alias might be. I believe Alias might not be an option on Dreamhost. At least I couldn't make sense of it.
Why not use error pages in the site root, then include the actual file from the shared section?

Apache strange behaviour: page loads even when request points to non-existing file

A client has a website running on a dedicated host (either developed, nor managed by myself) in which Apache is generating a very strange behavior.
When the requested url is of the type:
http://obituarieshelp.org/aboutus.html/whatever_here
it actually loads 'aboutus.html' if it exists, even though 'whatever_here' does not exist, and 'aboutus.html' is not a directory.
I took a look at the htaccess file, and everything seems to be fine. I am sure it is apache misconfigured somehow, but for the love of me, I cannot seem to find anything that could point to the problem in httpd.conf
Has anyone experienced the same, or has any suggestions as to why this would be?
This is controlled by the AcceptPathInfo HTTPd directive. The remainder of the path after the file is made available in the PATH_INFO environment/server variable, and scripts can use it to discern additional information about the request.

Weird apache behavior when trying to display urls without html extension

I have a url that is easily accessible when you request it as:
http://example.com/2005/01/example.html
or
http://example.com/2005/01/example
(I actually don't know why it works without the extension. Instead, it should return the usual 404 error.)
However, when I try to access the following url:
http://example.com/2005/01/example/
(note the trailing slash)
I get a 404 Not found error but with the requested url printed as:
http://example.com/2005/01/example.html/
So, it seems the ".html" part was automatically added by apache.
My question is: how do I disable this behavior? I need to do it because I want add mod_rewrite rules to hide the html extension, so that I can access that url as:
http://example.com/2005/01/example/
My apache is 2.2.9 on Ubuntu 8.10.
Thanks!
MultiViews could cause this behavior. Try to disable it:
Options -MultiViews
Is example.html an actual file that lies in the directory path 2005/01? It seems like mod_rewrite is already active. If you use a blog or content management system on your server, then it probably does stuff to your url's already.

Mask redirect to temporary domain with mod_rewrite

We are putting up a company blog at companyname.com/blog but for now the blog is a Wordpress installation that lives on a different server (blog.companyname.com).
The intention is to have the blog and web site both on the same server in a month or two, but that leaves a problem in the interim.
At the moment I am using mod_rewrite to do the following:
http://companyname.com/blog/article-name redirects to http://blog.companyname.com/article-name
Can I somehow keep the address bar displaying companyname.com/blog even though the content is coming from the latter blog.companyname.com?
I can see how to do this if it is on the same server and vhost, but not across a different server?
Thanks
Rather than using mod_rewrite, you could use mod_proxy to set up a reverse proxy on companyname.com, so that requests to http://companyname.com/blog/article-name are proxied (rather than redirected) to http://blog.companyname.com/article-name.
Here are more instructions and examples.
There is functionality with ZoneEdit called webforwards which could probably do this and hide what you are actually doing (unless someone looked into it).
The only thing that mod_rewrite can do is send HTTP header redirects, and those redirects (across servers) always result in the browser address bar reflecting the reality.
You should instead consider writing a 404 script that 'reflects' the blog. This would essentially be a transparent proxy, and many are already written.
The script would find if the requested page (that was 404'd) started with http://mycompany.com/blog/ . If it did, it would download and then send onto the client the blog page and associated files (probably caching them as well).
So requesting http://mycompany.com/blog/article_xyz would cause the 404 script to download and send http://blog.companyname.com/article_xyz.
It's probably more work than it's worth, but you might be able to design a simple enough 404 script that it's worthwhile.
-Adam