简体   繁体   English

在URL中写页面标题而不是页面ID

[英]Write page title in URL instead of page ID

I am having my website and whenever I add a new page into it, it creates an url with random id number which I don't want. 我有我的网站,每当我向其中添加新页面时,它都会创建一个带有不需要的随机ID号的URL。 I would like to use the page/product name as my URL, so what currently I am having is: 我想使用page/product名称作为我的URL,所以我目前拥有的是:

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

Desired: http://www.abcdex.in/deal/Product-Name 期望的: http://www.abcdex.in/deal/Product-Name : http://www.abcdex.in/deal/Product-Name

I have gone through with my .htaccess file and it shows at the bottom the following code: 我遍历了.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]

Could anyone help me out because It is creating a lot of mess and I am loosing my site rank because of this? 有人可以帮助我,因为它造成了很多混乱,因此我失去了网站排名?

Thank you in advance! 先感谢您!

learn from this example and apply in your .htaccess Example 从此示例中学习并应用于您的.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]

your .htaccess will be something like this your filenames 您的.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]

You can try do all of this in htaccess, but I like to solve these issues in PHP 您可以尝试在htaccess中完成所有这些操作,但是我想用PHP解决这些问题

On all of your pages with URLs similar to 在您所有具有类似以下网址的网页上

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

Convert the product name into a variable, such as $ProductName 将产品名称转换为变量,例如$ ProductName

Then redirect: 然后重定向:

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

It is better if you alter the header into a 301 moved permanently, but you will only be able to alter the header if the Product-Name is passed into the variable $ProductName before any parts of the page have been loaded. 最好将标头更改为永久移动的301,但只有在将页面的任何部分加载之前将产品名称传递到变量$ ProductName中的情况下,才能更改标头。

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

If you can't alter the header, then there are ways to redirect in PHP without using the header. 如果您无法更改标头,则可以使用PHP重定向而无需使用标头。 I believe the following code works: 我相信以下代码可以工作:

 $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>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM