Solved

Apache .htaccess rewriterule SEO friendly

Posted on 2007-03-31
11
2,645 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
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
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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 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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

830 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