Need help with URLs (this should be easy but i cant make it)

Discuss practical ways rearrange URLs using mod_rewrite.

Need help with URLs (this should be easy but i cant make it)

Postby djelica » Tue Oct 21, 2008 10:28 am

Hi all, im trying to build lyrics site and im almost done (using existing script)
Script itself makes links search engines friendly e.g:
http://www.example.com/Akon/347-Ghetto.html
But in .htaccess file is line:
RewriteRule ^([-A-Za-z0-9]+)/([0-9]+)([-A-Za-z0-9]+)\.html$ http://127.0.0.1/lyrics2/index.php?lyric_id=$2 [L]
So when i visit link http://127.0.0.1/lyrics2/Akon/347-Ghetto.html it rewrites it to http://127.0.0.1/lyrics2/index.php?lyric_id=347 and makes it SE unfriendly.
I would like my links to look like:
http://127.0.0.1/lyrics2/Akon/Ghetto.html or
http://127.0.0.1/lyrics2/Akon/347-Ghetto.html (like it should be)

For your easier understanding i have uploaded screenshot:
http://img387.imageshack.us/my.php?image=problemlk6.jpg
1: when i hover link
2: when i visit it

I have been reading many mod_rewrite tutorials but still cant make it work.
Any help will be appreciated!
djelica
 
Posts: 2
Joined: Tue Oct 21, 2008 9:39 am

Postby laisfun » Tue Oct 21, 2008 11:49 pm

RewriteRule /([0-9]){2,3}- http://127.0.0.1/lyrics2/index.php?lyric_id=$1 [L]
# you can also try [QSA,L] if the above doesn't work.

This above RewriteRule will match any 2-3 digit id# with a dash for quick performance. If your id's become larger than 3 then change the number now or later to reflect it (e.g., {2,4}). If your id's begin with only one digit then change it to {1,3} or {1,4}. Remember, to increase the last digit as your id's grow!

You're URL's will need to look like this
http://127.0.0.1/lyrics2/Akon/347-Ghetto.html
in order for the regex to parse out the id number for
your php script, without the id you don't have anything
to pass to the php script.
laisfun
 
Posts: 16
Joined: Sun Aug 27, 2006 7:03 pm
Location: CA, USA

Postby richardk » Wed Oct 22, 2008 7:24 am

Is the .htaccess file in /lyrics2?
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^[-a-z0-9]+/([0-9]+)-[-a-z0-9]+\.html$ /lyrics2/index.php?lyric_id=$1 [NC,QSA,L]

(For /lyrics2/Akon/347-Ghetto.html.)

You have to edit your PHP script to output these new URLs, mod_rewrite does not change them.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby djelica » Wed Oct 22, 2008 8:10 am

laisfun, thanks for your time, i tried all possible combinations and all of results was same as before.

richardk...
I don't know what to say... you are just... dunno...
All i did is copy/paste your code and i was like lol... all works like charm. You are pro!

THANK YOU RICHARDK
djelica
 
Posts: 2
Joined: Tue Oct 21, 2008 9:39 am

Postby laisfun » Wed Oct 22, 2008 10:26 am

I believe my mistake was going to your host/domain.
I should have written it as...

Code: Select all
RewriteRule /([0-9]+)-  /lyrics2/index.php?lyric_id=$1 [NC,QSA,L]

# or #

RewriteRule ^[^/]+/([0-9]+)-   /lyrics2/index.php?lyric_id=$1 [NC,QSA,L]


I was a bit tired when I responded, sorry.
laisfun
 
Posts: 16
Joined: Sun Aug 27, 2006 7:03 pm
Location: CA, USA

Postby richardk » Wed Oct 22, 2008 1:28 pm

You should write as limited patterns as possible.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby laisfun » Wed Oct 22, 2008 1:48 pm

What about the idea, if your URI patterns are consistent and your REGEX matches your pattern then stop at that point rather than continuing to parse and waisting, albeit though small, amounts of time?
laisfun
 
Posts: 16
Joined: Sun Aug 27, 2006 7:03 pm
Location: CA, USA

Postby richardk » Wed Oct 22, 2008 2:02 pm

I don't really understand what you're trying to say.

Code: Select all
^[-a-z0-9]+/([0-9]+)-[-a-z0-9]+\.html$

Will only match /lettersnumbersdashes/numbers-lettersnumbersdashes.html. That's a limited as mod_rewrite can do.

Code: Select all
/([0-9]+)-

Will match /absolutelyanything/numbers-absolutelyanything. A URL you are not wanting to match (eg. /a/b/c/d/e/f/1234-qwerty.php) will call /lyrics2/index.php and waste time. It is also unlikely to send a 404 error.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby laisfun » Wed Oct 22, 2008 2:11 pm

Wow, you're right; sorry for the confusion for both parties. Thank you.

So what about the idea of a RewriteRule that consisted like this...
Code: Select all
RewriteRule ^[-a-z0-9]+/([0-9]+)-


Since we have our match, then why continue?
laisfun
 
Posts: 16
Joined: Sun Aug 27, 2006 7:03 pm
Location: CA, USA

Postby richardk » Wed Oct 22, 2008 2:26 pm

It would still match /lettersnumbersdashes/numbers-absolutelyanything, eg. /abc/123-qwe/rt123/45\.-_as/df.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 25 guests

cron