0

I've been reading about redirection, and how it can affect (or not if done properly) SEO.

I'm changing my website's content platform from Drupal to a PHP custom made code.

In my current site I have two links that point to the same link like this:

.../node/123

.../my-node-title

Mainly because Drupal allows you to create a custom-made links, so every article has a default one (node/123) and the custom-made one (/my-node-title).

My question is about what to do in order to prevent losing any SEO that each link may have.

In the new website all articles are structured like this: content.php?id=123

I've stored in the database the custom-made link of every article.

Instead of doing a 301 redirect I'm rewriting all links that do not exist to be redirected to redirect.php page to process the request. There I take the string from the link, look for it in the database and redirect the user.

The process is like this:

in .htaccess file:

RewriteRule ^.*$ ./redirect.php

In redirect.php:

I grab the $_SERVER['REQUEST_URI'] and using explode() I get the last part of the link (ie. my-node-title), look for it in the database and grab the ID of the article (ie. 123) and save it in a $link variable.

Then I use header() function and do the redirect: header('Location: '.$link);

So, people still click on .../my-node-title but when the article loads at the navigation bar appears /content.php?id=123

I would like to know your comments about this solution. I know that with SEO there are not fixed rules, or certainty in anything, but I would like to know if what am I doing is acceptable. Thanks!

5
  • 2
    Why wouldn't you send a 301 redirect from the PHP script? It can do so: stackoverflow.com/questions/7324645/… It is just a matter of using header("HTTP/1.1 301 Moved Permanently"); before the location line. Nov 19, 2018 at 13:58
  • You mean sending the information as well? Like: header("HTTP/1.1 301 Moved Permanently"); and then header('Location: '.$link);?
    – Rosamunda
    Nov 19, 2018 at 14:00
  • 2
  • Thank you! So, you think that even if the link end up with a ?id=X it should not affect it's SEO?
    – Rosamunda
    Nov 19, 2018 at 14:02
  • Just setting the Location header with PHP will implicitly trigger PHP to also send a 302 response status. There is no redirect unless a 3xx status is sent.
    – MrWhite
    Nov 19, 2018 at 14:52

1 Answer 1

1

Use 301 redirect before location:

header("HTTP/1.1 301 Moved Permanently");

Also, You can use rewrite rule with 301 for each article in htaccess. Htaccess generated each time by php script in cron and/or when new article is added. This serves request faster then redirect.php if you have less then thousands of articles. This was you can make human readable urls for articles also.
This way you skip execution of php and one db request to grab article name and find id.

Here is how one of my htaccess looks like: Part between special comments replaced by script, all other configuration keeps unchanged by script:

# here non rewrite options, skipped
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,L]
# startreplace
RewriteRule ^HumanReadableTitle1.html$ blog.php [E=HumanURL:1,L]
RewriteRule ^HumanReadableTitle2.html$ certificates.php [E=HumanURL:1,L]
RewriteRule ^HumanReadableTitle3.html$ blog_post.php?id=15 [E=HumanURL:1,L]
RewriteRule ^HumanReadableTitle4.html$ blog_post.php?id=16 [E=HumanURL:1,L]
# endreplace
</IfModule>

'HumanReadableTitles' readed from DB. UTF8 encoded, but not URL encoded, mod_rewrite allow this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.