2

After reading endless pages about redirects and best practice, I'm still unsure if what I'm doing here is good, bad, or indifferent as far as SEO is concerned.

I have a table in a database that holds various URLs - some are straight redirects where pages have moved, and some are to translate user-friendly URLs.

The logic of my setup:

  • If file or directory doesn't exist, send request to controller.php
  • Controller checks if the request URL exists in the database
  • If it does, and it's just an user-friendly URL, then it sends a 200 header and outputs the page content (without changing the user-friendly URL in the address bar)
  • If it's a simple redirect, it sends a 301 redirect header to the new URL (which will be a user-friendly one and go through this process again)
  • If it can't be found in the database, then it sends a 404 header and outputs the content of the 404 page.

My .htaccess is as follows:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^\/controller\.php$ /controller.php [L,NC]

and my controller.php is as follows:

$originalURI = $_SERVER['REQUEST_URI'];

$controller = new Controller($originalURI);

if($controller->GetRedirectAddress())
{
    // will output either 200 or 301 status code depending on type of link
    header($_SERVER["SERVER_PROTOCOL"] . " " . $controller->httpStatusText, true, $controller->httpStatusCode);
    echo $controller->OutputRequestToPage();
}
else
{
    header($_SERVER["SERVER_PROTOCOL"] . " 404 Page not found ({$originalURI})", true, 404);
    echo $controller->Output404Page();
}

The site I'm updating has a lot of traffic and the last thing I want to do is have a negative impact on their page rankings so any advice here is very welcome.

Please let me know if any further info is needed in the comments and I'll update as necessary.

  • You appear to have implemented the front-controller pattern. It is very common and many sites with good SEO implement it. The most popular content management system (WordPress) uses this pattern. I'm not sure exactly what you question is though. I'm not sure what you mean by "headers after redirect" as you don't appear to be sending any headers after issuing a redirect. – Stephen Ostermiller Aug 10 '17 at 14:05
  • Cheers @StephenOstermiller - in it's simplest form my question is "Is it okay to send a 200 status when I'm actually redirecting the request to a controller and then outputting content to a page dynamically?" The code is just for context as I wasn't aware it was a common practice and wasn't sure the logic would make sense – Sam Aug 10 '17 at 14:09
  • I've updated the title to (hopefully) avoid confusion - "redirect" was poor terminology on my part (still a bit of a n00b with this...) – Sam Aug 10 '17 at 14:10
  • I tend to assume that "redirect" means a HTTP 301 or 302 redirect. I use the terminology "rewrite" for the type of "internal redirect" that mod_rewrite usually does. I've re-opened the question since you have clarified. – Stephen Ostermiller Aug 10 '17 at 14:12
  • Thanks for editing the title - my lack of experience is clearly showing! I'm hoping to stick around and learn/contribute as much as possible so all notes taken and I'll try to use clearer terminology in future. Thanks again :) – Sam Aug 10 '17 at 14:34
1

...then it sends a 200 header and outputs the page content (without changing the URL in the address bar)

What you are doing sounds perfectly OK. When you send a valid response then a 200 OK status is the correct thing to do. There is only 1 response from your server in this instance, so what other code(s) do you think you could return here?

Is it okay to send a 200 status when I'm actually redirecting the request to a controller and then outputting content to a page dynamically?

There may be a bit of confusion as to what is actually happening here; what an internal rewrite actually is? Although you seem to be describing the process perfectly OK. (Maybe the penny dropped in comments?)

  1. User makes a request to one of your user-friendly URLs (note, strictly speaking they are user friendly, rather than SEO friendly URLs. Search engines don't care so much. Although I suppose "SEO" is a bit of an umbrella term.) eg. example.com/friendly-url.

  2. Since this URL does not map directly to a file or directory, the request is internally rewritten to /controller.php (handled by your .htaccess code). This design pattern is called a "front controller". This is entirely internal to your server. The user (or search engine bot) is completely unaware of what is really going on - the complexity involved in generating your page is hidden from view.

  3. Your "controller" decides this is a valid request, builds the page and sends it back to the client with a 200 OK status. That's it, finished. As far as the client/user/search bot is concerned it might as well have been a static HTML page. (In fact, it might have been if you had pulled it as-is from a server-side cache of some kind.)

RewriteRule !^\/controller\.php$ /controller.php [L,NC]

However, there's a couple of minor issues with your RewriteRule directive:

  1. In a per-directory / .htaccess context, the RewriteRule pattern (ie. !^\/controller\.php$) will always be successful. The URL-path will never match /controller.php, because the directory-prefix is first removed before the URL-path is matched, so the URL-path never starts with a slash. So, this should be: !^controller\.php$. However, the rewrite-loop would still have been prevented because the URL would have passed through unchanged (it just stops a bit later). (There's also no need to escape slashes in the regex, since there are no slash delimiters.)

  2. You should remove the NC flag. This enables a case-insensitive match, as you are no doubt aware. However, this is applied to a negated pattern. You want it to rewrite when it's not "controller.php" (exactly), not when it's not "CONTROLLER.PHP" or "CoNtRoLlEr.pHp", etc. Unlikely to cause a problem, but it could and NC is just more work.

So, we have...

RewriteRule !^controller\.php$ /controller.php [L]
  • Cheers - I've edited my question to replace SEO-friendly to user-friendly and I'll await your guidance on the rewrite rule – Sam Aug 10 '17 at 15:36
  • 1
    The initial / was actually a typo (I'd amended the text to hide the actual URL) but the information is obviously still valid and very useful so I'm glad I did! I never thought about the NC flag with a negating pattern either that's a very good point. Overall a very helpful answer - I've learnt so much just from a single post! Thanks for spending the time to explain - I promise it shan't go to waste – Sam Aug 10 '17 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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