Solved

Apache .htaccess rewriterule SEO friendly

Posted on 2007-03-31
11
2,662 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 17

Expert Comment

by:mjcoyne
ID: 18829137
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
ID: 18829540
(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
ID: 18830794
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Expert Comment

by:ahoffmann
ID: 18832325
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
 
LVL 1

Expert Comment

by:albert006
ID: 18832718
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
ID: 18834794
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
ID: 18846304
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
ID: 18848674
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
ID: 18848738
# 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

624 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