如何使用 mod_rewrite 显示 SEO 友好的 URL?

How can I display SEO friendly URLs using mod_rewrite?(如何使用 mod_rewrite 显示 SEO 友好的 URL?)
本文介绍了如何使用 mod_rewrite 显示 SEO 友好的 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我本质上不是 PHP 开发人员,我被要求在现有的 PHP 网站上执行一些 SEO.

I am not a PHP developer at heart and I have been asked to perform some SEO on an existing PHP website.

我注意到的第一件事是丑陋的 URL,所以我想让这些 URL 重写为更具信息性的内容.以下是所有可能的模式:

The first thing I noticed was the ugly URLs so I want to get these to rewrite to something more informative. Here are all the possible patterns:

/index.php?m=ModuleType&categoryID=id
/index.php?m=ModuleType&categoryID=id&productID=id
/index.php?page=PageType
/index.php?page=PageType&detail=yes

所以基本上我想做的是将这些转换成类似的东西:

So basically what I want to do is convert these into something like:

/ModuleType/Category
/ModuleType/Category/ProductName
/Page
/Page

在任何建议或示例都很棒之前,我还没有使用过 mod_rewrite!

I haven't used mod_rewrite before any advice or examples would be great!

谢谢.

推荐答案

mod_rewrite 宁愿被用来做相反的事情:在内部将 /ModuleType/Category/ProductName 的请求重写为 /index.php?m=ModuleType&categoryID=id&productID=id.在文档中使用新 URL 是您的应用程序的工作.

mod_rewrite would rather be used to do the opposite: rewrite requests of /ModuleType/Category/ProductName internally to /index.php?m=ModuleType&categoryID=id&productID=id. Using the new URLs in the documents is the job of your application.

编辑 下面是一个将参数化网址转换为新网址的函数示例:

EditHere’s an example of how a function might look like that turns your parameterized URLs into the new ones:

function url($url, $rules) {
    $url = parse_url($url);
    parse_str($url['query'], $url['query']);
    $argNames = array_keys($url['query']);
    foreach ($rules as $rule) {
        if ($rule[0] == $url['path'] && array_keys($rule[1]) == $argNames) {
            $newUrl = $rule[2];
            foreach ($rule[1] as $name => $pattern) {
                if (!preg_match('/'.addcslashes($pattern, '/').'/', $url['query'][$name], $match)) {
                    continue 2;
                }
                $newUrl = str_replace('<'.$name.'>', $match[0], $newUrl);
            }
            return $newUrl;
        }
    }
    return $url;
}

$rules = array(
    array(
        '/index.php',
        array('m'=>'.*', 'categoryID'=>'.*', 'productID'=>'.*'),
        '/<m>/<categoryID>/<productID>'
    )
);
echo '<a href="' . url('/index.php?m=ModuleType&categoryID=categoryID&productID=productID', $rules) . '">/ModuleType/Category/ProductName</a>';

这篇关于如何使用 mod_rewrite 显示 SEO 友好的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)