Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL + htaccess mod_rewrite?

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.

like image 844
HaLo2FrEeEk Avatar asked Sep 06 '11 13:09

HaLo2FrEeEk


1 Answers

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}
like image 198
sanmai Avatar answered Oct 31 '22 06:10

sanmai