php自定义链接和自定义页面 [英] php custom link and custom pages

查看:65
本文介绍了php自定义链接和自定义页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在谷歌上搜索,找不到任何有用的东西.

I was looking on google and couldn't find anything useful.

基本上,我需要创建一个基于 html/php 的市场,但我被困在 1 件事上.我想要一个自定义链接,如 domain.com/store/tshirt.

Basically, I need to make an html/php-based marketplace but I'm stuck on 1 thing. I want to have a custom link like domain.com/store/tshirt.

我的意思是我想生成一个带有自定义链接的自定义页面,就像 wordpress 一样,而不是为每个产品制作新的 html/php 页面,因为这样做会占用大量的空间和时间.

What I mean is that I want to generate a custom page with a custom link like wordpress has, without making new html/php pages for each product, because it would take a lot of space and time to do it.

所以基本上,我正在寻找一种使用数据库中的数据生成带有自定义链接的自定义页面的方法,该数据是从管理面板(如 wordpress)插入的.

So basically, what I'm looking for is a way to generate custom pages with custom links using data from the database, which is inserted from admin-panel like wordpress.

推荐答案

你说的是在 .htaccess 文件中使用重写规则.

What you're talking about is using rewrite rules in an .htaccess file.

需要几个步骤.

1) 如果您还没有 .htaccess 文件,请创建一个.

1) Create an .htaccess file if you don't already have one.

2) 创建重写规则

3) 更改页面上的链接以匹配新的重写规则.

3) Change the links on your page to match the new rewrite rule.

4) 更改您的 sql 查询

4) Change your sql query

示例:

你有一个链接到这样的页面... example.com/products.php?id=24

You have a link to a page like this... example.com/products.php?id=24

您希望您的网址看起来像这样... example.com/belgian-coffee

You want your URL to look like this... example.com/belgian-coffee

步骤 1) 像这样在 .htaccess 中制定重写规则...

Step 1) make your rewrite rule in .htaccess like this...

RewriteEngine On    # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule    ^([A-Za-z0-9_-]+)$    products.php?id=$1    [NC,L]    # Handle page requests`

步骤 2) 将链接从 <a href="/products.php?id=24">Belgian Coffee</a> 更改为 <a href="/<?php echo str_replace(' ', '-', $row['ProductName']) ?>">比利时咖啡</a>
注意:str_replace 函数会将产品名称中的任何空格替换为连字符

Step 2) change the link from <a href="/products.php?id=24">Belgian Coffee</a> to <a href="/<?php echo str_replace(' ', '-', $row['ProductName']) ?>">Belgian Coffee</a>
Note: The str_replace function will replace any spaces in your product name to hyphens

步骤 3) 在查询数据库以获取产品名称反向工程"之前,您传递的参数.在这种情况下... belgian-coffee 像这样...

Step 3) Before you query the database for the product name "reverse engineer" your passed parameter. In this case... belgian-coffee like this...

$productname = str_replace('-', ' ', $_GET['id'])

第 4 步)更改您的 sql 语句以查询产品名称而不是产品 ID

Step 4) Change your sql statement to query for the product name instead of the product ID

大功告成!

快乐编码!

参考

htaccess 手册
htaccess 指南

这篇关于php自定义链接和自定义页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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