Apache .htaccess rewriterule SEO friendly


I would like to have the users redirected to a SEO friendly URL.

Now :
http://www.example.com/lyrics/index.php?p=show_lyric&titleID=123&lyric=Michael_Cretu&album=Invisible _Man_(1994)&title=Samurai

SEO friendly :
http://www.example.com/lyrics/Michael_Cretu/Invisible _Man_(1994)/Samurai/

Please advise how should I have the RewriteRule and the .htaccess file.

I would also like to have a regexp that converts all the special characters to "_". for example:

Invisible Man {MC} (1994)

should be converted to:


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The Perl part is easy enough:

#!/usr/bin/perl -w
use strict;

my $string = 'Invisible_Man_MC_(1994)';

$string =~ s/\s+/_/g;

print $string;
(I don't think this should be in Perl since you are using PHP in your script)

I see that in your normal URL you have a "titleID". I guess this is required for every lyric? If so, you'll need to put the ID somewhere in your SEO url. Like:

1. http://www.example.com/lyrics/123/Michael_Cretu/Invisible _Man_(1994)/Samurai/
2. http://www.example.com/lyrics/Michael_Cretu/Invisible _Man_(1994)/Samurai/123

An example of the .htaccess:


RewriteEngine On # activate mod_rewrite

# if you want to put the ID like in example 1.
RewriteRule /lyrics/(.+?)/(.+?)/(.+?)/(\d+) lyrics/index.php?p=show_lyric&titleID=$4&lyric=$1&album=$2&title=$3

# if you want to put the ID like in example 2.
RewriteRule /lyrics/(\d+)/(.+?)/(.+?)/(.+?) lyrics/index.php?p=show_lyric&titleID=$1&lyric=$2&album=$3&title=$4

# if you don't want to use any ID.
RewriteRule /lyrics/(.+?)/(.+?)/(.+?) lyrics/index.php?p=show_lyric&lyric=$1&album=$2&title=$3


The regex:

$string =~ s/[^a-z0-9]+/_/ig;

$string = preg_replace("/[^a-z0-9]+/i", "_", $string);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Given 'Invisible_Man_MC_(1994)',

$string =~ s/[^a-z0-9]+/_/ig;



You should also allow for apostrophes, for example, The Blower's Daughter Damien Rice (2003):

#!/usr/bin/perl -w
use strict;

my $string = "The Blower's Daughter Damien Rice (2003)";

$string =~ s/\s+/_/g;

print $string;

which produces:


while s/[^a-z0-9]+/_/ig produces:

10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

this is a server issue (except you have your index.php is your rewrite script itself)
Then you have to use the servers capabilities for URL rewriting, which is mod_rewrite for apache.

> I would also like to have a regexp that converts all the special characters to "_". for example:
are spaces no "special character" for your (as you example implies)?
In addition to my previous comment at 03.31.2007:

To make the URLs even nicer you should use these regexes:

$title = s/[^a-z0-9\-'_.!~*()]+/_/ig; # replace unsafe (defined in RFC2396) characters with underscore
$title = s/^_*(.*?)_*$/$1/; # remove underscores at begin and/or and

If you want to use the PHP versions, just ask me.
beside the fact that rewrite/redirect should be done by the server (see my previous comment), the last given regex is not really true, 'cause you have to apply different "valid characters" to various parts (schema, FQDN, path, parameter, search, ...) of the URI. For example are !* reserved characters while you miss " and %, not talking about (; = ? etc.)

Sometimes it's good to have the basics handy: RFC1630 ;-)

Sorry for being picky.
karthikramanathanAuthor Commented:

Thanks for your inputs. However,

RewriteEngine On
RewriteRule ^/lyrics/(.+?)/(.+?)/(.+?)/(\d+)$ /lyrics/index.php?p=show_lyric&titleID=$4&lyric=$1&album=$2&title=$3

doesn't work, but:

RewriteEngine On
RewriteRule ^lyrics/Txt1/Txt2/Txt3/244$ /lyrics/index.php?p=show_lyric&titleID=244&lyric=Txt1&album=Txt2&title=Txt3


What might be the reason? I am using Apache with PHP !

Yes, that works, but now you'll have to create rule for every single lyric.
# did you mean something like this?

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/lyrics/index.php\?p=show_lyric
RewriteRule ^/lyrics/(\w+)/(\w+)/(\w+)/(\d+)$ /lyrics/index.php?p=show_lyric&titleID=$4&lyric=$1&album=$2&title=$3

# keep in mind that this does not match: http://www.example.com/lyrics/Michael_Cretu/Invisible _Man_(1994)/Samurai/
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.