【问题标题】:.htaccess remove index.php and hide parameter key from URLs.htaccess 删除 index.php 并从 URL 中隐藏参数键
【发布时间】:2015-07-21 02:54:13
【问题描述】:

我有以下网址

www.example.com/index.php?tag= xxx

我想使用 .htaccess 使其如下所示

www.example.com/xxx

我用这段代码完成了:

 Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.+?)/?$ /index.php?tag=$1 [L,QSA]

如果我输入这个网址:

www.example.com/index.php?tag=1000

它被重定向到:

www.example.com/?tag=1000

如果:www.example.com/1000 它有效!

所以我有重复的 URL,这对 seo 不利。

如何将www.example.com/index.php?tag=1000 重定向到www.example.com/1000

【问题讨论】:

    标签: php apache .htaccess mod-rewrite seo


    【解决方案1】:

    将此代码用于 .htaccess 文件

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.+?)/?$ /index.php?key=$1 [L,QSA]
    

    你的 index.php 代码将是

    <?php 
    echo $_REQUEST['key'];  
    ?>
    

    然后调用 http://sitename.com/1000

    输出将是:1000

    【讨论】:

      【解决方案2】:

      如何将www.example.com/index.php?tag=1000 重定向到www.example.com/1000

      您可以在RewriteBase 行下方插入此规则:

      RewriteCond %{THE_REQUEST} /(?:index\.php)?\?tag=([^\s&]+) [NC]
      RewriteRule ^ /%1? [R=302,L,NE]
      

      【讨论】:

      • 我们不能再使用 $_GET['get'] 访问标签,如何解决这个问题?
      • OP 现有的内部重写规则的目标是index.php?tag=$1,这将使您获得$_GET['get']
      猜你喜欢
      • 1970-01-01
      • 2013-01-01
      • 2011-05-20
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 2014-03-13
      • 1970-01-01
      相关资源
      最近更新 更多