0

I have the following issue related to URL rewriting. I am sure there must be some good solution to this.

I converted this URL
domainname.com/index.php?page=product&pid=5&proTitle=Samsung Galaxy

After rewrite it looks like this
domainname.com/products/5/Samsung-Galaxy.html

The .htaccess code looks like this.

 RewriteRule ^products/(.*)/(.*).html$ index.php?page=product&pid=$1&proTitle=$2 [nc]

Rewrite Works fine. However, if I try to access old URL i.e domainname.com/index.php?page=product&pid=5&proTitle=Samsung Galaxy the page is still accessible and on top of that being crawled by Google and other search engines. I want If someone tries to access this URL, it should direct them to Page Not Found and this should also not be sniffed by any crawlers.

I am sure there must be a smart way of doing this. Awaiting for some valuable suggestions.

3
Contributors
4
Replies
28
Views
3 Years
Discussion Span
Last Post by mmcdonald
0

Check out 301 (Permant redirect) here

Change [nc] to [R=301,NC,L] and that should do the trick :)

Edited by Anthony21: edited question

0

I did try [R=301,NC,L] but the affect was opposite. It was loading domainname.com/index.php?page=product&pid=5&proTitle=Samsung Galaxy but when I entered the modified URL i.e domainname.com/products/5/Samsung-Galaxy.html it went on to look for the old URL with Page Not Found error. I want it the other way around.

0

Ah yes I see that R=301 is doing the opposite of what you want.

I'm not very familiar with htaccess files, but how about a 301 redirect from the index.php?page=product&pid=$1&proTitle=$2 format to the ^products/(.*)/(.*).html$ format on a line just before the RewriteRule you've got there currently? You'd have to use a different regex (that goes the other way round). This is what I'm thinking:

  • Redirect all old urls to the new format
  • Keep the current line which maps the new format to use the underlying old url

I don't know if that would work. just an idea.

0

Why don't you collect the URI using $_SERVER and see if it contains the term products? You can then use an IF statement, so if URL does contain that or one of several key terms redirect to 404:

<?php
    header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
    include("404-not-found.php");
?>

That way you're telling users and crawlers that the URL they have attempted to use no longer exists, however if you choose to navigate to that address you'll have access as you allowed your public IP in the IF statement.

Michael

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.