Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用htaccess获得良好的SEO链接_Php_.htaccess_Mod Rewrite_Seo - Fatal编程技术网

Php 使用htaccess获得良好的SEO链接

Php 使用htaccess获得良好的SEO链接,php,.htaccess,mod-rewrite,seo,Php,.htaccess,Mod Rewrite,Seo,我正在构建一个Web论坛,其链接如下所示: http://127.0.0.1/website/comments.php?topic_id=2/create+good+seo+links 生成链接的链接如下所示 <a href="comments.php?topic_id=<?php echo (int)($forum_topic['topic_id']); >/<?php echo urlencode(htmlspecialchars($forum_topic['top

我正在构建一个Web论坛,其链接如下所示:

http://127.0.0.1/website/comments.php?topic_id=2/create+good+seo+links
生成链接的链接如下所示

<a href="comments.php?topic_id=<?php echo (int)($forum_topic['topic_id']); >/<?php echo urlencode(htmlspecialchars($forum_topic['topic_head'])); > ">
我尝试在.htaccess文件中应用此代码:

   RewiteEngine on
   RewriteBase /website
   RewriteRule ^comments/([a-zA-Z0-9_-)/([0-9]+)\.php$ comments.php?id=$1

您在重写规则[a-zA-Z0-9\-]+


您还需要更改指向
$2
的id指针,因为它与第二个正则表达式规则相匹配。

您需要类似以下内容的htaccess规则:

RewiteEngine on
RewriteRule ^website/comments/([a-zA-Z0-9_-])/([a-zA-Z0-9_-])/([0-9]+)$ comments.php?topic_id=$3
将友好URL重定向到普通脚本文件,
$3
是重写规则第一部分中的第三组括号(即编号)

因此,您需要将输出更改为:

<a href="comments.php?topic_id=<?php echo $forum_topic['name']; ?>/<?php echo urlencode(htmlspecialchars($forum_topic['topic_head'])); ?>/<?php echo (int)$forum_topic['topic_id'] ?>">

如果你想做一个好的搜索引擎优化使用一些需求在URL关键字
例子
如果您有下载门户

www.domain.com/games/mobile-os/game-name/id/page.html

它将帮助您在google中获得良好的排名

如果我将id更改为2,它能适用于所有整数吗?由于它是一个论坛,将创建许多主题。您没有将id更改为2,而是将id更改为
$2
,这将符合
([0-9]+)
规则。谢谢您的回答。如何确保关键字是URL的一部分?这是一个论坛,用户用他们想要的标题创建主题。有办法解决这个问题吗?www.domain.com/forum-name/topic-name/by-user/page1.html类似的用法-而不是_
RewiteEngine on
RewriteRule ^subdir/comments/([a-zA-Z0-9_-])/([a-zA-Z0-9_-])/([0-9]+)$ comments.php?topic_id=$3
www.domain.com/games/mobile-os/game-name/id/page.html