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

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.

Recommended Answers

Check out 301 (Permant redirect) here

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

Jump to Post

All 4 Replies

Check out 301 (Permant redirect) here

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

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.

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.

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:

    header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");

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.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.