SEO友好的URL - 从blog.php的条款ArticleID = 26博客/ $ articleTitle友好、条款、博客、blog

2023-09-02 00:22:12 作者:强迫症复发

在终于让我的code到发布的URL友好的标题我的分贝(供以后使用的搜索引擎优化的URL),我现在在看如何让我的博客脚本接​​受上述网址。

在理想情况下,我想我的网址出现在堆栈溢出类似的方式。

目前我的网址是这样的:

  blog.php的行动= viewArticle和放大器;条款ArticleID = 26
 
7大日常操作 快速优化SEO

我希望它看起来是这样的:

  /博客/响应布局设计/
 

我已经整理出了剧本的一侧 - 即创建新的URL的一面,并将其保存在数据库中,但我在努力制定出如何实现对前端

我已经看过了一些指南和范例点缀四周,但他们只展示如何创建一个URL,例如:

  store.php行动= viewItem和放大器; ITEMID = 34来存储/项目/ 34
 

很显然,我可以用我的网址做到这一点很容易,但它仍然只是要显示的文章编号,而不是文章标题。

这是我目前的code通过ID从数据库中提取文章 - 如何进行修改,以获得我需要的功能,

 函数viewArticle(){
   如果(!使用isset($ _ GET [条款ArticleID])|| $ _ GET [条款ArticleID]!){网页();返回; }
   $结果=阵列();
   $结果['文章'] =文章:: getById((INT)$ _ GET [条款ArticleID]);
   $结果['PAGETITLE'] = $结果['文章']  - >称号。 |博客;
   要求(TEMPLATE_PATH/viewArticle.php。);
}
 

解决方案

您不需要数据库来做到这一点。所有的PHP框架(据我所知)支持这种类型的网址,并使用认识到什么是所谓的路由的方法。基本上一个URL的结构如下 www.website.com/controller(页)/方法/ getparameters 。你用它来访问此,因此是一个单一入口点。换句话说,你只需要在根目录1 的index.php 文件重定向所有调用它,然后打破网址到其给定的组成部分,并决定该怎么做从那里。一个简单的例子与Apache:

的.htaccess

  RewriteEngine叙述上

的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^ * $的index.php [L,NS]
 

的index.php

  $页= str_replace函数('的index.php','',$ _ SERVER ['PHP_SELF']);
$页= str_replace函数($页,'',$ _ SERVER ['REQUEST_URI']);
$页=爆炸('/',$页);
$控制器= array_shift($页);
$参数=阵列();
的foreach($页面$ VAL){
    $参数[] = urlde code($ VAL);
}
 

然后在网址 www.website.com/blog/responsive-layout-design / ,你将有

  $控制器=> 博客
$参数=>阵列(
    0 => 响应布局设计,
    1 => ''
)
 

希望这是有帮助的。

After finally getting my code to post a URL-friendly title to my db (for later use as the SEO optimised URL) I am now looking at how to get my blog script to accept said URL.

Ideally I would like my URLS to appear in a similar way to stack overflows.

Currently my URLs are like this:

blog.php?action=viewArticle&articleId=26

I want it to appear like this:

/blog/responsive-layout-design/

I have sorted out one side of the script - the side that create the new URL, and saves it in the database but I'm struggling to work out how to implement that on the front end.

I have looked at a few guides and examples dotted around but they only show how to create a URL such as:

store.php?action=viewItem&itemID=34 to store/item/34

Obviously, I could do this fairly easily with my URLs, but it's still just going to be displaying an article id, rather than the article title.

This is my current code for pulling articles from the database by ID - how would this be modified to gain the functionality I'm looking for?

function viewArticle() { 
   if ( !isset($_GET["articleId"]) || !$_GET["articleId"] ) { homepage(); return; } 
   $results = array(); 
   $results['article'] = Article::getById( (int)$_GET["articleId"] );      
   $results['pageTitle'] = $results['article']->title . " | Blog"; 
   require( TEMPLATE_PATH . "/viewArticle.php" ); 
} 

解决方案

You do not need databases to do that. All php frameworks ( that I know of ) support this type of urls and the method used to recognize what is what is called routing. Basically the structure of an url is the following www.website.com/controller(page)/method/getparameters. What you use to access this as such is a single entry point. In other words you only have 1 index.php file in the root directory and you redirect all calls to it, then break down the URL into its given components and decide what to do from there on. A simple example with apache:

.htaccess

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [L,NS]

index.php

$page = str_replace('index.php','', $_SERVER['PHP_SELF']);
$page = str_replace($page,'',$_SERVER['REQUEST_URI']);
$page = explode('/',$page );
$controller = array_shift($page );
$parameters = array();
foreach ($page as $val){
    $parameters[] = urldecode($val);
}

Then in the url www.website.com/blog/responsive-layout-design/ you will have

$controller => 'blog'
$parameters => array(
    0 => 'responsive-layout-design',
    1 => ''
)

Hope that was helpful.