5

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

would redirect to

reachchallenges.infectionist.com/x/y.php

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

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?

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

It should redirect to:

http://reachchallenges.infectionist.com/challenge/232/Challenge+Title

But instead it redirects to:

http://reachchallenges.infectionist.com/challenge/232/

Without the title. Am I doing something wrong? Do I have to specify RewriteEngine On in the httpd.conf file?

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

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.

HaLo2FrEeEk
  • 586
  • 7
  • 20

1 Answers1

5

With RewriteMap everything is possible:

RewriteMap examplemap prg:/path/to/file.php
RewriteRule (.*) ${examplemap:$1}

You can use mod_dbd as well:

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}
sanmai
  • 29,083
  • 12
  • 64
  • 76
  • 2
    You just forgot to mention that `RewriteMap` directive CANNOT be placed in .htaccess -- only in server config / virtual host context. If OP cannot edit those files (on shared hosting, for example) then it is useless for him. – LazyOne Sep 06 '11 at 14:29
  • No, I didn't. I let the inquirer discover it himself. Moreover, you'd have to restart Apache every time you change that script. But still, if one wants it, he can get it. – sanmai Sep 06 '11 at 14:36
  • _"But still, if one wants it, he can get it"_ -- True. I would just mentioned it anyway (personal preference). – LazyOne Sep 06 '11 at 14:40
  • I have a virtual private server through Dreamhost, so I can edit the httpd.conf file, but I don't know where to put the RewriteMap directive. Also, in sanmai's example, how would file.php get the chid parameter? Edit: I looked up RewriteMap in the Apache docs and I saw this: http://httpd.apache.org/docs/2.3/rewrite/rewritemap.html#dbd which looks like it allows SQL queries, could I use this instead of a php script? Something like "SELECT title FROM challenges WHERE id = %s", would that work with a MySQL database? – HaLo2FrEeEk Sep 07 '11 at 00:21
  • Yes, that is. It looks easy to configure and much easier to use than `RewriteMap prg` – sanmai Sep 07 '11 at 00:47
  • Ok, I went ahead and tried out the dbd method, I've updated my main post with more information. – HaLo2FrEeEk Sep 07 '11 at 04:46
  • Please post another question. – sanmai Sep 07 '11 at 04:49
  • Done: http://stackoverflow.com/questions/7329212/problems-with-rewritemap-maptype-dbd – HaLo2FrEeEk Sep 07 '11 at 05:33