如何从 PHP 脚本中更改 URL 的外观 [英] How to change appearance of URL from within a PHP script

查看:19
本文介绍了如何从 PHP 脚本中更改 URL 的外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一个文件中有一些 PHP 和 HTML,但我不确定如何使 URL 显示为该页面的名称.

I have some PHP and HTML in the same file, and I am not exactly sure how to make the URL appear as the name of that page.

这是我想做的一个例子:假设某个页面 id = 1,该页面的标题是 HelloWorld.

Here is an example of what I would like to do: lets say some page id = 1 and the title of that page is HelloWorld.

我希望 url 显示为 site.com/HelloWorld

Instead of site.com/page.php?id=1 I would like the url to appear as site.com/HelloWorld

但我遇到的问题是,我只有在通过 id 查询标题后才能知道该页面内的页面标题.

But the problem I am having is that I only get to know the title of the page inside that page after I query for the title by id.

考虑到我描述的设置,有没有办法让 url 显示为页面的名称?而且,如果有人通过使用更好看的 url 和页面名称而不是 id 链接到该页面,是否还有办法获取 id 以及页面的其余内容?

Considering the setup I described, is there a way to make the urls appear as the names of the pages? And also, if someone links to that page by using the better looking url with the name of the page instead of the id, is there still a way to get the id and by that, the rest of the page contents?

谢谢!!

推荐答案

您需要了解有关 .htaccess 工作原理的更多信息.

What you need is to learn more on how .htaccess work.

这是一个让我入门的好链接:

Here is a good link that got me started:

.htaccess 技巧和技巧

更新:

这是许多框架中非常常见的做法,其中所有请求都发送到 index.php,然后您使用 php 来提供正确的页面:

Here is a very common practice in many framework where all requests are sent to index.php, and from there you use php to serve the correct page:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.site.com/index.php [L,R=301]
</IfModule>

这篇关于如何从 PHP 脚本中更改 URL 的外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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