MySQL + htaccess mod_rewrite? - mysql

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.

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}

Related

Remove subfolder using URL rewriting

I'm not an expert of URL rewriting but I'd like to use .htaccess to show up a path like this:
https://www.example.com/folder/login/
in this way:
https://www.example.com/folder/
but I don't want a real redirect: my site should display https://www.example.com/folder/login/ page, but the URL would be different, so that an user could think that he still is on https://www.example.com/folder/ while logging in.
Is it possible?
If it is, one last question: I have already enabled RewriteEngine on on the ROOT folder, do I have to put it in another .htaccess file too (which is places into https://www.example.com/folder/login/)?
/folder/ is the requested URL and what appears in the browser's address bar and /folder/login/ is the underlying filesystem path that the request is rewritten to.
To internally rewrite /folder/ to /folder/login/ try the following in the root .htaccess file.
RewriteEngine On
RewriteRule ^folder/$ /folder/login/ [L]
/folder/login/ is presumably a filesystem directory, so this won't directly handle the request. Ideally, you would rewrite directly to the file that handles the request. In this case, I assume mod_dir will issue an internal subrequest for the directory index document, eg. index.php?
I have already enabled RewriteEngine on on the ROOT folder
Note that the order of directives matters.
do I have to put it in another .htaccess file too (which is places into https://www.example.com/folder/login/)
No, you can put it in the root .htaccess file. (You could put it in /folder/.htaccess, but the directives would need to change. It wouldn't make sense to put it in /folder/login/.htaccess since you then couldn't hide the /login subdirectory.)

htaccess - change part of requested URL before sending user onwards?

I have the following issue: we have an internal documentation system which generates help files as HTML. The software that generates them was recently upgraded and now there is a naming issue between capitalized and non capitalized folders.
Old URLs:
https://documentation.example.com/de/101/Skins/Default/Stylesheets/Components/Tablet.css
New URLs:
https://documentation.example.com/de/101/skins/default/stylesheets/components/tablet.css
We have different folders that are affected, so the URLs could look like this (there are many variations):
https://documentation.example.com/en/103/Skins/Default/Stylesheets/Components/Tablet.css
https://documentation.example.com/de/456/Skins/Default/Stylesheets/Components/Tablet.css
https://documentation.example.com/en/324/Skins/Default/Stylesheets/Components/Tablet.css
I would like to be able to take the requested URL, look for this string: "/Skins/Default/Stylesheets/Components/Tablet.css" change it to lowercase and then send the user on to the new changed URL.
Some of the solutions I have found require access to vhosts files which I don't. I am also unable to upload a PHP file or something like that.
Are there any solutions to my problem that could be done with the htaccess file alone? If yes how?
You need to define a rewrite map in your server/vhost config file where the css is hosted.
RewriteMap lc int:tolower
Then in your htaccess file, you can create a rule like:
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(.*)$ ${lc:$1} [L,NC]
Let me know if you need anymore help. This htaccess rule will cause every url to be converted to lowercase. This is a generic htaccess you could make it more refined to search specifically for the URL you need.

htaccess rewrite subdomain for image directory

So I duplicated and installed an existing Wordpress Site on my Server and linked it to a subdomain:
sub.domain.com
After I was finished with the work I put it back on to the main Domain but the problem now is that every image on the site has the URL sub.domain.com/wp-content/... instead of domain.com/wp-content/... and doesnt get displayed.
Is it somehow possible to rewrite the URL using htaccess so the images get displayed or do I need to change every single URL on every Image via mySQL?
My htaccess approach would be like:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,NC,QSA]
If this doesnt work, how can I do it in mySQL? Like every link in the database automated overwrite
I would not use a rewrite for this. That is pretty messy. If your links are stored in the DB then just update it in mysql. You can do it with one easy replace command.
UPDATE your_table
SET your_field = REPLACE(your_field, 'http://sub.domain.com', 'http://www.domain.com')
WHERE your_field LIKE '%http://sub.domain.com%'
Always back up your DB before doing any special updates like this.

Rewriting URLs in htaccess for a dynamic webpage using database value?

If I have a dynamic page, where it takes in an id parameter like example.com/posts.php?id=2, how do I make a RewriteRule in htaccess so that the url shows the title of the post rather than its id, so for example posts.php?id=2 shows a post with a title of "PHP is cool", I want the url to be rewritten like example.com/2/php-is-cool or something like that? Would that be possible if the title value is stored in a MySQL database?
Additional Info:
This is how my htaccess looks like:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]
ErrorDocument 404 /index.php
ErrorDocument 403 /index.php
php_value post_max_size 20M
Basically, I have a MySQL database which stores blog posts in a table called posts. The posts table has an id attribute (which is the auto-increment primary key), a title attribute, and a content attribute. When I access mydomain.com/posts.php?id=X, it will show my post with an id of 'X' from the database on the webpage. I just want to be able to re-write the URL such that it shows the title of the page. I'm doing this primarily for SEO, not aesthetics. Is this possible using htaccess, or would I have to approach this differently?
Rewrite rules apply to the way the URL is handled on your backend (apache). Using mod_rewrite to rewrite those URLs will not affect what the user is seeing in their browser's URL bar unless you redirect the user to a new URL (using a 302 redirect, for example), which would cause the browser to reload the page.
You can achieve what you're asking for using the HTML5 pushstate feature (with a fallback to URL hashes if pushstate is not supported in that browser).
Take a look at this URL for more details:
http://badassjs.com/post/840846392/location-hash-is-dead-long-live-html5-pushstate
Also, you could use BackboneJS and its Router feature to handle your page and URL handling logic in the browser.
http://backbonejs.org/#Router
This is a very application specific kind of solution and this answer cannot go into more details without knowing your exact configuration, application logic, etc.
If you're doing it for SEO only, the easiest way is to pass all URLs to one single script which analyses the request and delivers the content from the database.
Like in
RewriteRule (.*) index.php?url=$1
Of course you will have to exclude index.php from the rule.

How do I redirect with mod_rewrite to a document and set the GET parameter?

So I'm going straight to business. Both tested and confirmed on XAMPP (Windows 7 32-Bit) and MAMP (OS X Snow Leopard), both newest versions. I have the following site structure:
/op/.htaccess
/op/index.php
/op/profile.php
The paths are the absolute paths from Apache document root.
My .htaccess file looks like this:
RewriteEngine on
RewriteRule ^[a-zA-Z0-9]+$ profile.php?alias=$1
When I request the site via http://localhost:8888/op/ the document index.php is opened. That's okay. When I request via http://localhost:8888/op/example the document profile.php is opened, that's also fine. And there is set up a GET parameter with the name alias just like desired. But it's value is simply empty! Like this:
http://localhost:8888/op/profile.php?alias=
So I just want to pass on the last part of the url as a GET variable that it looks like this for the server:
http://localhost:8888/op/profile.php?alias=example
I am slowly getting desperate. I am fighting with this since two and a half hours now. Either it is a wrong server configuration or a real silly mistake. I guess it's the regular expression, but I just don't get it.
It's a regex thing, your htaccess should look like this:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)$ profile.php?alias=$1
The $1 at the end of the string matches the first sub-pattern, but you didn't indicate any sub-pattern. The first set of parenthesis is the first sub-pattern.
Additionally, you may wish to indicate a RewriteBase
RewriteEngine on
RewriteBase /
RewriteRule ^([a-zA-Z0-9]+)$ profile.php?alias=$1