PHP:URL重写 [英] PHP : url rewriting

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

问题描述

我想知道如何URL页面没有PHP扩展

I want to know how to page url without .php extension

为前这里是我的网站:

http://mywebsite.com/现在从每当我点击任何画廊主页它将进入页面gallery.php与查询字符串galleryID的前

http://mywebsite.com/ now from the home page whenever i click on any gallery it will goes to the page gallery.php with querystring of galleryID for ex

http://mywebsite.com/gallery.php?id=29

因此​​,而不是这个gallery.php的?ID = 29我想使URL一些相关的页面标题

So instead of this gallery.php?id=29 I want to make the url something related to the page title

http://mywebsite.com/9-WEDDING-GIFT-IDEAS

在此先感谢

推荐答案

有,你可以做到这两种方式。如果你没有一吨的网址,您可以将它们添加到您的htaccess的手动文件。

There are two ways you can do this. If you don't have a ton of URLs, you can add them in to your '.htaccess' file manually.

RewriteEngine On

# MANUAL
RewriteRule ^9-WEDDING-GIFT-IDEAS/?$ gallery.php?id=29 [L]
RewriteRule ^MOST-BEAUTIFUL-WEDDING-LOCATIONS/?$ gallery.php?id=30 [L]
# ...

另外,也可以有'gallery.php处理仰视它显示的文章的基础上的标题。所以,如果它收到的'9的婚礼礼物创意一个标题,那么就可以查询数据库中的那个标题并获取文章的标题。在这里,文章的标题会被传递的'身份证'参数'gallery.php。

Otherwise, you can have 'gallery.php' handle looking up which article to display based on the title. So if it receives a title of '9-WEDDING-GIFT-IDEAS', then it can look up that title in the database and fetch the article for that title. Here, the article title will be passed as the 'id' parameter to 'gallery.php'.

RewriteEngine On

# PARSING HANDLED IN gallery.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ gallery.php?id=$1 [L]

编辑:

<?php
// gallery.php

$article_title = $_GET['id'];

// ...
// DO A DB LOOKUP TO GET THE ARTICLE ID WHERE TITLE = '$article_title'
// OR JUST GET THE ARTICLE BASED ON THE TITLE INSTEAD

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

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