I'm actually not sure where to start with this one.
Basically I have a buddy in SEO and one of his clients sites has been duplicated by a certain web hosting service and the duplicate has not been removed in spite of turning it off. The preview site in question has a higher PR than the actual client site so SEO is being completely destroyed as a result.
The only control we have managed to get over the situation is the fact that the files (but not the .htaccess) are propagating from the client's server. Basically, it's a freaking nightmare.
What I am proposing to do is set up a conditional redirect in the PHP header files so that if the browser/engine hits the preview site, it will 301 to the client site. If, however, the browser/engine is hitting the correct site, we need to do nothing.
While I could probably figure out the syntax by trial & error, this is sort of time sensitive since the client in question has an SEO campaign that is going straight to Hades.
I'm hoping someone here will know how to format the conditional redirect.
Let's call the two domains:
previewdomain.com
domain.com
So loosely speaking...
if current url == previewdomain.com then 301 redirect to domain.com
else do nothing
Thanks for the assist!
This'll do:
if( stripos($_SERVER['HTTP_HOST'],"previewdomain.com") !== false) {
header("Location: http://domain.com".$_SERVER['REQUEST_URI'],true,301);
exit;
}
<?php
if(strpos($_SERVER['HTTP_HOST'], 'previewdomain.com') !== FALSE) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.domain.com");
}
Related
I'm trying to redirect hits on https://www.mobilemultimedia.be to the one of the languages I'm supporting like https://www.mobilemultimedia.be/en/
If I detect a browser language in English, I use the following code:
$browser_language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
http_response_code(301);
if (preg_match("/(fr|en|nl|it|es|de)/",$browser_language))
{
header("Location: ".$domain."/".$browser_language."/");
}
else
{
header("Location: ".$domain."/en/");
}
When I try it with my browser, it seems to work fine but if I use Google's indexation tool, it tells me that the page has a soft 404 and because of that it can't be indexed. I have set a http code 301 to avoid this but it doesn't seem to work.
If I check redirections with http://www.redirect-checker.org/index.php I see a 301 leading to the right page but if I test it with Google or Webpagetest.org or Pingdom tools, the redirection doesn't seem to work.
any idea?
I am really a noob when it comes to PHP and I would like to ask for your help. I would like to create a 404.php for my Wordpress site that shows the 404 page in the appropriate language. The site is multilingual: Dutch and English. (I tried some plugins but they don't work with my current theme). I have done some research and found some bits and pieces and mastered a piece of php code that doesn't work. If you could be so kind to point me in the right direction, that would be great. This is what I came up with:
<?php
$incomingUrl = $_SERVER['REQUEST_URI'];
if($incomingUrl == 'damcms.com/en') {
wp_redirect(damcms.com/en/page-not-found-404);
exit;
} else {
wp_redirect(damcms.com/pagina-niet-gevonden-404);
exit;
}
?>
I don't get an error message, nothing is being shown. So I miss something, what?Thanks in advance.
Claudia
First, the reason nothing happens is here:
wp_redirect(damcms.com/en/page-not-found-404);
Any time you have a string in PHP it must be in quotes; otherwise PHP tries to interpret it as instructions (and damcms.com/en/page-not-found-404 is not a valid PHP instruction).
So this would work:
wp_redirect('damcms.com/en/page-not-found-404');
But there's a bigger issue: you don't want to just check if the URL is exactly damcms.com/en — you want to check if it starts with damcms.com/en.
So, perhaps this:
if(strpos($_SERVER['REQUEST_URI'], 'damcms.com/en') !== false)) {
wp_redirect('damcms.com/en/page-not-found-404');
} else {
wp_redirect('damcms.com/pagina-niet-gevonden-404');
}
exit;
But going one level deeper, even this is a little iffy. WordPress already has a 404.php template that shows for any 404 page. I strongly recommend editing that — and there do the if ... 'damcms.com/en' test and then perhaps just include a 404-en.php template. That way the existing 404 mechanism still works. With the setup you have, the visitor is being redirected to a 200 page, so to a search engine it will appear your site has no 404s at all and everything is valid content!
I'm currently using the excellent mobile detection script from: detectmobilebrowsers.mobi
This works really well however, it redirects every and any page on your main site (including any query parameters) to your mobile site's home page.
What I need is:
http://www.mydomain.com/page.php?var1=X&var2=Y
to direct to:
http://mobile.mydomain.com/page.php?var1=X&var2=Y
I have multiple pages that should redirect with the query string to their mobile versions.
What's the best way to approach this? I thought that I should:
Examine the $_SERVER['HTTP_REFERER'] for the page and query string, use a switch/case to loop through the 10 or so pages that I need matching on the main and mobile sites then change the referer URL in the mobile detection script.
Does this make sense?
I've been struggling to get the page and query... any advise and thoughts welcome.
if ($mobile_is_detected) {
header('Location: http://mobile.mydomain.com' . $_SERVER['REQUEST_URI']);
exit;
}
In addition to Andy's answer, when redirecting you should set the response status to 301.
Be careful, you may not call header() if you have printed any HTML or echoed anything before calling the function.
if ($mobile_is_detected) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://mobile.mydomain.com' . $_SERVER['REQUEST_URI']);
}
You can use $_SERVER['QUERY_STRING'] to redirect to add the query string to the redirect URL in the first place.
We use varnish as our load balancer (among other things) but we get some strange behavior at the moment.
We have a script that gets called with some parameters, and depending on what parmas you pass, you get redirected to a different location using a 301 redirect (this it done with a php script and the header() function)
The problem is that the first time a URL is begin called the 301 redirect happens, but then the next time that same URL is called, you get a status of 200 OK, no redirect happens and just a white page is displayed.
I've added a session_start() to the top of the php script to try and stop varnish from caching the page, but nothing helped so far.
I've done some research regarding this issue, and saw that several people experience the same problem, but I wasn't able to find a solution yet.
How would I get varnish the stop caching the page?
Any help in the right direction will be appreciated.
Could you not exclude that url from the varnish cache?
Add something like the following to your default.vcl (or whatever your varnish config file is called).
sub vcl_recv {
if(req.url ~"^/thatpagethatredirects") {
return (pass);
}
}
This should stop varnish caching that url.
You could try finding the url that varnish is redirecting to and adding a query string with a randomly generated number to it.
Example:
<?php
$random_number = rand(10000, 99999999);
// This is what the redirect code MIGHT look like, but I doubt it.
header("Location: http://www.example.com/index.php?cache=$random_number");
?>
If you can find where the page is actually doing the redirecting and you add a random number query string, it should fix things. I have used this method of making sure images are not cached in the past and it always worked for me perfectly.
Oh, and if you can't find the redirect code that varnish is using itself. You could try adding this to the page that varnish loads after the 301 redirect:
<?php
$random_number = rand(10000, 99999999);
header("Location: NAME_OF_THIS_SCRIPT.php?cache=$random_number");
?>
Pretty much the same idea, just involves less hunting around. I'm not sure if this will break the load balancing function of varnish though.
This is rather awkward and is supposed to work correctly by default. Can you tell us what version of Varnish you are using and if you created a custom vcl file?
The bug was probably introduced in vcl_fetch. This should check for cacheability with checks like:
sub vcl_fetch {
...
if (req.status >= 300 ) {
return pass;
}
if ( ! obj.cacheable ) {
return pass;
}
..
}
so i want to do an external permanant redirect (301) from http://www.creya.com to http://creya.com.
i am not using apache but rather, abyss web server and i can't figure out the url rewrite rules. but i believe i could also do this at the app level with php.
i think wordpress does do this. i set http://creya.com/blog as your blog url and try to hit http://www.creya.com/blog; it redirects to http://creya.com/blog. i want to do the same thing.
any ideas how i can make this hijacking happen?
thanks in advance.
This should do it-
if($_SERVER['SERVER_NAME']!='creya.com')
{
Header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://creya.com".$_SERVER['REQUEST_URI']);
}
try
if(substr($_SERVER['SERVER_NAME'],0,4) == 'www.')
header("Location: http://". substr($_SERVER['SERVER_NAME'], 4)
Long time since I coded php, so can't remember how to get the full path, read a bit here (http://php.net/manual/en/reserved.variables.server.php) and change the last $_SERVER['SERVER_NAME']