Using .htaccess to redirect with a closing parenthesis
-
21-12-2019 - |
Question
I'm trying to address a problem that seemingly is out of my hands, but hopefully can be fixed with a little .htaccess magic.
Background of the Problem
I have an implementation of MediaWiki that currently uses an .htaccess file to prettify the URL. In the stock MediaWiki configuration without the mod_rewrite, each article in the wiki is normally accessed by visiting:
www.mywebsite.org/index.php?title=George_Clooney_(actor)
With the .htaccess (code below), we're able to clean this up so that the the same page can be accessed by going to:
www.mywebsite.org/George_Clooney_(actor)
This is all working as it should. Internally, there's no problem here.
Summary of the Problem
The problem is a significant portion of my wiki's traffic comes from vBulletin, which I have found has a bug with how it automatically parses URLs in members' posts. When they add the link above, vBulletin parses the url into a BB Code link, but leaves out the ending parenthesis. In fact, their final BB Code generally looks like this:
[url]http://www.mywebsite.org/George_Clooney_(actor[/url])
Which is unfortunate, because members don't usually catch it, and then the link sends them to a page that doesn't exist at:
www.mywebsite.org/George_Clooney_(actor
My Question
Is it possible, hopefully through a rewrite rule, to add the closing parenthesis if an unclosed opening parenthesis is detected?
To help, our wiki follows a pretty strict naming format, and I don't foresee any situation where we'd have an article that has an opening parenthesis without a closing parenthesis. In all cases the words in the parentheses are located at the end of the article name. Our article naming convention is always either:
Article_Name or Article_Name_(type)
So, if we get a bad link that tries to visit "www.mywebsite.org/George_Clooney_(actor", I'd like to automatically redirect them to "www.mywebsite.org/George_Clooney_(actor)".
Other possible routes I've considered is coding an article suggestion section of the 404 page. I feel this will be a bit more work, but will be my go-to solution if this htaccess plan doesn't work out.
Here's what I currently have for my .htaccess file:
RewriteEngine On
Options +FollowSymLinks
# Require Subdomain
RewriteCond %{HTTP_HOST} ^mywebsite.org [NC]
RewriteRule ^(.*)$ http://www.mywebsite.org/$1 [L,R=301]
# Pretty URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/index.php [PT,L,QSA]
Any help would be greatly appreciated. htaccess, and the combination of the right regex, is still pretty new and magical to me.
Solution
You can use this rule as your very first rule (just below RewriteEngine On
line):
RewriteRule ^(.*?\(.*?[^)])$ /$1) [R=301,L,NE]
This is assuming there is only one missing parenthesis in the URL.