本地 XAMPP 上的 .htaccess 文件无法工作

5

我在本地使用XAMPP有一个PHP项目,同时也有.htaccess文件:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /locations/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test.php [L]
</IfModule>

但是这并没有起作用,我只得到了404错误。在httpd.conf文件中,我已经取消注释了这一行:

# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
AllowOverride All

我正在使用Windows 8.1。请问有什么方法可以让它正常工作吗?


C:\xampp\htdocs\myProjectRoot - kennySystemExit
因为我想要重定向URL - 当我请求localhost/myProjectRoot/locations/时,我想要重定向到localhost/myProjectRoot/test.php。它在生产服务器版本上运行良好,但在本地主机上由于这个.htaccess问题而无法正常工作,我猜测。 - kennySystemExit
但是我没有locations文件夹,我想要的是:当用户键入localhost/myProjectRoot/locations/时,它会将他“重定向”到localhost/myProjectRoot/test.php。所以它将请求这个php文件。但我也尝试创建locations文件夹并在那里放置这个.htaccess文件,但它没有起作用。 - kennySystemExit
2个回答

7
您可以在根目录的.htaccess文件中使用此规则:
RewriteEngine On
RewriteBase /

RewriteRule ^locations(/.*)?$ test.php [L,NC]

我通过从Apache配置文件中删除<Files“ .ht *”> Require all denied </ Files>来启用了.htaccess。它可以工作并且我收到了500错误。但是这个.htaccess仍然不能按照我需要的方式工作(重定向)。 - kennySystemExit
另外,如果我需要将位于“localhost/myProjectRoot/locations/”之后的所有字符串重定向到test.php,我应该添加什么?例如,localhost/myProjectRoot/locations/123456 将转到 localhost/myProjectRoot/test.php。 - kennySystemExit
另外,如果我需要将位于“localhost/myProjectRoot/locations/”之后的所有字符串的URL重定向到test.php,我应该添加什么?例如,localhost/myProjectRoot/locations/123456将转到localhost/myProjectRoot/test.php。 - kennySystemExit

2

如果使用XAMPP,您将在以下位置找到文件:

{xampp_dir}/apache/conf/httpd.conf

搜索以下字符串:

LoadModule rewrite_module modules/mod_rewrite.so

并取消注释它(删除“#”符号)。 现在搜索另一个字符串AllowOverride None,并将其替换为AllowOverride All。


我已经做了,但不幸的是,在重新启动Apache之后,没有任何变化。 - kennySystemExit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接