I am having a struggle trying to get my .htaccess to remove all underscores from an url. I have a lot of old aliases in my articles and they are all with underscores. I have searched for a sollution, but couldnt find any.





What I am trying to get:




As you can see I need to replace the underscore with hyphens and get it into lowercase, but the aliases are between 1 and 14 underscores. Now I've created the following rewrite rules:

RewriteRule ^nieuws/artikel/([^_]*)_([^_]*)_([^_]*)/([^_]*)$ http://mysite.com/nieuws/${lc:$1}-${lc:$2}-${lc:$3} [R=301,L]
RewriteRule ^nieuws/artikel/([^_]*)_([^_]*)_([^_]*)_([^_]*)/([^_]*)$ http://mysite.com/nieuws/${lc:$1}-${lc:$2}-${lc:$3}-${lc:$4} [R=301,L]
RewriteRule ^nieuws/artikel/([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)/([^_]*)$ http://mysite.com/nieuws/${lc:$1}-${lc:$2}-${lc:$3}-${lc:$4}-${lc:$5} [R=301,L]

With the RewriteMap on the virtualhost ofcourse

RewriteMap lc int:tolower 

My question is, how do I get this dynamicly with efficient code to catch all alias between unlimited amounts of underscores?

  • Probably not relevant to you, but just in case - you can do redirects with Joomla! system plugins. I do not know any that does exactly what you want, but if you happen to have the skills to write plugins it should fairly simple. Nov 4, 2017 at 2:05

2 Answers 2


This isn't the cleanest way, but it works. Because of the [L] in it for Last rule, it won't be a redirect loop because it exits once applied:

# Four underscores
RewriteCond %{REQUEST_URI} (.*)_(.*)_(.*)_(.*)_(.*)$
RewriteRule ^(.*)_(.*)_(.*)_(.*)_(.*)$ /$1-$2-$3-$4-$5 [L,R=301]

# Three underscores
RewriteCond %{REQUEST_URI} (.*)_(.*)_(.*)_(.*)$
RewriteRule ^(.*)_(.*)_(.*)_(.*)$ /$1-$2-$3-$4 [L,R=301]

# Two underscores
RewriteCond %{REQUEST_URI} (.*)_(.*)_(.*)$
RewriteRule ^(.*)_(.*)_(.*)$ /$1-$2-$3 [L,R=301]

# One underscore
RewriteCond %{REQUEST_URI} (.*)_(.*)$
RewriteRule ^(.*)_(.*)$ /$1-$2 [L,R=301]
  • I changed my question, to clarify my question. I got the code you send already. Thanks for your help anyway! I hope you can help me further?
    – Brum
    Nov 3, 2017 at 13:11

Don't do this at the .htaccess level, replace your Joomla links in the database (by using the MySQL REPLACE command), and then create a simple PHP redirect in the defines.php file (you can read more about this file here). You will have better control on your links this way.

Essentially, you will need to have the following code in your defines.php file (replace http:// below with https:// if your site uses https):

    $currentURL = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if (strpos ($currentURL, '_') !== FALSE){
        $currentURL = str_replace('_', '-', $currrentURL);
        header("HTTP/1.1 301 Moved Permanently"); 
        header("Location: ".$currentURL); 
  • No, I have to do this in the .htaccess level, because otherwise my links in google won't work anymore, I think I wasn't clear enough, I am sorry! I need the google links to go to my article links but in google the links have underscores and I have - but I dont want to do this by changing my data in the database with hyphens.
    – Brum
    Nov 6, 2017 at 9:46
  • Your links should work in Google. There will be a 301 redirect from the underscored to the hyphened link.
    – itoctopus
    Nov 6, 2017 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.