linux - 具有相对路径重定向的 Mod_Rewrite

标签 linux mod-rewrite redirect ubuntu apache2

我在名为 clips/ 的目录中的 .htaccess 文件中有此规则:

RewriteRule ^mlk/?$ segment/index.php?clip=1 [R=301,QSA,L]

我的意图是,当有人访问 http://example.local/clips/mlk 时,他们会被重定向到 http://example.local/clips/segment/index .php?clip=1

实际上发生的是,当有人访问 example.local/clips/mlk 时,他们被重定向到 example.local/var/www/example/clips/segment/index.php?clip=1

我不确定它为什么这样做。如果我将重写规则更改为:

RewriteRule ^mlk/?$ /segment/index.php?clip=1 [R=301,QSA,L]

用户被重定向到 example.local/segment/index.php?clip=1,这仍然是错误的。如果这些文件在网站的目录树中移动,我不想指定绝对路径。我怎样才能使它相对而不是绝对起作用?

最佳答案

尝试如下添加 RewriteBase 指令

RewriteEngine On
RewriteBase /clips/

RewriteRule ^mlk/?$ segment/index.php?clip=1 [R=301,QSA,L]

编辑

but is there any way to get this to work without using a RewriteBase directive

你也可以试试

RewriteEngine On


RewriteCond %{REQUEST_URI} ^(/[^/]+/) [NC] 
RewriteRule ^mlk/?$ http://%{HTTP_HOST}%1segment/index.php?clip=1 [R=301,QSA,L]

关于linux - 具有相对路径重定向的 Mod_Rewrite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9102358/

相关文章:

Linux 非阻塞套接字 : how much data can be read before blocking?

.htaccess - 如何将子域中的一个文件重定向到新 URL?

php - 将旧的 php url 重定向到新的 Flask url

带有 POST 数据的 PHP Symfony 重定向

apache - 没有mod_rewrite的Apache重写?

redirect - Safari IOS 13.1.2 上的特殊 PWA 错误

c++ - 以编程方式提取 .deb 包

linux - 当文件的第二列与另一个文件中的值匹配时如何从文件中提取行

linux - 网络连接后运行脚本

mysql - HTAccess - 令人困惑的干净网址