简体   繁体   English

使用名称替换ID的mod_rewrite

[英]mod_rewrite with name replacing ID

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 . URL结构为mywebsite.com/giveaway/giveaway.php?id=(any number)我希望mywebsite.com/giveaway/giveaway.php?id=(any number)动态URL更改为静态/友好URL mywebsite.com/giveaway/name-of-giveaway-corresponding-to-id In my .htaccess file found in my root folder i have the following: 在我的根文件夹中找到的.htaccess文件中,我具有以下内容:

    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]

The current .htaccess file removes .php and also redirects and changes a URL on my site from 当前的.htaccess文件会删除.php,还会重定向并更改我网站上的网址,
mywebsite.com?page=number to mywebsite.com/page/number mywebsite.com?page=numbermywebsite.com/page/number

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. 我知道您必须从我的数据库中获取与php中的ID相对应的名称并使用它,但是我不确定如何在.htaccess文件中编写代码或如何使用并从数据库传递结果。 Thanks for any help! 谢谢你的帮助!

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. 我不知道这是什么意思,但是您无法将重写引擎(该数据库是基础Apache服务器层的一部分)连接到必须由php访问的数据库。

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. 因此,您不能直接将“ giveaway-corresponding-to-id”改写为id,您需要将其改写为Giveaway.php?nameofgiveaway =(赠品名称),然后在数据库中搜索该字符串。

Your way to go is to add a rewrite rule like 您的方法是添加一个重写规则,例如

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

(not tested, so forgive me if something is wrong) and search for $_GET['nameofgiveaway']. (未经测试,如果出现问题,请原谅我)并搜索$ _GET ['nameofgiveaway']。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM