Solved

Apache .htaccess rewriterule SEO friendly

Posted on 2007-03-31
11
2,623 Views
Last Modified: 2008-06-08
Hi,

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:

Invisible_Man_MC_(1994)

Thanks.
0
Comment
Question by:karthikramanathan
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 17

Expert Comment

by:mjcoyne
Comment Utility
The Perl part is easy enough:

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

my $string = 'Invisible_Man_MC_(1994)';

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

print $string;
0
 
LVL 1

Accepted Solution

by:
albert006 earned 168 total points
Comment Utility
(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/
OR
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:

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

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

Assisted Solution

by:mjcoyne
mjcoyne earned 166 total points
Comment Utility
Given 'Invisible_Man_MC_(1994)',

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

produces:

Invisible_Man_MC_1994_

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:

The_Blower's_Daughter_Damien_Rice_(2003)

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

The_Blower_s_Daughter_Damien_Rice_2003_
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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)?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Expert Comment

by:albert006
Comment Utility
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.
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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.
0
 

Author Comment

by:karthikramanathan
Comment Utility
Hello,

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

works!!!

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

Thanks.
0
 
LVL 1

Expert Comment

by:albert006
Comment Utility
Yes, that works, but now you'll have to create rule for every single lyric.
0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 166 total points
Comment Utility
# 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/
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now