mod_rewrite的使用名称替换ID [英] mod_rewrite with name replacing ID

查看:117
本文介绍了mod_rewrite的使用名称替换ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的网站的一个网页的动态显示的内容。该URL的结构是 mywebsite.com/giveaway/giveaway.php?id=(任何数字) 我想改变这种动态URL转换为静态/友好的URL mywebsite.com/giveaway/name-of-giveaway-corresponding-to-id 。   在我的我的根文件夹中找到.htaccess文件我有以下几点:

I have a page on my website that dynamically displays content. The URL structure is mywebsite.com/giveaway/giveaway.php?id=(any number) I wish to change that dynamic URL into a static/friendly URL mywebsite.com/giveaway/name-of-giveaway-corresponding-to-id. In my .htaccess file found in my root folder i have the following:

    RewriteEngine On

    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} \s/+\?page=([^\s&]+) [NC]
    RewriteRule ^ /page/%1? [R=301,L]

    # existing rule
    RewriteRule ^page/([^/]+)/?$ /?page=$1 [L,QSA]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [L]

目前的.htaccess文件删除的.php,也重定向和我的网站从
改变一个网址 mywebsite.com?page=number mywebsite.com/page/number

The current .htaccess file removes .php and also redirects and changes a URL on my site from
mywebsite.com?page=number to mywebsite.com/page/number

我知道你必须让从我的数据库对应的ID在PHP的名称和使用,但林不知道该怎么写code在.htaccess文件或如何使用,并通过从结果数据库。感谢您的帮助!

I know you have to get the name corresponding to the ID in php from my database and use that, but im not sure how to write the code in the .htaccess file or how to use and pass the results from the database. Thanks for any help!

推荐答案

我不知道,如果这是你的意思,但是无法连接重写引擎,这是底层Apache服务器层的一部分,以数据库,其具有由PHP的访问。

I don't know if that is what you mean, but you can't connect the rewrite engine, which is part of the underlying Apache server layer, to the database, which has to be accessed by php.

所以你不能重写,直接id为名称 - 赠品对应到身份证,你需要把它改写成类似giveaway.php?nameofgiveaway =(赠品的名称),然后搜索数据库中该字符串。

So you can't rewrite the "name-of-giveaway-corresponding-to-id" to the id directly, you need to rewrite it to something like giveaway.php?nameofgiveaway=(name of giveaway) and then search the database for that string.

您的路要走是添加像重写规则

Your way to go is to add a rewrite rule like

RewriteRule ^giveaway/([^/]+)$ giveaway.php?nameofgiveaway=$1 [L,QSA] 

(没有测试过,所以请原谅我,如果事情是错的)并搜索$ _GET ['nameofgiveaway'。

(not tested, so forgive me if something is wrong) and search for $_GET['nameofgiveaway'].

这篇关于mod_rewrite的使用名称替换ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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