【发布时间】:2015-01-04 14:51:49
【问题描述】:
我遇到了一个非常常见的问题,我需要将我的 site.com/page.php?id=1&title=page-title 转换为 site.com/page-title-id
我认为这可以很容易地在 .htaccess 文件中添加一些 mod_rewrite ,但我觉得这可能不是最适合 SEO 的方法,你觉得呢?
另一种方法是在 PHP 代码中进行一些更改,但我对这种语言相对较新,我不了解 PHP 附带的所有库和函数,这些库和函数可以让我的生活更轻松。
到目前为止,我在我的 .htaccess 中正在做什么(这不起作用):
# BEGIN ocasion_system
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php\?title=([^&\s]+)&?
RewriteRule (.*)\.php$ /$1/%1/? [L,R=301]
RewriteRule ^([^/]+)/([^/]+)/$ $1.php?title=$2 [QSA,L]
</IfModule>
而我的 page.php 有
//all includes up here..
$page = new Page();
$page->__set('title', $_GET["title"]); //this is how i set up my page interface, please don't laugh
if ($_GET["title"] != NULL){
$page = get_page($page, $db);
echo '<pre>';
print_r($page);//works as intended when i access http://localhost/page.php?title=default prints all the Page object with that title.
echo '</pre>';
}
我想 PHP 的解决方案会更好,因为我不想让搜索引擎认为我在隐藏网站或重定向或任何东西,只是希望 URL 像 site.com/page-title- id 或类似的。
编辑:在 .htaccess 中尝试了不同的方法
【问题讨论】:
-
目前还不清楚您要做什么,但这里有一些想法。 (1) 使用 htaccess 即可;它实际上比尝试仅在 PHP 中完成这一切要好。 (2) 您的
RewriteRules 没有任何title参数,但这正是您在 PHP 脚本中寻找的。span> -
@EdCottrell 这取决于。我通常更喜欢将所有请求重定向到
index.php并拥有一个构建良好的Router对象来处理我的路由。 -
@SecondRikudo 我可能应该更清楚。我和你一样,但你仍然需要以某种方式进行重定向,通常是 htaccess。无论如何,它与伪装无关。
-
@EdCottrell No....所有请求在内部映射到 index.php,index.php 读取客户端指定的 URL,并根据该 URL 确定要做什么。没有重定向。
-
@Dolan 不清楚的部分是您在 PHP 代码中查找
$_GET['title'],但在您的RewriteRules 中只有...page=...。我不确定您希望title参数来自哪里。
标签: php apache .htaccess mod-rewrite