【问题标题】:.htaccess in subdirectory - how to use RewriteBase子目录中的 .htaccess - 如何使用 RewriteBase
【发布时间】:2017-08-22 17:06:07
【问题描述】:

我正在尝试以下方法。

我有这个目录结构,它在我们的网络中可用,IP 地址为 xxx.xxx.xxx.xxx

root
    -dir1
    -dir2
    -dir3

现在在我的 dir1 文件夹中,我有一个使用以下 .htaccess 文件的站点

RewriteEngine On
RewriteBase /dir1/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]

在我的网站上,我使用前面的“/”链接源和页面,例如“/details”或“/css/app.css”

问题是,由于前面的“/”,链接总是转到根文件夹。

如何使链接“保留”在子目录中而无需编辑所有链接和来源?

我试过用

RewriteBase /
RewriteBase /dir1
RewriteBase /dir1/
RewriteBase dir1

非常感谢任何帮助。谢谢!

【问题讨论】:

  • 从目标中删除前导斜杠:RewriteRule ^(.*)$ index.php?path=$1 [NC,L,QSA]
  • 谢谢@anubhava,但这也没有用... :(
  • 除非您有其他未在此处显示的规则/配置,否则它不可能无法工作。

标签: apache .htaccess mod-rewrite server apache2


【解决方案1】:

RewriteBase 不会改变任何东西,因为它只适用于相对路径目标。

RewriteBase 指令指定要用于每个目录 (htaccess) RewriteRule 指令的 URL 前缀,该指令替换相对路径

因为你的目标已经是绝对的/index.php,你可以完全省略RewriteBase


从给定的 .htaccess 中,似乎 index.php 返回了请求的文件。所以在这种情况下,我只需在路径前加上适当的目录,例如

RewriteRule ^(.*)$ /index.php?path=/dir1/$1 [NC,L,QSA]

或者如果这对脚本更有效,也可以提供一个额外的参数,例如

RewriteRule ^(.*)$ /index.php?path=$1&subdir=dir1 [NC,L,QSA]

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 2013-04-03
    • 2019-03-23
    • 1970-01-01
    • 2010-10-16
    • 2020-06-21
    • 2017-10-18
    • 2010-12-04
    相关资源
    最近更新 更多