Jump to content

Recommended Posts

Hello ;

 

So I made our dynamic urls static url's by using .htaccess (works) :

RewriteEngine On
RewriteRule ^product/([^/]*)\.php$ /model.php?model=$1 [L]

But now I need to have a 301 redirect to the static pages to tell the search engines to index the static url's instead of the dynamic url's. I already updated robots.txt not to crawl the model.php pages.

 

Can I do this in PHP or do I need to add another rewriterule in .htaccess - if so, how ?

I already tried so many things, none of them work.

 

Thanks;

P.

Link to comment
https://forums.phpfreaks.com/topic/282467-301-redirect-dynamic-urls/
Share on other sites

If you are always providing the static url's in your navigation and links, there is no reason to worry about the php pages. Neither users nor search engines will see them.

 

The fact of the matter is that you have php pages for your site, and if you redirected from them, you would have an endless loop of redirects.

If you are always providing the static url's in your navigation and links, there is no reason to worry about the php pages. Neither users nor search engines will see them.

 

The fact of the matter is that you have php pages for your site, and if you redirected from them, you would have an endless loop of redirects.

 

But most of the dynamic url's are already in the search engines, I guess I need to tell them those URL's are invalid ?

You can add some code to your scripts to check the value of $_SERVER['REQUEST_URI']. If it contains your model.php script name, then you can lookup what the proper static URL is and redirect them. $_SERVER['REQUEST_URI'] should reflect what the user typed into the browser for the URL, rather than the re-written URL.

 

 

Alternativly you could add an extra _GET variable to your rewrite rule such as rewrite=1. In your script test if $_GET['rewrite'] is set, and if not, redirect.

 

Either way, you need to make sure your site's existing navigation and other links are updated to the new static URLs. The search engines will get them indexed and start using them as time passes.

But now I need to have a 301 redirect to the static pages to tell the search engines to index the static url's instead of the dynamic url's

/quote]

 

You have no static URL's, just differenty built dynamic ones. Google doesn't care either way, as long as the URL describes what it's pointing at.

 

 I guess I need to tell them those URL's are invalid ?

[/quoet]

 

No, absolutely not. If you do that, the searchengines will think the page has been deleted instead of being replaced. They will drop the ranking for the old page and start from zero with the new one, making possibly your site drop in rank dramatically.

 

You are moving pages, thus 301 is a must.

Well, ok, so theoretically, what you could do is setup a redirect rule and include the "NS" (No sub request) although I haven't tested this, it's worth a try.

 

RewriteCond %{QUERY_STRING} ^model=(\w+)$
RewriteRule ^model\.php$ /product/%1.php [NS,R=301,L]
RewriteCond %{QUERY_STRING} ^model=(\w+)$
RewriteRule ^model\.php$ /product/%1.php [NS,R=301,L]

 

Does not seem to work, this is how I create the SEO urls :

RewriteEngine On
RewriteRule ^seo-friendly-foldername/([^/]*)\.php$ /product.php?model=$1 [L]

The dynamic link looks like :

http://www.site.org/product.php?model=12345

The SEO url looks like

http://www.site.org/seo-friendly-foldername/12345.php

 

The 301 redirect should become : http://www.site.org/seo-friendly-foldername/12345.php

 

Any ideas ?

Let me get this straight -- you provided an example that doesn't actually match what you have, although you didn't indicate that to anyone.

 

I then used your example to provide you a Rewrite condition and rule, which of course doesn't work.

 

Am I to understand that you did not try and translate what I gave you to match your actual configuration?

Let me get this straight -- you provided an example that doesn't actually match what you have, although you didn't indicate that to anyone.

 

I then used your example to provide you a Rewrite condition and rule, which of course doesn't work.

 

Am I to understand that you did not try and translate what I gave you to match your actual configuration?

 

I tried, but it seems to return the same dynamic url. Since, obviously I made a mistake in the original post I tried to correct it with a better example. I'm sorry that I gave the wrong details, for some reason I cannot match the code with the actual configuration ....

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.