Rewrite hyphenated URL to pascal case using apache mod_rewrite

328 Views Asked by At

The user friendly URLs should be hyphened, for example:

http://example.com/some-category/some-file

My inherited folder structure however, is pascal cased, therefore the rewritten URL should look like this:

http://example.com/SomeCategory/SomeFile.php

Note: The amount of words in a category or file name will vary, so don't expect consistency in that way. ex. http://example.com/some-long-category/file.

Im wondering how to do this, with a general rewrite condition-rule pair for any category and file, using Apache's mod_rewrite.

You can test your rules here, without the need to setup an Apache server.

2

There are 2 best solutions below

30
On BEST ANSWER

I decided that adding another answer for the updated question made most sense, since my original answer may be useful for someone and is a valid approach as the question was originally stated.

mod_rewrite, as I understand it, loops until the URL is not altered, so we should be able to use that to loop until every word is changed. Here's the code.

This part needs to be in your main server config, at root level or in the <VirtualHost>:

RewriteEngine on
RewriteMap uc int:toupper

Then this in your .htaccess:

RewriteEngine on
RewriteRule (.*)(?:^|-|(/))([a-z])([a-z]*)(?![a-z])(.*?)(?:\.php)?$ $1$2${uc:$3}$4$5.php

That should do it. Please try it and let me know results.

As discovered in the comments, there is a default limit of 10 iterations, which means ten words limit. If you need to match more than ten words then you can increase the LimitInternalRecursion directive as appropriate.

Note: The many comments below are mostly concerned with testing and tweaking until it worked, which it does. Although it then turned out that bare directories need to be matched too. A problem for another day, and perhaps another question!

5
On

Note that the question has been updated since this answer was written, and did not originally state that it is not a consistent format - this answer covers word-word/word-word

This part needs to be in your main server config, at root level or in the <VirtualHost>:

RewriteEngine on
RewriteMap uc int:toupper

Then this in your .htaccess:

RewriteEngine on
RewriteRule ([a-z])([a-z]*)-([a-z])([a-z]*)/([a-z])([a-z]*)-([a-z])([a-z]*) %{uc:$1}$2%{uc:$3}$4/%{uc:$5}$6%{uc:$7}$8.php

I believe the rewrite engine does not need to be on in the first one, but have put it in to be sure. I will leave the testing to you. I've done more than enough of the work! Can't test it there anyway as it needs the first part setting up.

It will redirect single letters in any of the four word positions. Assume all are the same format, two words using a to z only separated by one hyphen.

If you don't have access to the main server config then I suggest running a script to rename your files instead, making the names all lowercase at least. It's not possible without the RewriteMap and that has to go in the main config.

Ref: