【发布时间】:2021-12-15 13:37:41
【问题描述】:
一直在尝试在一个 Xampp 虚拟主机中通过 .htaccess 使用 mod_rewrite,但我没有得到我想要的结果。
我要做的是将以下内容重写:www.example.com/name/billy.html 为:www.example.com/billy
无需尝试重写虚拟主机运行正常的 URL,我可以访问所有页面。但是,当我使用相应的重写规则添加 .htaccess 时,我得到一个 404 页面未找到。正则表达式虽然按预期工作。我看到对www.example.com/name/billy.html 的请求已被重写为www.example.com/billy,但页面没有加载。
name 文件夹存在于文件结构中,.htaccess 位于example 文件夹内。
目前,我的 vh 配置如下所示:
<VirtualHost *:80>
DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/example"
ServerName www.example.com
<Directory "/Applications/XAMPP/xamppfiles/htdocs/example">
Options Indexes FollowSymLinks ExecCGI Includes
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
这是.htaccess文件的内容:
RewriteEngine On
RewriteRule ^name\/([a-z]+).html$ /$1 [L,NC,R]
缺少什么?
【问题讨论】:
-
你确定文件
/Applications/XAMPP/xamppfiles/htdocs/hello/billy确实存在吗? -
/Applications/XAMPP/xamppfiles/htdocs/hello/billy 不存在。但是 /Applications/XAMPP/xamppfiles/htdocs/hello/billy.html 是的。就像我说的那样,没有 URL 重写一切工作正常。当我尝试重写到 www.hello.com/billy 时,我得到了 404 页面未找到。
-
旁白: "Rewrite URL in XAMPP VirtualHost" - 这个标题有点误导,因为您实际上并没有在 VirtualHost 中重写 URL,您使用的是
.htaccess. -
www.hello.com/billy 不存在,所以它必须是 www.hello.com/billy.html。将正则表达式更改为:
^name\/([a-z]+.html)$,它将起作用。我在.html之后移动了右括号。
标签: apache .htaccess mod-rewrite xampp