【问题标题】:Code for getting SEO friendly in htaccess file在 htaccess 文件中获得 SEO 友好的代码
【发布时间】:2014-01-28 23:26:03
【问题描述】:

我是 htaccess 文件的新手,有以下要求。

我有一个像这样处理的动态页面

    com/index.php?postname=products
    .com/index.php?cat=news&country=india

对于这些页面,我想要这样的 SEO 友好网址

     .com/products/      to     .com/index.php?postname=products
      .com/news/country   to    .com/index.php?cat=news&country=india

我需要什么代码将其放入 htaccess 文件中?

我需要编写 RewriteCond 或 RewriteRule 还是两者兼而有之?

如果可能,请给我执行此操作的示例代码。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/?$ /index.php?postname=$1 [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?cat=$1&country=$2 [L]
    

    【讨论】:

    • 我想你有一个错字phjp
    【解决方案2】:

    为您的工作尝试这个示例。

    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{QUERY_STRING} ^postname=(.*)$
    RewriteRule ^index\.php$ /postname/%1? [R=301]
    RewriteRule ^postname/([^-]+)$ /postname.php?id=$1 [L]
    

    上面的代码会重定向

    index.php?postname=products
    

    index/products
    

    【讨论】:

      【解决方案3】:

      这是可以帮助您的htaccess 代码

      将 .htaccess 行保存在您的 index.php 文件夹或根文件夹中。

      <IfModule mod_rewrite.c>
      RewriteEngine On
      
      RewriteRule ^([A-Za-z0-9_\-]+)$ index.php?postname=$1 [L]
      RewriteRule ^([A-Za-z0-9_\-]+)/([A-Za-z0-9_\-]+)$ index.php?cat=$1&country=$2 [L]
      
      </IfModule>
      
      <IfModule mod_security.c>
         SecFilterEngine Off
         SecFilterScanPOST Off
      </IfModule>
      
      <IfModule mod_gzip.c>
      mod_gzip_on No
      </IfModule>
      

      对我来说很好用。

      【讨论】:

        猜你喜欢
        • 2014-01-06
        • 2012-09-02
        • 2011-05-29
        • 2013-01-22
        • 1970-01-01
        • 2016-03-11
        • 1970-01-01
        • 2012-08-21
        • 2013-02-03
        相关资源
        最近更新 更多