MYSQL + htaccess的mod_rewrite的? [英] MySQL + htaccess mod_rewrite?
问题描述
我使用代理状短域名在我的网站相结合。在简短的域名是hrci.me和长域reachchallenges.infectionist.com。 hrci.me使用了mod_rewrite,并具有pretty的多少确实从hrci.me一个简单的重定向到reachchallenges.infectionist.com的规则,因此,例如:
I'm using a proxy-like short domain in conjunction with my site. The short domain is hrci.me and the long domain is reachchallenges.infectionist.com. hrci.me uses mod_rewrite and has a rule that pretty much does a simple redirect from hrci.me to reachchallenges.infectionist.com, so for example:
hrci.me/x/y.php
会重定向到
reachchallenges.infectionist.com/x/y.php
这么简单就可以了。在主网站,我有更多的规则,进一步重写URL,prettifying它。其中一个例子是在我的网站,challenges.php,它接受一个参数的脚本, CHID 的,这是一个挑战的ID链接到数据库中的更多信息。通过为参数化脚本就应该是这样的: /challenge.php?chid=123
,但在它改写它看起来像这样: /挑战/ 123 /挑战+标题/
,其中挑战+标题
是从数据库项目的实际称号。还有一个不同的方式,你可以调用同一个页面,像这样: / ch123
,所以在本质上,你可以访问页面3种不同的方式:
Simple as can be. On the main site I have more rules that further rewrite the URL, prettifying it. One example is a script on my site, challenges.php, which accepts a single parameter, chid, which is the challenge ID linked to more information in the database. Passed as a parameterized script it would look like this: /challenge.php?chid=123
, but after it's rewritten it looks like this: /challenge/123/Challenge+Title/
, where Challenge+Title
is the actual title of the item from the database. There's also a different way you can call the same page, like this: /ch123
, so in essence you can access the page 3 different ways:
1. /challenge.php?chid=123
2. /challenge/123/Challenge+Title/
3. /ch123
这其实完美的作品,我有这个问题是,我想这是从hrci.me重定向的URL首先被改写看起来像上面的#2,这样用户可以点击hrci.me/ch123和htaccess的文件将读取数据库,获得挑战ID 123标题,重写URL以 /挑战/ 123 /挑战+标题/
,然后将其重定向到reachchallenges.infectionist.com 。是这样的可能吗?是否有可能从一个MySQL数据库以这种方式使用的htaccess读?
This actually works perfectly, the issue that I have is that I want the URLs that are redirected from hrci.me to first be rewritten to look like #2 above, so the user would click hrci.me/ch123 and the htaccess file would read the database, get the title for challenge id 123, rewrite the url to /challenge/123/Challenge+Title/
and then redirect it to reachchallenges.infectionist.com. Is something like this possible? Is it possible to read from a MySQL database using htaccess in this way?
更新: 我说这我的httpd.conf文件:
UPDATE: I added this to my httpd.conf file:
DBDriver mysql
DBDParams "host=*****,user=*****,pass=*****,dbname=*****"
RewriteMap hrci "dbd:SELECT title FROM challenges WHERE id = %s"
RewriteLog "/home/halo2freeek/rewrite.log"
RewriteLogLevel 3
然后添加一个重写规则,以我的一个子域的,我真的不使用(测试它):
Then added a RewriteRule to one of my subdomains that I don't really use (to test it):
RewriteEngine On
RewriteRule ^ch([0-9]{1,4})(/)?$ http://reachchallenges.infectionist.com/challenge/$1/${hrci:$1} [R=301,L]
当我参观这条道路上的子域名:
When I visit this path on the subdomain:
/ ch232
它应该重定向到:
http://reachchallenges.infectionist.com/challenge/232/Challenge+Title
但是,相反它重定向到:
But instead it redirects to:
http://reachchallenges.infectionist.com/challenge/232/
无标题。难道我做错了什么?我一定要指定 RewriteEngine叙述在
在httpd.conf文件?
Without the title. Am I doing something wrong? Do I have to specify RewriteEngine On
in the httpd.conf file?
更新2 :好了,所以我加了 RewriteEngine叙述在
行并保存,当我试图重新启动Apache我得到这个错误:
UPDATE 2: Ok, so I added the RewriteEngine On
line and saved, when I tried to restart Apache I got this error:
RewriteMap: file for map hrci not found:/dh/apache2/apache2-ps54462/dbd:SELECT title FROM challenges WHERE id = %s
它看起来像它完全无视 DBD
部分,并试图读取整个事情作为一个文件名。现在我真的不知道我做错了。
It looks like it's completely ignoring the dbd
part and trying to read the whole thing as a file name. Now I really don't know what I'm doing wrong.
推荐答案
通过的 RewriteMap指令一切皆有可能:
RewriteMap examplemap prg:/path/to/file.php
RewriteRule (.*) ${examplemap:$1}
您可以使用 mod_dbd 还有:
DBDriver mysql
DBDParams "host=localhost,user=db_user,pass=password,dbname=db"
RewriteMap myquery "dbd:select new_url from rewrite where old_url = %s"
RewriteRule (.*) ${myquery:$1}
这篇关于MYSQL + htaccess的mod_rewrite的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!