Serving a directory outside DocumentRoot with Apache - 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.

Related

Apache AliasMatch Creating Infinite 301 Redirects

while trying out AliasMatch I found a curious behaviour of it.
Let's say you have rules like this in the httpd.conf:
AliasMatch "/goinfinite/" "C:/test/"
<Directory "C:/test">
Require all granted
</Directory>
The directory contains an index.php. However, said php file is not used as directory index. Instead, a 301 redirect is created to http://localhost/goinfinite/index.php/. From that page a redirect is created to http://localhost/goinfinite/index.php/index.php/ and so on.
My goal was to just forward any URL that contains /goinfinite in its path to C:/test and use its index.php file, however I created an infinity loop of 301 redirects. I am unsure what causes these explicit redirects which then trigger the AliasMatch rule again.
I have read the Apache documentation. This is what would be needed for AliasMatch to work like Alias:
AliasMatch "^/image/(.*)$" "/ftp/pub/image/$1"
In my case, I could do something like
AliasMatch "/goinfinite/(.*)" "C:/test/$1"
However, if the path is simply /goinfinite/ $1 would not contain anything. So why would it work in this case?
My question is an academic one, I would like to understand why the directory index is not used, but a redirect triggered instead.

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

RewriteRule not working in httpd.conf

I'm trying to make the following occur:
When I go to http://www.mysite.com/b-pogs/20 (or http://www.mysite.com/b-pogs/20/), it internally redirects to http://www.mysite.com/b-pogs/index.php?comic=20.
The way I'm trying to do this is as follows (and I'll include everything that I think is relevant).
All of this occurs in my httpd.conf.
By the way, going directly to index.php and using getters works perfectly right now.
Alias /b-pog /var/www/b-pog
<Directory /var/www/b-pog>
RewriteEngine on
RewriteRule ^/b-pog/([0-9]+)$ /b-pog/index.php?comic=$1 [PT]
</Directory>
When I go to http://www.mysite.com/b-pog/20 (or http://www.mysite.com/b-pog/20/), it gives me a The requested URL /b-pog/20/ was not found on this server.
I solved my problem (I believe). If anyone wants to correct this solution, please do. It seems kind of wrong.
I just removed the <Directory> and </Directory> parts because it wouldn't really matter regardless. I'm not sure if there's anything they would do, anyways.
It seems to be working.

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).

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