stegers72 Posted September 27, 2013 Share Posted September 27, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282467-301-redirect-dynamic-urls/ Share on other sites More sharing options...
gizmola Posted September 27, 2013 Share Posted September 27, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282467-301-redirect-dynamic-urls/#findComment-1451350 Share on other sites More sharing options...
stegers72 Posted September 27, 2013 Author Share Posted September 27, 2013 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/282467-301-redirect-dynamic-urls/#findComment-1451352 Share on other sites More sharing options...
kicken Posted September 27, 2013 Share Posted September 27, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282467-301-redirect-dynamic-urls/#findComment-1451355 Share on other sites More sharing options...
vinny42 Posted September 27, 2013 Share Posted September 27, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282467-301-redirect-dynamic-urls/#findComment-1451357 Share on other sites More sharing options...
gizmola Posted September 27, 2013 Share Posted September 27, 2013 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] Quote Link to comment https://forums.phpfreaks.com/topic/282467-301-redirect-dynamic-urls/#findComment-1451363 Share on other sites More sharing options...
stegers72 Posted September 27, 2013 Author Share Posted September 27, 2013 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/282467-301-redirect-dynamic-urls/#findComment-1451429 Share on other sites More sharing options...
gizmola Posted September 27, 2013 Share Posted September 27, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/282467-301-redirect-dynamic-urls/#findComment-1451442 Share on other sites More sharing options...
stegers72 Posted September 27, 2013 Author Share Posted September 27, 2013 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 .... Quote Link to comment https://forums.phpfreaks.com/topic/282467-301-redirect-dynamic-urls/#findComment-1451469 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.