logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in  RSS Apache Lounge  


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.



Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: Using Alias (mod_alias) with .htaccess files
Author
cbj4074



Joined: 02 Nov 2012
Posts: 24
Location: United States

PostPosted: Wed 20 Feb '13 23:59    Post subject: Using Alias (mod_alias) with .htaccess files Reply with quote

Hi, everyone,

I posted this to the Apache httpd Users mailing list, but nobody there has responded in several days. (This may be an unfortunate side-effect of answering far more questions than I ask; people tend to skip-over my posts because they assume that I'm answering a question and not asking one.)

I'm using mod_alias to map URLs to directories that are outside of Apache's DocumentRoot. However, it seems that .htaccess files are being ignored in these directories.

File: my-customizations.conf (included in httpd.conf)
Code:

Alias /project-one "C:/Users/Ben/Documents/Projects/one-svn/trunk"
Alias /project-two "C:/Users/Ben/Documents/Projects/two-svn/trunk"

<DirectoryMatch
"^C:/Users/Ben/Documents/Projects/.*-svn/(trunk|branches|tags)/">
    Options +Indexes +FollowSymLinks

    AllowOverride All

    Include conf/auth.conf
</DirectoryMatch>


File: "C:/Users/Ben/Documents/Projects/one-svn/trunk/public/.htaccess"
Code:

RewriteEngine on
Options All

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


The first block seems to have the intended effect. I'm able to access each project at the designated alias, and my authentication rules are applied.

This block is definitely effective, because if I remove it, I receive "access denied", as expected.

However, it appears that the .htaccess file is being ignored entirely. If I cram a bunch of syntactically-invalid junk into the .htaccess file, no error occurs. The directory index file (index.php in this case) is loaded without errors.

I see the following excerpt among mod_info's output:

Code:

In file: C:/Program Files/apache/conf/my-customizations.conf
   3: <DirectoryMatch
"^C:/Users/Ben/Documents/Projects/.*-svn/(trunk|branches|tags)/">
   16:   Options +Indexes +FollowSymLinks
   23:   AllowOverride All
     : </DirectoryMatch>


Given that "AllowOverride All" is present, shouldn't the .htaccess file be honored?

This post seems to describe the same issue: http://stackoverflow.com/questions/8376590/htaccess-is-ignored-when-using-an-aliased-uri .

Unfortunately, the "answer" doesn't address the fact that the .htaccess file isn't loaded at all, in which case the mod_rewrite peculiarities are irrelevant.

I'm using Apache 2.4 on Win32.

Any help is very much appreciated.

Thank you!
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 6785
Location: Germany, Next to Hamburg

PostPosted: Thu 21 Feb '13 20:28    Post subject: Reply with quote

Do not put the AllowOverride All in DirectoryMatch but Directory See http://httpd.apache.org/docs/current/mod/core.html#allowoverride
Back to top
cbj4074



Joined: 02 Nov 2012
Posts: 24
Location: United States

PostPosted: Thu 21 Feb '13 21:29    Post subject: Reply with quote

You're a good man, James Blond. You were "right on the money", so to speak.

I had to move the AllowOverride directive from a <DirectoryMatch> stanza to a <Directory> stanza.

Unfortunately, this restriction, which is for my own good, I'm sure, means that I'll have to hard-code the directory path for every project that I set-up.

The only alternative that I can see is to place "AllowOverride all" into the <Directory /> stanza, but that's a terrible idea for a number of reasons, not the least of which are elucidated in the manual:

Quote:

For security and performance reasons, do not set AllowOverride to anything other than None in your <Directory /> block. Instead, find (or create) the <Directory> block that refers to the directory where you're actually planning to place a .htaccess file.


If I had 50 projects, I'd have to create 50 <Directory> blocks. I guess I'll just buckle and move all of my projects into Apache's document root, as much as I hate to do it.

Thank you so much for the solution.
Back to top
glsmith
Moderator


Joined: 16 Oct 2007
Posts: 2243
Location: Sun Diego, USA

PostPosted: Fri 22 Feb '13 3:21    Post subject: Reply with quote

Not necessarily, if you think through your project locations properly.

<Directory "C:/projects/*/public_html">
Back to top
cbj4074



Joined: 02 Nov 2012
Posts: 24
Location: United States

PostPosted: Fri 22 Feb '13 19:34    Post subject: Reply with quote

Ah, I didn't realize that <Directory> accepts wildcards and even regular expressions. This opens a world of possibilities.

I'll try to implement my original plan with this knowledge, and share the details if it works as I had hoped.

Thanks, glsmith!
Back to top
cbj4074



Joined: 02 Nov 2012
Posts: 24
Location: United States

PostPosted: Mon 25 Feb '13 23:01    Post subject: Reply with quote

That does indeed work. Effectively, I replaced <DirectoryMatch> with <Directory ~ [expr]>, where [expr] is a regular expression.

The only downside to this approach is that now it seems necessary to specify a RewriteBase in each project's .htaccess file; otherwise, 403 (Forbidden) errors result:

Code:

You don't have permission to access /(project-one|project-two)/public/index.php on this server.


I would really prefer not to have to modify each project's .htaccess file to suit my environment, as this would create a lot of work.

The reason that 403s result makes perfect sense: mod_rewrite is operating on a filesystem path and not a root-relative URL when a RewriteBase is not specified. A relevant excerpt from the mod_rewrite log:

Code:

[...][perdir /(project-one|project-two)/public/] applying pattern '^(.*)$' to uri 'C:/Users/Ben/Documents/Projects[...]


Absent the ability to use regular expression capture groups within the <LocationMatch> block (this seems not to be supported), in order to specify the RewriteBase dynamically, I see no immediate solution to this problem.

In httpd.conf:
Code:

<Directory ~ "^C:/Users/Ben/Documents/Projects/.*-svn/(trunk|branches|tags)/">
   AllowOverride All
   Include conf/auth.conf
   Options +Indexes +FollowSymLinks
</Directory>

# I'll replace these with a single AliasMatch directive once the rest works.
Alias /project-one "C:/Users/Ben/Documents/Projects/Project One/project-one-svn/trunk"
Alias /project-two "C:/Users/Ben/Documents/Projects/Project Two/project-two-svn/trunk"


In project's .htaccess file:
Code:

# Rewrite URIs of the form 'index.php?q=x' (except for real files/directories):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


It occurred to me to forego the .htaccess files altogether, and instead place the appropriate rules in the main server configuration (in addition to what is pasted above):

Code:

<LocationMatch "/(project-one|project-two)/public">
  RewriteEngine on
  Options All
 
  # Rewrite URIs of the form 'index.php?q=x' (except for real files/directories):
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


But the same problem persists: Apache seems to be unable to determine where, on the filesystem, "index.php" actually exists.

Is there a solution to this, beyond specifying a value for RewriteBase (e.g., "/project-one/public")?

Thanks again.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 6785
Location: Germany, Next to Hamburg

PostPosted: Tue 26 Feb '13 12:18    Post subject: Reply with quote

You should exclude index.php before

(untested, but it shoud look some kinda this)
Code:

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Back to top


Reply to topic   Topic: Using Alias (mod_alias) with .htaccess files View previous topic :: View next topic
Post new topic   Forum Index -> Apache