Redirecting specific URL's

Discuss practical ways rearrange URLs using mod_rewrite.

Redirecting specific URL's

Postby joppiesaus » Thu Nov 06, 2008 5:51 am

Hello,

currently my .htacces looks like this:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*[^/])/?$ /$1.php [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ Product.php?categorie=$1&subcategorie=$2&slug=$3 [L]

In my website I have two types of files.

1. Dynamc one where all product details are shown (Product.php)
This rewrite: RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ Product.php?categorie=$1&subcategorie=$2&slug=$3 [L] makes sure I can use this URL:

mysite.nl/XXX/YYY/ZZZ (NEW)

instead of:

mysite.nl/Product.php?categorie=XXX&subcategorie=YYY&slug=ZZZ (OLD)

This works fine, but I would really like to have the old URL also redirected to the new one.

2. Normal non dynamic URL's. The rest of my files are non dynamic and normally uploaded on my server. They look like this:

Contact.php (OLD)
Shoppingcart.php (OLD)
etc.

Thanks to

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*[^/])/?$ /$1.php [QSA,L]

I can link directly to:

Contact/ (NEW)
Shoppingcart/ (NEW)
etc

How can I also make sure that the old URL's are also redirected to the new ones. I've tries everything, but cant seem to get it to work...

Thanks.
joppiesaus
 
Posts: 10
Joined: Tue Oct 14, 2008 11:32 pm

Postby richardk » Thu Nov 06, 2008 8:40 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?categorie=([^&]+)(&.*)?$ [NC]
RewriteCond %2&%{QUERY_STRING} ^([^&]+)&(.*&)?subcategorie=([^&]+)(&.*)?$ [NC]
RewriteCond %1&%3&%{QUERY_STRING} ^([^&]+)&([^&]+)&(.*&)?slug=([^&]+)(&.*)?$ [NC]
RewriteRule ^Product\.php$ /%1/%2/%4/? [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /$1/ [R=301,L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*[^/])/?$ /$1.php [QSA,L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /Product.php?categorie=$1&subcategorie=$2&slug=$3 [L]
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: Majestic-12 [Bot] and 4 guests

cron