动态页面中有不同的静态网址 [英] have different static url in dynamic page

查看:82
本文介绍了动态页面中有不同的静态网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,每个人都有自己的个人资料.我想使用诸如 mywebsite/user1 mywebsite/user2 之类的静态URL,但实际上我会留在同一页面中并动态更改内容.原因是,当我打开站点时,我向数据库询问一些数据,并且我不想每次更改页面时都询问它. 我不喜欢 mywebsite?user = 1

I have a website where each person has his personal profile. I would like to have static URL like mywebsite/user1, mywebsite/user2, but actually I would remain in the same page and change the content dynamically. A reason is that when I open the site I ask to a database some data, and I don't want to ask it each time I change page. I don't like url like mywebsite?user=1

有解决方案吗?

谢谢

[修改效果更好]

我有一个动态页面,显示我网站的用户个人资料.因此,URL类似于 http://mywebsite.me?user=2 但我想有一个静态链接,像 http://mywebsite.me/user2name

I have a dynamic page that shows the user profile of my website. So the URL is something like http://mywebsite.me?user=2 but i would like to have a static link, like http://mywebsite.me/user2name

我为什么要这个?因为它很容易记住和编写,并且因为我可以动态更改页面的内容,而不必每次都向我的数据库询问数据(我需要所有页面中的一些共享信息.所有页面的信息都相同)

Why I want this? Because it's easy to remember and write, and because i can change dynamically the content of the page, without asking each time data to my database (i need some shared info in all the pages. info are the same for all the pages)

推荐答案

是的,有解决您问题的方法!

Yes there are solutions to your problem!

第一个解决方案是服务器依赖.我有点不确定这在IIS服务器上如何工作,但是在Apache中这很简单. Apache可以从名为 .htaccess 的文件中获取指令. .htaccess文件需要与活动脚本位于同一文件夹中才能工作.它还需要指令AllowOverride All和模块 mod_rewrite 已加载在主服务器配置中.如果已完成所有这些设置,则需要编辑.htaccess文件以包含以下内容

The first solution is server dependend. I am a little unsure how this works on an IIS server but it's quiet simple in Apache. Apache can take directives from a file called .htaccess. The .htaccess file needs to be in the same folder as your active script to work. It also needs the directive AllowOverride All and the module mod_rewrite loaded in the main server configuration. If you have all this set up you need to edit your .htaccess file to contain the following

RewriteEngine on
RewriteRule ^mywebsite/([^/\.]+)/?$ index.php?user=$1 [L]

这将允许您使用mywebsite/12访问mywebsite/index.php?user=12. mod_rewrite 的初学者指南.

This will allow you to access mywebsite/index.php?user=12 with mywebsite/12. A beginner guide to mod_rewrite.

您也可以仅使用PHP伪造它.它不会像前面的示例那样漂亮,但是它是可行的.另外,请考虑到您正在使用用户输入,以便将数据视为污染.用户需要通过mywebsite/index.php/user/12访问脚本.

You could also fake this with only PHP. It will not be as pretty as the previous example but it is doable. Also, take into concideration that you are working with user input so the data is to be concidered tainted. The user needs to access the script via mywebsite/index.php/user/12.

<?php
    $request = $_SERVER['REQUEST_URI'];
    $request = explode($request, '/'); // $request[0] will contain the name of the .php file
    $user[$request[1]] = $request[2];

    /* Do stuff with $user['user'] */
?>

这些是我所知达到目标的最快方法.

These are the quickest way I know to acheive what you want.

这篇关于动态页面中有不同的静态网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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