How to "remove" unfriendly URLs?

Discuss practical ways rearrange URLs using mod_rewrite.

How to "remove" unfriendly URLs?

Postby inspirationally » Sat Oct 11, 2008 1:44 am

Hello there,

I searched in the board, but couldn't find it. So I decided to join you and ask.

With the help of these boards, I could, without any htaccess knowledge, successfully make my site http://johnny-depp.org much more friendly and remove all those /2008.php?02-15?page=12 etc. and make it beautifully looking with this code:

Code: Select all
RewriteRule ^fans/list/([^/.]+)/$ /fans/?list&country=$1
RewriteRule ^fans/list/([^/.]+)/([^/.]+)/$ /fans/?list&country=$1&start=$2
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)/([^/.]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)/([^/.]+)/page([0-9]+)/$ /$1/$2.php?display=archives&page=$3
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)/([^/.]+)/entry/([0-9]+)/$ /$1/$2.php?display=entry&id=$3
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)/([^/.]+)/subcat/([0-9]+)/$ /$1/$2.php?display=subcat&subcatid=$3
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^projects/movies/([^/.]+)/$ /projects/movies/$1.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^projects/scripts/([^/.]+)/$ /projects/scripts/$1.php
RewriteRule ^([^/.]+)/quotes/([^/.]+)/$ /$1/quotes/?$2
RewriteRule ^([^/.]+)/quotes/cats/([^/.]+)/$ /$1/quotes/index.php?cat&catid=$2
RewriteRule ^([^/.]+)/quotes/cats/([^/.]+)/page([0-9]+)/$ /$1/quotes/index.php?cat&catid=$2&page=$3
RewriteRule ^([^/.]+)/quotes/authors/([^/.]+)/$ /$1/quotes/index.php?author&author=$2
RewriteRule ^([^/.]+)/quotes/authors/([^/.]+)/page([0-9]+)/$ /$1/quotes/index.php?author&author=$2&page=$3
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/$ /$1/$2.php?$3
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/([^/.]+)/$ /$1/?$2
RewriteRule ^feed/$ /skins/feed.rss



I also have some other htaccess files in sub-folders f.e. for my galleries.

The only problem I have is, that people are still (from old links on other sites etc.) coming to the site from old links linked in discussion boards etc. and also in my own news script.

My question now is, how do I remove the old URLs, the "unfriendly" ones, to redirect to the new ones.
I tried with a 301 forwarding, but it gives me an internal server error because of a loop I build in like that.

Do you have any idea how I can achieve that?


Thanks for your help, Martina.
inspirationally
 
Posts: 3
Joined: Sat Oct 11, 2008 1:29 am
Location: Germany

Postby richardk » Sat Oct 11, 2008 9:04 am

I would do it with PHP (example).

My question now is, how do I remove the old URLs, the "unfriendly" ones, to redirect to the new ones.
I tried with a 301 forwarding, but it gives me an internal server error because of a loop I build in like that.

Try adding
Code: Select all
RewriteCond %{ENV:REDIRECT_STATUS} ^$

to you redirects.

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?list(&.*)?$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?country=([^&]+)(&.*)?$ [NC]
RewriteCond %2&%{QUERY_STRING} ^([^&]+)&(.*&)?start=([^&]+)(&.*)?$ [NC]
RewriteRule ^fans/?$ /fans/list/%1/%3/? [R=301,L]


You should use the L flag to stop processing when your rules match
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^fans/list/([^/.]+)/$          /fans/?list&country=$1          [QSA,L]
RewriteRule ^fans/list/([^/.]+)/([^/.]+)/$ /fans/?list&country=$1&start=$2 [QSA,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)/([^/.]+)/$ /$1/$2.php [QSA,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)/([^/.]+)/page([0-9]+)/$ /$1/$2.php?display=archives&page=$3 [QSA,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)/([^/.]+)/entry/([0-9]+)/$ /$1/$2.php?display=entry&id=$3 [QSA,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)/([^/.]+)/subcat/([0-9]+)/$ /$1/$2.php?display=subcat&subcatid=$3 [QSA,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^projects/(movies|scripts)/([^/.]+)/$ /projects/$1/$2.php [QSA,L]

RewriteRule ^([^/.]+)/quotes/([^/.]+)/$                      /$1/quotes/?$2                                [QSA,L]
RewriteRule ^([^/.]+)/quotes/cats/([^/.]+)/$                 /$1/quotes/index.php?cat&catid=$2             [QSA,L]
RewriteRule ^([^/.]+)/quotes/cats/([^/.]+)/page([0-9]+)/$    /$1/quotes/index.php?cat&catid=$2&page=$3     [QSA,L]
RewriteRule ^([^/.]+)/quotes/authors/([^/.]+)/$              /$1/quotes/index.php?author&author=$2         [QSA,L]
RewriteRule ^([^/.]+)/quotes/authors/([^/.]+)/page([0-9]+)/$ /$1/quotes/index.php?author&author=$2&page=$3 [QSA,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/$ /$1/$2.php?$3 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/([^/.]+)/$ /$1/?$2 [QSA,L]

RewriteRule ^feed/$ /skins/feed.rss [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby inspirationally » Sat Oct 18, 2008 1:53 am

I want to thank you, Richard! I try it with htaccess now, actually I did it for most of my functions, but with one I have problems to make the function "vice versa".

Can you help me on this

Code: Select all
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)/([^/.]+)/entry/([0-9]+)/$ /$1/$2.php?display=entry&id=$3 [QSA,L]


please? I tried

Code: Select all
RewriteCond %{ENV:REDIRECT_STATUS} ^$   
RewriteCond %{QUERY_STRING} ^(.*&)?display=entry(&.*)?$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?id=([0-9]+)(&.*)?$ [NC]
RewriteRule ([^/.]+)/([^/.]+)/ /$1/$2/entry/%1? [R=301,L]


but it doesn't work :(
There are some more like this but i guess, when I know how to do this one, then I can do the others alone.
inspirationally
 
Posts: 3
Joined: Sat Oct 11, 2008 1:29 am
Location: Germany

Postby richardk » Sat Oct 18, 2008 4:00 am

Code: Select all
([^/.]+)/([^/.]+)/

should be
Code: Select all
^([^/.]+)/([^/.]+)\.php$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby inspirationally » Sat Oct 18, 2008 4:15 am

Hm. No, sadly not.

This URL

http://johnny-depp.org/johnny/articles/ ... ry&id=1344

should be beautiful:

http://johnny-depp.org/johnny/articles/entry/1344/

Sorry if it was my mistake.
inspirationally
 
Posts: 3
Joined: Sat Oct 11, 2008 1:29 am
Location: Germany

Postby richardk » Sat Oct 18, 2008 9:35 am

Then you want
Code: Select all
^([^/.]+)/([^/.]+)/?$


Try putting the redirect directly after
Code: Select all
RewriteEngine On
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: Google [Bot] and 24 guests

cron