Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess is ignored when using an aliased URI

I am using mod_rewrite to route all requests to index.php.

My folder structure is as follows:

/Users/Peter/Projects/Framework
    /.htaccess
    /index.php

In my .htaccess file I have the following:

RewriteEngine on
RewriteCond $1 !^index\.php/
RewriteRule ^(.*)$ index.php/$1 [L]

This works fine when I visit projects.localhost/framework/example.

I also have the following alias set up: Alias /~alias /Users/Peter/Projects

When I navigate to http://projects.localhost/~alias/framework/example I get a 404 error and the following in my error log: File does not exist: /Users/Peter/Projects/framework/example

It seems that the .htaccess file isn't being called when I use the aliased URL (typing giberish into the .htaccess file doesn't trigger any kind of error when using the aliased URL seems to confirm this).

AllowOveride is set to All.

How do I get .htaccess working when using the aliased URL and have the rewrite rules apply consistently regardless of the URL (aliased or not aliased)?

Edit: Navigating to projects.localhost/~alias/framework/index.php/example also works fine, confirming that the alias is working properly (apart from not having the .htaccess rules applied to it).

like image 342
Peter Horne Avatar asked Dec 04 '11 16:12

Peter Horne


1 Answers

I found your question having the same issue. and from @Gerbens comment I managed to find this part of the Apache manual which states:

#
#  /abc/def/.htaccess -- per-dir config file for directory /abc/def
#  Remember: /abc/def is the physical path of /xyz, i.e., the server
#            has a 'Alias /xyz /abc/def' directive e.g.
#

RewriteEngine On

#  let the server know that we were reached via /xyz and not
#  via the physical path prefix /abc/def
RewriteBase   /xyz

#  now the rewriting rules
RewriteRule   ^oldstuff\.html$  newstuff.html

Adding a rewrite base to the .htaccess file in the aliased directory solved my issue.

So in your case

RewriteEngine on
RewriteBase /~alias
RewriteCond $1 !^index\.php/
RewriteRule ^(.*)$ index.php/$1 [L]
like image 97
danneth Avatar answered Oct 23 '22 02:10

danneth