【问题标题】:Difficult to alter and separate REQUEST_URI with htaccess很难用 htaccess 改变和分离 REQUEST_URI
【发布时间】:2014-10-04 21:26:34
【问题描述】:

我正在尝试使用 htaccess 和 %{REQUEST_URI} 参数处理 URL。我研究了很多,使用htaccess.madewithlove.be和一个虚拟机构建规则并测试没有成功。

请求网址是:

1) www.example.com.br/category/product/Beer?id=16

另一个更好理解的请求 URL 示例:

2) www.example.com.br/category/product/Wine?id=33

我创建的规则是:

RewriteCond %{HTTP_HOST} ^(.*)example\.com\.br$
RewriteCond %{REQUEST_URI} ^(.*)category/product/(.*)$
RewriteRule ^(.*)$ http://www.mynewsite.com.br/category/product/luxe-product/$1? [R=301,L]

预期的重写是:

1) www.mynewsite.com.br/category/product/luxe-product/Beer

2) www.mynewsite.com.br/category/product/luxe-product/Wine

但是,使用此规则,输出 URL 是:

1) www.mynewsite.com.br/category/product/luxe-product/category/product/Beer

2) www.mynewsite.com.br/category/product/luxe-product/category/product/Wine

在这种情况下,我几乎没有将类别/产品放在输出中以简化,因为我无法使用 $0、$1 和 $2 指令进行分隔。有人知道如何解决这个问题吗?

问候,

【问题讨论】:

    标签: .htaccess request-uri


    【解决方案1】:

    您不需要REQUEST_URI 上的条件,因为您可以在RewriteRule 中匹配它。

    相反,您可以使用此规则

    RewriteCond %{HTTP_HOST} example\.com\.br$ [NC]
    RewriteRule ^category/product/([^/]+)$ http://www.mynewsite.com.br/category/product/luxe-product/$1? [R=301,L]
    

    或者你可以像你一样使用REQUEST_URI(两者都是正确的)

    RewriteCond %{HTTP_HOST} example\.com\.br$ [NC]
    RewriteCond %{REQUEST_URI} ^/category/product/([^/]+)$ [NC]
    RewriteRule ^ http://www.mynewsite.com.br/category/product/luxe-product/%1? [R=301,L]
    

    【讨论】:

    • 成功了!我有一个疑问,我的错误是在 RewriteRule 中使用 (.*),对吧?我还不明白这部分规则。我需要多学习。你能推荐一个好的教程吗?谢谢和最好的问候。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    相关资源
    最近更新 更多