prettify我用PHP的网址 [英] Prettify my URLs with PHP

查看:168
本文介绍了prettify我用PHP的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在PHP的网站,我想有结构,美观大方,和pretty的网址。我知道一个网站我曾在的.htaccess ,但我有一些动态创建的这些URL规则,那么,有没有办法做到这一点在PHP本身?我觉得喜欢Word preSS这是否在某种程度上有其网页了。

I'm writing a site in PHP that I would like to have structures, nice, and pretty URLs in. I know in one site I had rules setup in the .htaccess, but I have to have some of these URL rules created dynamically, so is there a way to do that in the PHP itself? I feel like Wordpress does this somehow with its pages.

基本上,如果我去这个页面:

Basically, if I go to this page:

site.com/users/Xan

这将在技术上是加载:

site.com/users?name=Xan

我可以写的模式(使用正则表达式)很容易。我只需要知道如何使这一切成为可能。

I can write the patterns (using regex) fairly easily. I just need to know how to make this possible.

推荐答案

选项1:

要重定向使用的.htaccess /用户进入这个在:

To redirect using .htaccess to /users enter this in:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ users?user=$1 [L,QSA]

您可以再得到你的用户名为$用户通过简单地做:

You can then get your username as $user by simply doing:

$user = $_GET['user'];

选项2:

如果这不是你要找你以后有什么和重定向/用户/用户名$ _GET ['页']在index.php,坚持到这一点你的.htaccess:

If that's not what you're after and you're looking to redirect /users/username to $_GET['page'] in the index.php, stick this into your .htaccess:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

让我们爆炸在PHP中的响应:

So let's explode that response in PHP:

$urlarray = explode("/",$_GET['page']);

现在

echo $urlarray[0]; // users

echo $urlarray[1]; // username

这篇关于prettify我用PHP的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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