【问题标题】:.htaccess redirect and visible URL.htaccess 重定向和可见 URL
【发布时间】:2015-09-18 07:51:20
【问题描述】:

我是 htaccess 的新手,我想知道如何做这两件事,是否推荐它们,或者是否有其他更好的方法来实现对 SEO 友好且美观的 URL:

  1. 从 www.example.com/products?id=car 重定向到 example.com/car。如果您能告诉我对 SEO 友好的最佳方法,那就太好了(301 或其他)。
  2. 即使单击重定向到 www.example.com/products?id=car 的链接,也始终将 URL example.com/car 显示为漂亮的 URL,而不是符号。

【问题讨论】:

    标签: regex .htaccess mod-rewrite url-rewriting seo


    【解决方案1】:

    您可以在DOCUMENT_ROOT/.htaccess 文件中使用此代码:

    RewriteEngine On
    RewriteBase /example/
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+) [NC]
    RewriteRule ^ %1? [R=302,L,NE]
    
    # internal forward from pretty URL to actual one
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/?$ products.php?id=$1 [L,QSA]
    

    【讨论】:

    • 非常感谢。不知道我做错了什么,但它不起作用。我输入网址 www.example.com/car,它显示网络“产品”不存在。
    • 我解决了,我不记得把 .php 放在 products 后面。对不起,我很菜鸟。无论如何,当我输入 URL /car 时,它可以正常工作,但是当我导航到 products?id=car 时,URL 是 products?id=car ,而不是漂亮的。
    • 是的,我正在使用 /products.php?id=car ,这就是我单击链接时得到的结果。我在那里遗漏了一些东西,因为我无法获得漂亮的网址。
    • 完全相同的规则在我的 Apache 上将我从 http://localhost/products.php?id=car 重定向到 http://localhost/car
    • 终于成功了!我非常感谢你的帮助。最后一个问题,这对 SEO 有好处吗?
    猜你喜欢
    • 2012-02-03
    • 1970-01-01
    • 2015-07-28
    • 2011-10-05
    • 2016-09-28
    • 2013-01-27
    • 2011-03-23
    • 1970-01-01
    相关资源
    最近更新 更多