【问题标题】:Mod_Rewrite with Relative Path Redirects具有相对路径重定向的 Mod_Rewrite
【发布时间】:2012-02-24 12:27:14
【问题描述】:

我在位于名为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,仍然不正确。如果这些文件在网站的目录树中移动,我不想指定绝对路径。我怎样才能让它相对而不是绝对地工作?

【问题讨论】:

    标签: linux mod-rewrite redirect ubuntu apache2


    【解决方案1】:

    尝试添加如下 RewriteBase 指令

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

    编辑

    但是有什么方法可以在不使用 RewriteBase 指令的情况下使其工作

    你也可以试试

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

    【讨论】:

    • RewriteBase 指令有效——但有什么方法可以在不使用 RewriteBase 指令的情况下使其工作?
    • 我将条件更改为RewriteCond %{REQUEST_URI} ^(/[^/]+/([^/]+/)*) [NC],现在它似乎也适用于嵌套目录。谢谢。
    • 如果希望规则独立于目录所在的命名空间中的位置(以及深度)起作用,则后者不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 2014-08-19
    • 2016-10-07
    • 1970-01-01
    相关资源
    最近更新 更多