Mobile app version of vmapp.org
Login or Join
Caterina187

: Is it OK for SEO to send a 200 status when I'm actually rewriting the request to a controller and then outputting content to a page dynamically? After reading endless pages about redirects

@Caterina187

Posted in: #HttpHeaders #Redirects #Seo

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.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Caterina187

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ann8826881

...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?)


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.
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.
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:


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.)
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]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme