Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Apache .htaccess rewriterule SEO friendly

Posted on 2007-03-31
11
Medium Priority
?
2,669 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 672 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 664 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 664 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

Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Six Sigma Control Plans

670 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