使用 php 的 SEO 友好 url [英] SEO friendly url using php

查看:49
本文介绍了使用 php 的 SEO 友好 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 php 制作对 SEO 友好的网站...

在文件夹 jcheck 中,我有一个 index.php 文件,它以一个参数id"作为输入.

mydomain.com/jcheck/index.php?id=1 有效

我怎样才能做到如下

mydomain.com/jcheck/1

我尝试制作 .htaccess 文件并放入

RewriteEngine onRewriteCond %{REQUEST_URI} 1/重写规则 1/http://mydomain.com/jcheck/index.php?id=1

我怎样才能让它工作?

解决方案

你基本上可以通过两种方式做到这一点:

使用 mod_rewrite 的 .htaccess 路由

在您的根文件夹中添加一个名为 .htaccess 的文件,并添加如下内容:

RewriteEngine onRewriteRule ^/Some-text-goes-here/([0-9]+)$/picture.php?id=$1

这会告诉 Apache 为这个文件夹启用 mod_rewrite,如果它被要求提供一个匹配正则表达式的 URL,它会在内部将它重写为你想要的,而最终用户不会看到它.简单但不灵活,因此如果您需要更多功能:

PHP 路线

将以下内容放入您的 .htaccess 中:

FallbackResource index.php

这会告诉它运行 index.php 来处理它通常在您的站点中找不到的所有文件.在那里你可以例如:

$path = ltrim($_SERVER['REQUEST_URI'], '/');//修剪前导斜线$elements = expand('/', $path);//用斜线分割路径if(count($elements) == 0)//没有路径元素意味着家显示主页();else switch(array_shift($element))//弹出第一项并切换{案例一些文本到这里":ShowPicture($elements);//将其余参数传递给内部函数休息;案例更多":...默认:header('HTTP/1.1 404 未找到');Show404Error();}

这就是大型网站和 CMS 系统的做法,因为它在解析 URL、配置和数据库相关 URL 等方面提供了更大的灵活性.不过,对于零星使用,.htaccess 中的硬编码重写规则会很好.

*****从用PHP重写URL**复制此内容>

I am trying to make SEO friendly website using php...

In folder jcheck i have a index.php file which take one parameter "id" as input.

mydomain.com/jcheck/index.php?id=1 works

How can i make it as follow

mydomain.com/jcheck/1

I have tried making .htaccess file and putting

RewriteEngine on

RewriteCond %{REQUEST_URI} 1/
RewriteRule 1/ http://mydomain.com/jcheck/index.php?id=1

How can i make it work?

解决方案

You can essentially do this 2 ways:

The .htaccess route with mod_rewrite

Add a file called .htaccess in your root folder, and add something like this:

RewriteEngine on
RewriteRule ^/Some-text-goes-here/([0-9]+)$ /picture.php?id=$1

This will tell Apache to enable mod_rewrite for this folder, and if it gets asked a URL matching the regular expression it rewrites it internally to what you want, without the end user seeing it. Easy, but inflexible, so if you need more power:

The PHP route

Put the following in your .htaccess instead:

FallbackResource index.php

This will tell it to run your index.php for all files it cannot normally find in your site. In there you can then for example:

$path = ltrim($_SERVER['REQUEST_URI'], '/');    // Trim leading slash(es)
$elements = explode('/', $path);                // Split path on slashes
if(count($elements) == 0)                       // No path elements means home
    ShowHomepage();
else switch(array_shift($elements))             // Pop off first item and switch
{
    case 'Some-text-goes-here':
        ShowPicture($elements); // passes rest of parameters to internal function
        break;
    case 'more':
        ...
    default:
        header('HTTP/1.1 404 Not Found');
        Show404Error();
}

This is how big sites and CMS-systems do it, because it allows far more flexibility in parsing URLs, config and database dependent URLs etc. For sporadic usage the hardcoded rewrite rules in .htaccess will do fine though.

*****Copy this content from URL rewriting with PHP**

这篇关于使用 php 的 SEO 友好 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆