【问题标题】:Htaccess redirect with subdirectories and dynamic querystring带有子目录和动态查询字符串的 Htaccess 重定向
【发布时间】:2015-07-30 21:54:52
【问题描述】:

我在 http-host/subdir1/subdir2/siterootdir 之类的子文件夹中安装了一个 Magento 站点。

现在我想缩短我的品牌页面的相当长的 URL,如下所示:

请求网址:

http://hostname/subdir1/subdir2/siterootdir/brands/brandname.html

原网址:

http://hostname/subdir1/subdir2/siterootdir/brands/index/brandsproduct/brand/brandname

htaccess 使用动态查询字符串重定向。

我尝试了许多来自谷歌搜索的示例,但是当我运行所需的请求 URL(如上)时,我不断收到 404 not found 页面。

另外我不太擅长正则表达式和重写规则/条件,所以任何人都可以为上述问题提供解决方案并解释其背后的细节吗?

请注意,brandname 当然是动态的。

【问题讨论】:

    标签: regex .htaccess magento redirect


    【解决方案1】:

    这个 .htaccess 文件会将http://hostname/subdir1/subdir2/siterootdir/brands/brandname.html 重定向到http://hostname/subdir1/subdir2/siterootdir/brands/index/brandsproduct/brand/brandname

    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{HTTP_HOST} hostname$ [NC]
    RewriteCond %{HTTP_HOST} !index
    RewriteRule ^(.*)\.html$ http://hostname/subdir1/subdir2/siterootdir/brands/index/brandsproduct/brand/$1 [R=302,L]
    

    把它放在http://hostname/subdir1/subdir2/siterootdir/brands/ 位置

    \.html 从 URL 中去除 html 扩展。

    R=302 将重定向标记为临时。使用 301 表示永久移动。

    【讨论】:

    • 正如我之前所说,它是一个magento网站,里面没有像“brands”这样的目录,它只是在url中。我在“siterootdir”目录中安装了站点,所以您可以编辑您的答案,以便我可以将它放在根文件夹 htaccess 文件中吗?
    • 所以在请求 URL 中有两个变量:“brands”和“brandname”。这些可以在重写规则中使用。然而,“brandsproduct”从何而来?
    【解决方案2】:

    我采取了不同的方法来解决这些问题,因为我只需要重写品牌特定的 url,所以我猜它与 htaccess 场景并不完全相关,所以我稍微改变了方法并将rewrite programmatically 改为 Core URL Rewrites (“自定义”类型)每当从管理员保存任何品牌时,我也不希望浏览器中的网址发生变化,这是由 magento 提供的网址重写。

    即使有人通过“htaccess”方式找到解决方案然后让我知道,我仍然对此持开放态度,因为我不必保存每个品牌来创建它的 URL 重写。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-26
      • 2016-01-18
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 2015-05-27
      • 2012-08-26
      相关资源
      最近更新 更多