.htaccess文件似乎没有效果[重复]

pxyaymoc  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(163)

此问题在此处已有答案

Redirect all to index.php using htaccess(8个答案)
25天前关闭。

OS: Mac Monterey
Web Server: Mac built-in Apache server (not MAMP)
Server version: Apache/2.4.54 (Unix)
Server built:   Aug 24 2022 03:08:51

网站结构:

~Sites/test

在~/Sites/test/php.index中,我有:

echo "Hello World!";

当我这样做时:

localhost/test

我明白了:

Hello World!

我希望所有的内容都转到index.php,不管我在url中输入了什么路径,所以我添加了一个~/Sites/test/.htaccess,其中包含以下内容:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php[L]

当我这样做时:

localhost/test

我还是明白了:

Hello World!

但如果我这样做:

localhost/test/foo

我得到:

**Not Found**
The requested URL was not found on this server.

我正在学习一个教程,而.htaccess文件应该把我带到index.php文件,不管我在url中包含什么路径。
有什么想法吗?

bzzcjhmw

bzzcjhmw1#

考虑到路径到文件夹/目录Sites是你的根路径,如果是这种情况下,然后把你的.htaccess规则文件沿着站点文件夹(不是里面,随着它),然后尝试以下规则。

请确定:

  • 一次只能使用一个规则。
  • 在测试URL之前清除浏览器缓存。

这会考虑您的路径如下:/root/singh/hddocs/Sites/root/singh/htdocs/.htaccessroot/singh/htdocs/Sites/test/index.php中的一个或多个。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^  Sites/test/index.php [QSA,L]

***或者***如果您根目录实际上是从Sites文件夹开始,请尝试以下规则

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^   /Sites/test/index.php [QSA,L]

相关问题