How to redirect all wordpress posts and pages to a new domain.? - wordpress

i need a 301 redirect
www.old-domain.com/simple-post/ to go to www.new-domain.com (not new-domain.com/simple-post)
I tried using some .htaccess rules and did nothing,
mod-rewrite is enabled
thanks

If you are transfering your WordPress website from a domain to another, this is the way to go :
http://codex.wordpress.org/Moving_WordPress
Almost all the time theses lines do the trick :
//FIXME: do comment/remove these hack lines. (once the database is updated)
update_option('siteurl', 'http://your.domain.name/the/path' );
update_option('home', 'http://your.domain.name/the/path' );
Of course you also need to change every static links inside your posts and/or template files.
Here is a link that could help replacing links in content :
http://lorelle.wordpress.com/2005/12/01/search-and-replace-in-wordpress-mysql-database/

put this code on header.php
pass in "get_the_ID(1)" post(simple-post)id.
set redirect url.
<?php if (get_the_ID(1)) { ?>
<script type="text/javascript">
window.location = "http://www.google.com";
</script>
<?php } ?>

Related

Pretty URLs for named anchors on WordPress page

I'm working on a WordPress site in which the services page is one long page with anchors for each section.
www.mysite.com/services/#human-resources jumps to the Human Resources section as expected.
Is there any way in the .htaccess file to make a url such as www.mysite.com/services/human-resources/ jump directly to the anchor?
I've tried several different rewrite rules, the closest I've gotten is:
RewriteRule services/human-resources services/#human-resources [NE,L,R=301]
But this shows the # in the url which defeats the purpose. Removing the R=301 just causes it to do nothing.
Thanks.
I have been working on a similar site and this is how I solved that issue.
First add this action to functions.php, witch redirects the user to the proper anchor on the page.
function url_handler(){
$path = substr($_SERVER['REQUEST_URI'], 1); //get the path minus the '/'
if( is_page() || is_404() ){ // do you own conditional tags
wp_redirect( home_url("#" . $path) ); //redirect to the #anchor
exit();
}
}
add_action( 'template_redirect', 'url_handler' );
Next you need to make a javascirpt handler witch gets the url of the page (the url with the anchor) and make the proper event to scroll to that section. Something like this:
var hash = window.location.hash;
hash = hash.replace('#','');
hash = hash.replace('/','');
/* do stuff here */
Hope this helps

how to set a header.php redirect in wordpress?

i just want to ask how to set a redirect url in header.php? it seems I am having a redirect loop.
<?php
header("Location: http://www.sitename.com/category/videos/");
?>
You should use wp_redirect instead of the php header() function. Hook the call to the wp_loaded to prevent the "headers already sent"-error.
Example: (add to functions.php)
add_action ('wp_loaded', 'my_redirect_function');
function my_redirect_function() {
// define when the redirect should be made
// example: only redirect for the page with slug "about-me"
if(!is_page( 'about-me' )){
return;
}
// define your url here
$url = 'http://google.com';
wp_redirect($url);
exit;
}
You Can put you code directly in header.php
or
if($_SERVER['REQUEST_URI']=="your URL When you want redirect")
{
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.sitename.com/category/videos/");
exit;
}
Place the following HTML redirect code in a WordPress Page or Post:
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourURL.com/index.htm">
This code tells your browser to auto-refresh the current Web page. When it does, it loads the URL in the "url" string. The "0" attribute associated to "content" determines how many seconds before the redirect takes place. The attributes associated with "content" and "url" are the only ones you should alter.

Redirect outside WP after Login

I have http://mysite.com/admin.php
There I check wether the user is admin or not.
In second case I send the user to the wp-login page like this:
blog.mysite.com/wp-login.php?redirect_to=http%3A%2F%2Fmysite.com/admin.php
I expect redirect back for admin.php but wordpress always send me to wp-admin control panel.
I have researched.
When the dest. host is not in filter
allowed_redirect_hosts
WP just redirect the user to wp-admin.
How can I add more hosts to the filter?
If I put this example from the WP Codex on functions.php it stops working.
(http://codex.wordpress.org/Plugin_API/Filter_Reference/allowed_redirect_hosts)
add_filter( 'allowed_redirect_hosts' , 'my_allowed_redirect_hosts' , 10 );
function my_allowed_redirect_hosts($content){
$content[] = 'blog.example.com';
$content[] = 'codex.example.com';
// wrong: $content[] = 'http://codex.example.com';
return $content;
}
Add the following in your functions.php:
function my_allowed_redirect_hosts($allowed_host) {
$allowed_host[] = 'anothersite.com';
$allowed_host[] = 'www.someotherwebsite.com';
return $allowed_host;
}
add_filter('allowed_redirect_hosts','my_allowed_redirect_hosts');
Replace anothersite.com and add new values accordingly.
If you're trying to redirect users in a normal page, you can make use of Wordpress's wp_redirect() function:
<?php
wp_redirect( $location, $status );
exit;
?>
Documentation: wp_redirect()
Hope this helps!
Finally it works!
What I was doing wrong is putting the code in the WP functions.php file, and not in my custom theme functions.php file.
Thanks all!

Wordpress Redirect page

I search several times but I couldn't find nothing that really helps me.
Basically I have a very VERY simple Theme which is working very well.
I also have a database with some IP's and depending by this IP I need to redirect the guest to different pages.
So on my template index.php file I added:
if ($country == "br") {
echo "case1";
} else {
$location = bloginfo('template_url')."/index-nbr.php";
echo $location;
?>
<script type="text/javascript">
window.location= <?php echo "'" . $location . "'"; ?>;
</script>
<?php
}
I tried many different ways to redirect but none of them work. Could someone help me with this stuff?
Ok let me make your life easier.
Just install this plugin you will find a text box on the bottom of every page in PAGES section in Admin panel where you can put your desired link/URL of the page and that will redirect you to your inserted URL.
Page Link to Plugin

need the right way to setcookie in wordpress

i've been looking the whole day how to setcookies in wordpress. in my way i found out (using the developer toolbar) that the cookie is set but still not working.
i have 2 files the first contains the login form redirecting to another page to set the cookie and return to another page to check if it's working. domain which is tested on is like this : blog.mydomain.com. here's the setcookie file :
<?php
setcookie("user_name","test",time()+3600);
?>
and chcking the cookie like this :
if(isset($_COOKIE["user_name"])){
echo "cookie exists";
}
else{
echo "cookie doesn't exist";
}
i've read many topics about this issue but there was no clear answer.
Thanks in advance
This typically happens when you try to set a cookie after sending output to the browser. To set a cookie in WP, you should use the 'init' hook to set the cookie on init.
function set_username_cookie() {
if (!isset($_COOKIE['user_name'])) {
setcookie("user_name","test",time()+3600);
}
}
add_action( 'init', 'set_username_cookie');
well, my best way to use cookie in wordpress is this,
function set_my_cookie() {
global $post;
$post_id = $post->ID;
$cookie_name = "my_cookie";
$cookie_value = "my_cookie_val";
if (!isset($_COOKIE['my_cookie'])) {
{
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
}}add_action( 'wp', 'set_my_cookie');
i used this the function to setcookie in wp hook of wordpress. the main reason of this is that we may need sometime current page or post, that we cannot access on init hook, but we can access in wp hook.
now, in a shortcode or other plugin/theme functions we may just need to check if the cookie exists or not. thats it
Another option is to use PHP's ob_start(); and ob_end_flush();.
You can find documentation on the two functions here
The way I resolved my issues was to call the two functions before and after the opening and closing html tags like this:
<?php ob_start(); ?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php /* Wordpress loop and other tempate code here */ ?>
</body>
</html>
<?php ob_end_flush(); ?>
The issue I was running into was calling a global function that used PHP's setcookie(); and because WordPress processes the page progressively, the cookie couldn't be created due to the page's headers already being sent.
PHP's output buffering function forces the headers to be sent before WordPress processes the page.
Hope this helps.

Resources