Rewriting URLs does not work if the website is accessed through an alias directory

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

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

My problem is rewriting the urls 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 answer:

Not found

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

I can confirm that the article.php file article.php in d:\www\mysite\article.php .

In the past, I had the root of my site ( d:\www\mysite ) configured as the Apache server DocumentRoot (instead of c:\wamp\www , which is the default), in which case my redrawing URL worked , so my current The problem should be related to the fact that my site is located behind the 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> 
+4
source share
1 answer

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

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

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

If only http://localhost/mysite , it should return not found on this server . If you do not want this to happen, add another alias along with the above:

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

or

Just this:

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

Why RewriteBase? from RewriteBase Directive Apache Docs :

The RewriteBase directive explicitly sets the base rewrite URL for each directory. As you will see below, RewriteRule can be used in configuration files for each directory (.htaccess). In this case, it will act locally, removing the local directory prefix before processing and applying the rewrite rules only to the remainder. When processing is complete, the prefix is โ€‹โ€‹automatically added back to the path. Default value: RewriteBase physical path directory

When replacing a new URL, this module should re-insert the URL into the server processing. To be able to do this, he needs to know what a URL prefix or URL base is. By default, this prefix is โ€‹โ€‹the corresponding file path. However, for most websites, URLs are NOT directly related to physical file name paths, so this assumption is often wrong! Therefore, you can use the RewriteBase directive to specify the correct URL prefix.

Example from the RewriteBase Apache Docs Directive :

 # # /abc/def/.htaccess -- per-dir config file for directory /abc/def # Remember: /abc/def is the physical path of /xyz, ie, the server # has a 'Alias /xyz /abc/def' directive eg # 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 
+7
source

Source: https://habr.com/ru/post/1396101/


All Articles