redirecting to new links permanently

Discuss practical ways rearrange URLs using mod_rewrite.

redirecting to new links permanently

Postby krema » Tue Jun 17, 2008 2:48 pm

My rewrite rule is currently

Code: Select all
RewriteRule ^cat/([^/\.]+)/?$ /cat.php?titleid_cat=$1 [L]


It works fine for making my long URLS, short and friendly, but recently i've been trying to figure out how to make this a permanent redirect.

I have tried

Code: Select all
RewriteRule ^cat/([^/\.]+)/?$ /cat.php?titleid_cat=$1 [R=301,L]


The rule works, but not as i intended. Instead it seems to divert (redirect) me to the original long (php? etc) URL.

1. Should the code be written something like (below) in order for a link to always divert to the new URL, rather than the old?

Code: Select all
RewriteRule ^cat.php?titleid_cat=$1 /cat/([^/\.]+)/?$ [R=301,L]


In other words, the other way around?

2
. or do i write a seperate rule, which redirects old url to new?
krema
 
Posts: 2
Joined: Tue Jun 17, 2008 2:31 pm

Postby richardk » Thu Jun 19, 2008 1:27 pm

You have to have two rules
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
# Match the query string parameter.
RewriteCond %{QUERY_STRING} ^(.*&)?titleid_cat=([^&]+)(&.*)?$ [NC]
RewriteRule ^cat\.php$ /cat/%2/? [R=301,L]

RewriteRule ^cat/([^\./]+)/?$ /cat.php?titleid_cat=$1 [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby krema » Thu Jun 19, 2008 3:40 pm

Thankyou very much, works great! :wink:
krema
 
Posts: 2
Joined: Tue Jun 17, 2008 2:31 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 25 guests

cron