2017-02-11 109 views
1

首先,我正在尝试实现: 我正在测试基础http://example.com/subdir,这是所有子文件夹,index.php和.htaccess所在的位置。htaccess:相对路径不能正常工作

我有3个组的规则,如在.htaccess(额外的规则删除):

<IfModule mod_rewrite.c> 
Options +FollowSymlinks 
RewriteEngine on 

# Convert item/edit/xx -> index.php?p=edit&id=xx 
# Convert item/remove/xx -> index.php?p=remove&id=xx 
#RewriteCond %{REQUEST_URI} ^/item [NC] 
#RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC] 
RewriteRule ^item/([a-z]+)/([0-9]+)$ index.php?p=$1&id=$2 [NC,L] 

# Convert category/yyyy -> customer/pagination.php?category=yyyy 

#RewriteCond %{QUERY_STRING} category=([a-zA-Z\s]+)$ 
RewriteRule ^customer/category/([a-zA-Z\s]+)$  customer/pagination.php?category=$1 [NC,L] 


# Convert action/about -> index.php?p=about 
# Convert action/terms -> index.php?p=terms 


#RewriteCond %{QUERY_STRING} p=([a-z]+)$ 
RewriteRule ^action/([a-z]+)$  index.php?p=$1 [NC,L] 

</IfModule> 

我面对的是没有的RewriteCond工作(给路径未找到),所以它被注释掉的第一个问题目前为止。 RewriteRule在绝对路径下工作正常(例如RewriteRule^action /([az] +)$ http://example.com/subdir/index.php?p= $ 1 [NC,L]),但是这会导致浏览器显示真实的URL,因此我试图使它与相对路径。 我的问题是,在第一次重定向后,左侧链接的第一部分被添加到路径中,即http://example.com/subdir在点击动作/关于链接后变为http://example.com/subdir/action。 定义RewriteBase或将斜线前缀加到URL上只会让事情变得更糟。 我会感谢一位能够发现问题根源的鹰眼专家。

+0

我真的不明白你的问题的第二部分。什么是导致错误的示例URL,您希望重定向到什么路径,以及它实际重定向到的路径是什么? – Anonymous

+0

示例URL是:http://example.com/subdir/action/about。点击后,它会重定向到正确的关于页面,但下一个请求的路径将变为http://example.com/subdir/action/。因此,当您点击时,例如,操作/条款链接 - 生成的url = http://example.com/subdir/action/action/terms。请注意添加到路径中的额外“操作”文件夹(我需要重定向到http://example.com/subdir/index.php?p=terms)。所有链接都是相对的BTW,例如动作/约。谢谢。 – user2097141

回答

1

第一个问题是%{REQUEST_URI}始终包含完整路径。所以,你的情况可能被更改为:

RewriteCond %{REQUEST_URI} ^/subdir/item [NC] 

第二个问题实际上没有使用的.htaccess来解决。你只需要告诉浏览器你想要什么。所以,你可以使用两种方法之一。

  1. 使用<base>元素(这将适用于所有相对URI在页面上):

    <base href="/subdir" /> 
    
  2. 使用相对路径来在浏览器中显示的网址:

    <a href="terms">Click here</a> 
    

如果你真的想解决问题使用.htaccess,唯一真正的方法是在请求后删除重复的目录。

+1

非常感谢,设置有所帮助,但我必须将PHP中的所有标题重定向更改为绝对路径,以使所有链接都能正常工作。在我得到#RewriteCond%{QUERY_STRING}^id =([0-9] +)$ [NC]后,我会发布完整的代码。 – user2097141