2

I have a virtual host running on my local copy of Apache. It has an alias.

<VirtualHost 127.0.0.1>
        ServerName oliverash.me.dev
        DocumentRoot "/usr/docs/oliverash.me/public"
        Alias /mayor "/usr/docs/uni/tmosct/public"
</VirtualHost>

This works fine. I can access the alias by going to oliverash.me.dev/mayor in my browser. However, I have a simple .htaccess rewrite in that alias' folder, which rewrites everything (except folders) to the index.php page. This rewrite is working fine.

Although the rewrite is working, the rewrite returns with a 404 Not Found error. For instance, if I go to oliverash.me.dev/mayor/abcdefg, that rewrites to the index.php file, but I get this error:

"The requested URL /usr/docs/uni/tmosct/public/index.php was not found on this server."

This file does actually exist. I can access it directly.

When I use the same alias but for localhost instead of just this virtual host, the rewrite works fine, and the index.php returns with the actual file, and not the 404. This makes me think it is something to do with the permissions of my virtual host. So I tried to add this beneath my httpd-vhosts.conf (with no luck):

<Directory "/usr/docs/oliverash.me/public">
        AllowOverride All
        Order allow,deny
        Allow from all
</Directory>

These are the same settings that are used on the localhost's DocumentRoot, so I don't see why it's not helping.

UPDATE:

So I checked the Apache error_log. Turns out that the rewrites for this alias are actually relative to the document root of this virtual host.

[Fri Jan 06 22:30:57 2012] [error] [client 127.0.0.1] File does not exist: /usr/docs/oliverash.me.dev/usr

This rewrite is actually looking in /usr/docs/oliverash.me.dev/public/usr/docs/uni/tmosct/public for index.php. Hopefully it is obvious that it should be looking in /usr/docs/oliverash.me.dev.

UPDATE 2:

Okay, this is a problem I'm experiencing a local copy of Apache. However, I've just run into exactly the same problem for an alias on my live Web server. This isn't using any kind of virtual host.

[Sat Jan 07 02:41:51 2012] [error] [client 86.11.223.135] File does not exist: /var/www/html/var

Again, the path is relative to the DocumentRoot. It should be an absolute path.

Kinda annoying :(

UPDATE 3:

This is exactly the problem I am having, but perhaps much better phrased: http://forums.devshed.com/apache-development-15/rewritebase-alias-and-rewriterule-are-not-rooting-to-required-file-395917.html

Oliver Joseph Ash
  • 361
  • 3
  • 5
  • 14
  • What's in Apache's error log when the 404 occurs? – Shane Madden Jan 06 '12 at 20:30
  • `[Fri Jan 06 22:30:57 2012] [error] [client 127.0.0.1] File does not exist: /usr/docs/oliverash.me.dev/usr` - see update – Oliver Joseph Ash Jan 06 '12 at 23:23
  • Where's the actual `.htaccess` file located? And what are its contents? – Shane Madden Jan 06 '12 at 23:30
  • In the public folder of the alias `/mayor`, so `/usr/docs/uni/tmosct/public`. Contents are http://jsfiddle.net/OliverJAsh/fVAmU/ – Oliver Joseph Ash Jan 06 '12 at 23:51
  • So the `.htaccess` is in `/usr/docs/uni/tmosct/public`, but the `index.php` is in `/usr/docs/oliverash.me/public`? Try adding a `RewriteBase /` to your `.htaccess`, then. Or get rid of all the extra complexity and put your rewrite directive in your ``? – Shane Madden Jan 07 '12 at 00:00
  • No. The `index.php` is in the public folder of the alias, so `/usr/docs/uni/tmosct/public`. I've tried using RewriteBase with no luck :( All I added was `RewriteBase /`. – Oliver Joseph Ash Jan 07 '12 at 03:10
  • Set a `RewriteLog` location and set `RewriteLogLevel 9` - let's see what it's doing. – Shane Madden Jan 07 '12 at 04:09
  • I did that, and here's the log for when this rewrite is accessed: http://jsfiddle.net/OliverJAsh/qsSev/ - any chance of debugging that?! – Oliver Joseph Ash Jan 07 '12 at 15:39
  • That's functioning correctly. Assuming that you don't have other rewrite configurations somewhere, it's sending the request to `/usr/docs/uni/tmosct/public/index.php`. What's the error log showing for these requests? – Shane Madden Jan 07 '12 at 20:53

2 Answers2

6

Solution: The RewriteBase have to be the same as the Alias definition, not the physical path/directory in the filesystem!

Check the RewriteBase directive for mod_rewrite, it sounds like it could be related to that. Read about it at: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase

Edit 1:
To try this out I setup a clean fresh Ubuntu in VMware, a clean apache2 installation and created a new folder out of the normal path, then setup a .htaccess and I made it work with a proper RewriteBase rule. The default DocumentRoot is /var/www, I put a index.php in there just to show me where I am. It echoes out "I am index.php in default!". Then I created this Alias in Apaches configuration:

Alias /testberg /home/www/testberg
<Directory "/home/www/testberg">
    Options +Indexes
    AllowOverride All
</Directory>

And in there I put another index.php saying "I am index.php in testberg!". Under /home/www/testberg I created a .htaccess with the following contents:

RewriteEngine On
RewriteBase /testberg
RewriteRule ^apa.test$ index.php

When I browse to http://192.168.100.183/testberg/apa.test I now see: "I am index.php in testberg!" and no errors in Apaches logfile, etc.

Isn't this what you wanted to accomplish?

Edit 2:
Trying with a different virtual host. On my Windows desktop I pointed ahntest.net to my VMware IP in c:\windows\system32\drivers\etc\hosts. On my VMware server I created /home/www/ahntest.net and put a modified index.php there to echo "I am index.php in ahntest.net!" and created the following virtual host:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName ahntest.net
    DocumentRoot /home/www/ahntest.net

    Alias /testberg /home/www/testberg
    <Directory "/home/www/testberg">
        Options +Indexes
        AllowOverride All
    </Directory>
</VirtualHost>

Browsing to http://ahntest.net gives me "I am index.php in ahntest.net!", browsing to http://ahntest.net/testberg gives me "I am index.php in testberg!" and finally browsing to http://ahntest.net/testberg/apa.test gives me "I am index.php in testberg!" so it works just fine here too from what I can tell. The .htaccess/patch in Edit 2 is the same as under Edit 1 above.

Mattias Ahnberg
  • 4,139
  • 19
  • 19
  • Without `RewriteBase /` I get this error: `[Sat Jan 07 16:08:07 2012] [error] [client ::1] script '/usr/docs/oliverash.me/public/index.php' not found or unable to stat` not found or unable to stat`. And with `RewriteBase` I get this error: `[Sat Jan 07 16:06:46 2012] [error] [client ::1] File does not exist: /usr/docs/oliverash.me/public/usr`. You see, the rewrite is going via the path of the virtual host. – Oliver Joseph Ash Jan 07 '12 at 16:09
  • This guy mentions these things too, and suggests that you skip mod_alias and rely on mod_rewrite alone, so perhaps that is what you have to do in your situation: http://www.tedpavlic.com/post_apache_rewriting_examples.php#redir_diff – Mattias Ahnberg Jan 07 '12 at 17:45
  • I updated after setting up a clean environment and tried this out again from scratch. – Mattias Ahnberg Jan 07 '12 at 18:08
  • Thanks for going to these lengths to help me sort my problem. That is sort of what I'm trying to do - the rewrite works on the main host (`localhost`), however, when I set up a virtual host, this is when the rewrite starts to throw errors. Can you try adding a virtual host to your setup and see what that produces for you? – Oliver Joseph Ash Jan 07 '12 at 19:16
  • np. Updated & seems to work fine still. – Mattias Ahnberg Jan 07 '12 at 19:28
  • Found the problem! Set everything up like you did. This helped me then to realise - the `RewriteBase` must be equal to the alias. So, if my work was in the folder `/public`, but the alias was `/mayor`, then the `RewriteBase` also had to be `/mayor`. Previous I put `RewriteBase /public`. This was the problem all along. Thank you very, very much for your help. Could you please update your answer to point out that the RewriteBase must match the alias URL and not the actual path it leads to? In the unlikely scenario anyone else runs into the same problem. – Oliver Joseph Ash Jan 07 '12 at 20:38
  • Done! If you want you can add the solution to the end of your question too to clarify. And not nice that you gave that other guy the correct answer tag for the question. ;) – Mattias Ahnberg Jan 07 '12 at 20:54
  • There you go ;) – Oliver Joseph Ash Jan 07 '12 at 21:42
1

What you need is a directory container for the alias target

<Directory "/usr/docs/uni/tmosct/public">
  ...
</Directory>

UPDATE

You have to put the Directory container inside the VirtualHost container for them to work for the virtual host.

UPDATE 2

Quickly checking the jsFiddle thing, you need a

NameVirtualHost *:80

above the container and next you should rewrite the

<VirtualHost 127.0.0.1>

into

<VirtualHost *:80>

and last not least you need an entry in your hosts file, so the URL can be resolved.

Chris
  • 1,185
  • 2
  • 9
  • 18
  • I tried this. I have two directory containers, one for the virtual host, one for the alias target. Both have the same properties as I listed above. Server restarted, still getting the same error. The directory containers are beneath the virtual host container in the `.conf` file. – Oliver Joseph Ash Jan 06 '12 at 20:27
  • 1
    updated my answer. Hope this helps, GL. – Chris Jan 06 '12 at 20:31
  • Here is my code: http://jsfiddle.net/OliverJAsh/XkdUh/ Still getting 404. Thanks for your help so far! – Oliver Joseph Ash Jan 06 '12 at 20:38
  • Ok, updated my answer a second time. Should work after a restart. – Chris Jan 06 '12 at 20:46
  • I've changed the virtual host identifier but the `NameVirtualHost` was actually already there (sorry about that). See my updated code: http://jsfiddle.net/OliverJAsh/XkdUh/1/ - my DNS hosts appears to be fine, as the URL is resolving properly. Restarted, still not working! Are the properties inside of my directory containers correct? – Oliver Joseph Ash Jan 06 '12 at 20:49
  • You can add `Options Indexes FollowSymLinks` and `DirectoryIndex index.php` with the later should be loaded globally anyways. So yes, to me your config looks good. Maybe check your logfile. You can increase your `LogLevel` to `debug`. My best guess would be an error in your rewrite (maybe). I am no pro for rewrite rules. – Chris Jan 06 '12 at 20:56
  • let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/2160/discussion-between-oliver-joseph-ash-and-chris) – Oliver Joseph Ash Jan 06 '12 at 21:21