【问题标题】:Write page title in URL instead of page ID在 URL 中写入页面标题而不是页面 ID
【发布时间】:2017-04-01 22:31:21
【问题描述】:

我有我的网站,每当我在其中添加新页面时,它都会创建一个带有我不想要的随机 ID 号的 url。我想使用page/product 名称作为我的网址,所以目前我拥有的是:

当前:http://www.abcdex.in/deal/d29e96cfd623a83f37f1bc12b4465131

所需:http://www.abcdex.in/deal/Product-Name

我已经完成了我的.htaccess 文件,它在底部显示了以下代码:

RewriteEngine On
Options -Indexes
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond $1 !^(index\.php|assets|forum|robots\.txt|canvas\.xml)
RewriteRule ^(.*)$ index.php?/$1 [L]

谁能帮帮我,因为它造成了很多混乱,我因此失去了我的网站排名?

提前谢谢你!

【问题讨论】:

  • 这被称为Human Readable URL,最好在您的管理系统级别解决这个问题 - 管理您的站点的系统。检查那里选项Human Readable URL
  • 提交表单时url会改变吗?那可能是当你点击一个产品的时候?
  • @kapil sharma 我已经粘贴了一个示例从中学习,如果有任何疑问,请随时申请。

标签: javascript php web e-commerce


【解决方案1】:

从这个例子中学习并应用到你的 .htaccess 示例

  AddDefaultCharset utf-8
 Options +FollowSymlinks -Indexes
 RewriteEngine on 
 RewriteBase /living/
 # skip all files and directories from rewrite rules below
 #RewriteCond %{REQUEST_FILENAME} -d [OR]
 #RewriteCond %{REQUEST_FILENAME} -f
 #RewriteRule ^ - [L]
 #RewriteRule ^authors/([^/]*)\.html$ main.php?authchar=$1 [QSA,L]

您的 .htaccess 将类似于您的文件名

  AddDefaultCharset utf-8
  Options +FollowSymlinks -Indexes
  RewriteEngine on 
  RewriteBase /
 # skip all files and directories from rewrite rules below
 #RewriteCond %{REQUEST_FILENAME} -d [OR]
 #RewriteCond %{REQUEST_FILENAME} -f
 #RewriteRule ^ - [L]
 #RewriteRule ^deal/([^/]*)\.html$ yourscript.php?d29e96cfd623a83f37f1bc12b4465131=$1 [QSA,L]

【讨论】:

    【解决方案2】:

    你可以尝试在 htaccess 中完成所有这些,但我喜欢在 PHP 中解决这些问题

    在您的所有网页上的网址类似于

    http://www.abcdex.in/deal/d29e96cfd623a83f37f1bc12b4465131
    

    将产品名称转换为变量,如$ProductName

    然后重定向:

     <?php 
     header("Location: http://www.abcdex.in/deal/'.$ProductName.'"); 
     ?>
    

    最好将标题更改为永久移动的 301,但只有在加载页面的任何部分之前将 Product-Name 传递到变量 $ProductName 时,您才能更改标题。

     header("HTTP/1.1 301 Moved Permanently"); 
    

    如果您不能更改标头,那么有一些方法可以在 PHP 中重定向而不使用标头。我相信以下代码有效:

     $URL="http://www.abcdex.in/deal/'.$ProductName.'";
     echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';
     echo "<script type='text/javascript'>document.location.href='{$URL}';       </script>";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多