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']
Related
I am trying to use a php script that redirects visitors based on certain criteria. I use the script succesfully on an apache server however, I am experimenting with nginx and php-fpm and the same script doesn't seem to be working as it should.
header("Location: $url");
exit();
The strange thing is it appears to be appending the URL I am trying to redirect to to the original URL so the URL it tries to forward to looks like:
originaldomain.com/redirectdomain.com.
Has anybody ever come across this before where it as appending the redirect domain to the original URL instead of redirecting straight to it?
Please let me know if you need any further information to help.
You need to make sure the URL has http:// at the beginning of it, otherwise it thinks it's going to a path on your domain, and not a redirect to an actual site.
$url has to begin with http:// or https://
if (strpos($url, 'http') === 0 ) {
$newurl = $url;
} else {
$newurl = "http://" . $url;
}
Then just use $newurl in you header request :)
If the above answers are not yet fixed header redirect, you need to check in your php.ini file output_buffering on or off or set limit.
output_buffering = On
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");
}
a website has used a "301 permanent redirect" to my site is there a way i can set code that detects this and displays a page when my website is accessed through this?
Does anyone have any idea about this?
You can get only a referer. I think you will not be able to get the http status code on server which the client gets during last request.
So my answer is NO, you cannot get the 301 status code on your server.
But you can do a little of needed magic with referer variable.
e.g. in PHP you can read this:
$_SERVER['HTTP_REFERER'];
Not much you can do. If you were doing the 301, you could set the referrer to the querystring. But since you're not, you can only grab what the request has given you.
You can try using PHP's $_SERVER['HTTP_REFERER'] to track the source URL from where your visitor comes from. I think it's a bit dodgy though and might not yield the same result in all browsers. Even PHP's documentation says 'it cannot really be trusted'.
Why do you have to use .htaccess for the redirect? You could do something like this:
Site A's index.php:
header("Location: http://siteb.com/?ref=".urlencode('http://sitea.com');
Site B's index.php:
if(isset($_GET['ref']))
{
if($_GET['ref']=='http://sitea.com')
{
// Do something
}
}
Edit:
If you can't edit Site A's code or server settings, try using:
if($_SERVER['HTTP_REFERER']=='http://sitea.com')
{
// Do something
}
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.
I have a site I'm working on with a /landing-page/ folder, and I'll be making a number of landing pages. I'd like to be able to put urls that have /lp/ instead of /landing-page/ in advertisements, and have any url with /lp/ in it replaced with /landing-page/.
Thus:
www.site.com/lp/ipad-inspections
would automatically redirect to:
www.site.com/landing-page/ipad-inspections
I would like to do this without having a /lp/ folder and a second page corresponding to each landing page. My thought is to have the 404 page check the url and redirect, but I can't seem to get the following to work:
<?php /* Automatic redirect for landing pages */
$current_loc = $_SERVER['REQUEST_URI'];
$short_lp = '/lp/';
$long_lp = '/landing-page/';
if (strpos($current_loc, $short_lp)) {
$current_loc = str_replace($short_lp, $long_lp, $current_loc, 1);
header("location: ".$current_loc);
}
What am I doing wrong here, that my page is coming up blank? I have narrowed it down to the first line in my if statement, which is crashing my page.
If there a better way to do this with apache? Some line I can put in htaccess, maybe?
RedirectPermanent /lp /landing-page
in your .htaccess should do the trick. Best to do this sort of unconditional redirect BEFORE it reaches the PHP stage. It's essentially a "free" operation in Apache, and saves your server the whole parse/compile/execute PHP stages.
Keep it as you've done it but include the 301 permanently moved header.
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);
Also try referencing your URL absolutely, so as to include http://www.domain.com/
Update
If you want to redirect with htaccess try
redirect 301 /lp/landing-page/ http://www.domain.com/new.html