我想将一些 MySQL
代码迁移到 MySQLi
,所以我创建了一个子目录并将所有 Web 文件复制到该子目录中,以便同时在线工作该网站继续运行,没有任何干扰,尤其是这样我就可以在同一个数据库上使用原始数据库配置。我计划在迁移完成后将子目录移动到主目录。
我现在的主要问题是,当我调用 domain.com/subdirectory/script.php
时,最终结果是调用了 domain.com/script.php
反而。我的 .htaccess
中有指令“隐藏”了 .php
扩展名,所以通常第一个响应是 302
然后是 200
。但现在它实际上返回了 302,但这次 location
变成了主目录中的脚本,即使它是从子目录调用的。
这是有问题的主目录和子目录的 .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
Options -Indexes
ErrorDocument 404 domain.com/page-not-found
最佳答案
我没有检查您重写规则的有效性或逻辑性。但是根据您的要求,我认为您缺少的是 RewriteBase指令。
The RewriteBase directive specifies the URL prefix to be used for per-directory (htaccess) RewriteRule directives that substitute a relative path.
您可以像这样修改您的 .htaccess 文件。
你的主目录:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule ^(.+)\.php$ $1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,END]
Options -Indexes
ErrorDocument 404 domain.com/page-not-found
您的子目录:
RewriteEngine On
RewriteBase /subdirectory/
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule ^(.+)\.php$ $1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,END]
Options -Indexes
ErrorDocument 404 domain.com/page-not-found
这里,唯一的区别是我添加了RewriteBase
并更改了重定向位置中的绝对路径。 ($1
而不是 /$1
)
希望对你有所帮助。如果您有任何疑问,请随时询问 :)
关于子目录中的 PHP 脚本在调用时自动请求根目录中的相同脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51227427/