如果通过别名目录访问网站,则URL重写不起作用

Šim*_*das 4 apache mod-rewrite wampserver

d:\www\mysite我的本地计算机上的目录中有一个网站.我安装了WAMPServer并mysite为我的站点设置了别名目录.

因此,例如,http://localhost/mysite/static-resource.html正确检索位于其中的文件d:\www\mysite\static-resource.html.

我的问题是我的.htaccess文件中的URL重写:

RewriteEngine On    
RewriteRule ^articles/(\d+) ./article.php?id=$1
Run Code Online (Sandbox Code Playgroud)

当我尝试访问时http://localhost/mysite/articles/1,我得到了这样的回复:

未找到

在此服务器上找不到请求的URL /www/mysite/article.php.

我可以确认存在一个article.php文件d:\www\mysite\article.php.

在过去,我将我的site(d:\www\mysite)的根目录设置为DocumentRootApache服务器(而不是c:\wamp\www默认的),在那种情况下,我的URL重写工作,所以我当前的问题必须与事实相关我的网站是"后面"别名目录.


mysite.conf文件的内容:

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

<Directory "d:/www/mysite/">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order allow,deny
    Allow from all
</Directory>
Run Code Online (Sandbox Code Playgroud)

Thi*_*key 7

我没有在重写规则中看到RewriteBase.
在您的.htaccess添加RewriteBase规则中.

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

RewriteBase /mysite/因为你的而必须拥有Alias /mysite/ "d:/www/mysite/"

如果只是http://localhost/mysite被访问,它应该返回一个not found on this server.如果您不希望发生这种情况,请添加另一个别名以及上面的内容,如下所示:

Alias /mysite "d:/www/mysite/" 
Run Code Online (Sandbox Code Playgroud)

要么

只是这个:

AliasMatch /mysite(/.*)? d:/www/mysite/$1 
Run Code Online (Sandbox Code Playgroud)

为什么要RewriteBase?来自RewriteBase Directive Apache Docs:

RewriteBase指令显式设置每个目录重写的基本URL.如下所示,RewriteRule可用于每个目录的配置文件(.htaccess).在这种情况下,它将在本地执行操作,在处理之前剥离本地目录前缀,并仅将重写规则应用于其余部分.处理完成后,前缀会自动添加回路径.默认设置为; RewriteBase物理目录路径

当新URL发生替换时,该模块必须将URL重新注入服务器处理.为了能够做到这一点,它需要知道相应的URL前缀或URL基是什么.默认情况下,此前缀是相应的文件路径本身.但是,对于大多数网站而言,URL与物理文件名路径没有直接关系,因此这种假设通常是错误的!因此,您可以使用RewriteBase指令指定正确的URL前缀.

RewriteBase指令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
Run Code Online (Sandbox Code Playgroud)

  • 我对同一场景也有同样的问题,但添加`RewriteBase/my-alias`并不适合我.任何的想法? (3认同)