Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL rewriting doesn't work if web-site is accessed via alias directory

I have a web-site in the directory d:\www\mysite on my local computer. I installed WAMPServer and set up an alias directory mysite for my site.

So, for instance, http://localhost/mysite/static-resource.html correctly retrieves my file which is located in d:\www\mysite\static-resource.html.

My issue is with the URL rewriting in my .htaccess file:

RewriteEngine On    
RewriteRule ^articles/(\d+) ./article.php?id=$1

When I try to access http://localhost/mysite/articles/1, I get this response:

Not Found

The requested URL /www/mysite/article.php was not found on this server.

I can confirm that there exists a article.php file at d:\www\mysite\article.php.

In the past, I had the root of my site (d:\www\mysite) set up as the DocumentRoot of the Apache server (instead of c:\wamp\www which is the default), and in that scenario, my URL rewriting worked, so my current issue must be related to the fact that my site is "behind" an alias directory.


The contents of my mysite.conf file:

Alias /mysite/ "d:/www/mysite/" 

<Directory "d:/www/mysite/">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order allow,deny
    Allow from all
</Directory>
like image 419
Šime Vidas Avatar asked Feb 21 '23 12:02

Šime Vidas


1 Answers

I do not see a RewriteBase in your rewrite rules.
In your .htaccess, add a RewriteBase rule.

RewriteEngine On    
RewriteBase /mysite/
RewriteRule ^articles/(\d+) ./article.php?id=$1

RewriteBase has to have /mysite/ because of your Alias /mysite/ "d:/www/mysite/"

If just http://localhost/mysite is accessed, It should return a not found on this server. If you do not want this to happen, add another alias along with the above like this:

Alias /mysite "d:/www/mysite/" 

or

Just this:

AliasMatch /mysite(/.*)? d:/www/mysite/$1 

why a RewriteBase? from RewriteBase Directive Apache Docs:

The RewriteBase directive explicitly sets the base URL for per-directory rewrites. As you will see below, RewriteRule can be used in per-directory config files (.htaccess). In such a case, it will act locally, stripping the local directory prefix before processing, and applying rewrite rules only to the remainder. When processing is complete, the prefix is automatically added back to the path. The default setting is; RewriteBase physical-directory-path

When a substitution occurs for a new URL, this module has to re-inject the URL into the server processing. To be able to do this it needs to know what the corresponding URL-prefix or URL-base is. By default this prefix is the corresponding filepath itself. However, for most websites, URLs are NOT directly related to physical filename paths, so this assumption will often be wrong! Therefore, you can use the RewriteBase directive to specify the correct URL-prefix.

Example from RewriteBase Directive Apache Docs:

#
#  /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
like image 104
ThinkingMonkey Avatar answered Feb 25 '23 00:02

ThinkingMonkey