Php 301 redirect
-
Hi
I am migrating an old wordpress site to a custom PHP site and the URL profiles will be different, so want to retain all link profiles and more importantly if a user visits the old urls via search then they are seamlessly transferred to the new equivalent page
For example
www.domain.com/about-us is going to need to redirect to www.domain.com/aboutus.php
www.domain.com/furniture is going to need to redirect to www.domain.com/furniture-collections.php
etc
What is the best way of achieving this apart from .htaccess as not 100% confident of doing this. Could it be done via PHP or using meta tags?
-
Hi, Not meta tags. If no other way, You should do it with PHP header() function.
<code>header("HTTP/1.1 301 Moved Permanently"); header("Location: /somelocation");</code>
But you need to add some conditions to determine if the page should be redirected. For example to redirect /example.html to https://www.new-domain.com/example you need to write something like:
if (isset($_SERVER['REQUEST_URI']) && parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) == '/example.html')
{header('HTTP/1.1 301 Moved Permanently');header('Location: https://www.new-domain.com/example');exit();}
It can be a lot of code, if you need to redirect many pages. In this case, You can try to use PHP 301 Redirect Generator - https://www.301-redirect.online/php-header-location-generator
-
OK - take this page: www.domain.com/about-us
Presuming that this is a php page, open it up in an editor and insert this code right at the top
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.domain.com/aboutus.php" );
?>and repeat this same procedure for any other files you need to redirect.
-
Hi
Thanks for the link, it mentions under php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );
?>So how would I implement multiple page changes in the code above i.e.
www.domain.com/about-us is going to need to redirect to www.domain.com/aboutus.php
www.domain.com/furniture is going to need to redirect to www.domain.com/furniture-collections.php -
There are various ways of doing this and yes the good ol htaccess can be a bit daunting at first.
Check this page out, should help you.
http://www.webconfs.com/how-to-redirect-a-webpage.phpYou cannot do a 301 redirect with meta tags - a meta tag "refresh" does not provide a 301 redirect.
Useful hint: Before commiting to it, try using the same script either in PHP or HTACCESS but with a 302 (temporary redirect) - then test it works and then make it a 301 (permanent redirect) - that way if you get it wrong, Google will not take the permanent instruction.
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
301 Redirect to Home Page or Sub-Page?
What do you think about 301 redirect of good expired domain to a sub-page instead of the home page? I'm doing this so I don't hurt my brand name. Let me know your thoughts please. Thank you
Intermediate & Advanced SEO | | JuanWork0 -
301 redirecting to anchor points
A client has just given me a list of redirect URLs as requested of them. However, they're wanting to redirect a bunch of pages to various anchor points within the same page. For example: /pages/about might redirect to: /pages/our-story#our-mission And: /pages/history might redirect to: /pages/our-story#history Is there any problem with this? I've never seen or been asked to redirect like this before.
Intermediate & Advanced SEO | | muzzmoz0 -
Problem with redirects in coldfusion
How to redirect pages in cold fusion? If using ColdFusion and modrewrite, the URL will never be redirected from ModRewrite.
Intermediate & Advanced SEO | | alexkatalkin0 -
Canonical OR redirect
Hi, i've a site about sport which cover matches. for each match i've a page. last week there was a match between: T1 v T2 so a page was created: www.domain.com/match/T1vT2 - Page1 this week T2 host T1, so there's a new page www.domain.com/match/T2vT1 - Page2 each page has a unique content with Authorship, but the URL, Title, Description, H1 look very similar cause the only difference is T2 word before T1. though Page2 is available for a few days, on site links & sitemap, for the search query "T2 T1 match" Page1 appears on the SERP (high location). of course i want Page2 to be on SERP for the above query cause it's the relevant match. i even don't see Page2 anywhere on the SERP and i think it wasn't indexed. Questions: 1. do you think google see both pages as duplicated though the content is different? 2. is there a difference when you search for T1 vs T2 OR T2 vs T1 ? 3. should i redirect 301 Page1 to Page2? consider that all content for Page1 and the Authorship G+ will be lost. 4. should i make rel=canonical on Page1 to Page2? 5. should i let google sort it out? i know it's a long one, thanks for your patience. Thanks, Assaf
Intermediate & Advanced SEO | | stassaf0 -
301 redirect with /? in URL
For a Wordpress site that has the ending / in the URL with a ? after it... how can you do a 301 redirect to strip off anything after the / For example how to take this URL domain.com/article-name/?utm_source=feedburner and 301 to this URL domain.com/article-name/ Thank you for the help
Intermediate & Advanced SEO | | COEDMediaGroup0 -
How long for a 301 redirect to pass PR?
Hi, How long does it take for a 301 redirect to pass PR/Juice to the new domain it's redirecting to? From what I understand you tell Google in Webmaster tools this domain is now going to this domain and then setup a file on the old domains hosting to redirect to the new. And that's it! If that is correct how long does it take? 2 days, 2 weeks, months, maybe never??? Cheers
Intermediate & Advanced SEO | | activitysuper0 -
Changing a parent category and 301 redirecting
I have a set of three pages that are subpages of a parent. The structure is as follows: mysite.com/directory/personal-widgets mysite.com/directory/commercial-widgets mysite.com/directory/widgets-services The partent page name "directory" really isn't working for where I want these pages to evolve. So I want to change it to "guides" In a world without worrying about google, I would simply change the parent page to guides, so they look like this, and be done with it: mysite.com/guides/personal-widgets But, the obvious problem is that I have external links to the page now. And the pages have a nice PR. And they also have Facebook page Likes and I don't know if I'll lose those. I know that if I should do this I should redirect the pages to the new pages of course. My question is: Will redirecting the old URL to the new URL with a 301 cause anything negative to happen that I might not be expecting? Does Google dislike Redirects for any reason, or understand they are sometimes necessary?
Intermediate & Advanced SEO | | bizzer0 -
Open Site Explorer not Seeing 301 Redirect to non-www
I cannot figure out why open site explorer doesn't see that when you go to http://preferredroofingkc.net/ it redirects to http://www.preferredroofingkc.net/ This is a wordpress installation that uses a cannonical url http://www.preferredroofingkc.net/ The HTACCESS file also has a 301 redirect as follows: RewriteEngine On
Intermediate & Advanced SEO | | RobertFisher
RewriteBase /
RewriteCond %{HTTP_HOST} ^preferredroofingkc.net [NC]
RewriteRule ^(.*)$ http://www.preferredroofingkc.net/$1 [L,R=301] But, open site explorer still shows these sites separately without alerting that there is a 301 redirect.0