Jump to content

  • Log in with Facebook Log in with Twitter Log In with Google      Sign In   
  • Create Account

Subscribe to HRA Now!

 



Are you a Google Analytics enthusiast?

Share and download Custom Google Analytics Reports, dashboards and advanced segments--for FREE! 

 



 

 www.CustomReportSharing.com 

From the folks who brought you High Rankings!



Photo
- - - - -

Permanent 301 Redirect


  • This topic is locked This topic is locked
320 replies to this topic

#301 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 14 December 2006 - 05:08 PM

Yep. That's one way to do it. wink.gif

<edit for correction>

I missed a syntax error in there. David PM'd me because it wasn't working. Basically, ya gotta make sure you're escaping the periods in your RewriteCond lines because those characters have a special meaning in REGEX. So here's the same code with a tiny, tiny correction:
CODE
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite\.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

Edited by Randy, 15 December 2006 - 08:16 AM.


#302 tictoc

tictoc

    HR 1

  • Members
  • Pip
  • 1 posts
  • Location:dallas, tx

Posted 02 February 2007 - 10:42 PM

This has been a great read and an excellent forum. I was hoping to find someone in the same scenario as me but have not found it yet. I am not really sure what to do. I have posted in other webmaster forums but still have no clue as to what to do about this issue:

I am trying to do a redirect on shared windows hosting. The problem is my site is being indexed supplemental in google with the https:// (secure which I do not even use) and the non-www regular URL. I would rather everything direct to the http://www.mysite.com.

I called the webhost and they offer no advice of even how to shut off the https:// since its on shared hosting for my site only. If I could find code or a way to somehow to 301 redirect these https:// and non-www pages to the www.mysite.com pages I think the problem could be corrected.

I also need to mention this is a site using .asp.

I hope someone can help. Thanks!!

#303 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 08 February 2007 - 08:02 AM

FTR we're not ignoring you tictoc. What you're asking is pretty easy to do on a *nix server, but to my knowledge IIS just doesn't offer this. Well, it may if you have ISAPI installed, but your host should have told you the way to use it if it were installed.

I have a feeling you may end up having to use a scripted solution instead. Not the best solution perhaps, but should be able to accomplish the task.

I'm not an IIS/ASP person, but I think you should be able to use ASP's Request.ServerVariables in a little if conditional statement to do the job. Maybe something along the lines of
CODE
if Request.ServerVariables("HTTPS") = "on" Then

...code to do the redirect back to the http side of things...
...don't forget to capture any query strings if you need to...


If you're familiar with ASP hopefully this will give you a place to start.

Oh, and welcome ! hi.gif

#304 chrishirst

chrishirst

    A not so moderate moderator.

  • Moderator
  • 6,725 posts
  • Location:Blackpool UK

Posted 08 February 2007 - 05:28 PM

Can't think of a way to manage this via the IIS management console

but with scripting, request.servervariables("HTTPS") = "on" should do the trick or

request.servervariables("SERVER_PORT") <> "80" (OR = "443" )

if not isnull(request.servervariables("SERVER_SECURE_PORT") ) or request.servervariables("SERVER_SECURE_PORT") <> "0"

#305 BTAR

BTAR

    HR 1

  • Members
  • Pip
  • 1 posts

Posted 10 February 2007 - 01:40 AM

Hi everyone,

This has been a most informative thread!
My site is ASP based running on II6 at WebCentral.com.au

I have two domains - one is an abbreviated version of the other which is primarily used for email as it's shorter. They are ratb.com.au and rockaroundtheblock.com.au
I can see Google is indexing both domains which is bad, due to duplicate content. I want the long domain used as the web address.

What I want to do is detect which domain has been used to enter the site, and then 301 redirect it to the other. I've put this code into global.asa but it only works when entering the domain - if someone enters via a bookmarked / google indexed link it doesn't work.

[codebox]Sub Session_OnStart
dim safeURL

'***** ONLY CHANGE THE LINE BELOW *****
safeURL = "rockaroundtheblock.com.au" 'domain name
'***** ONLY CHANGE THE LINE ABOVE *****

dim requestedURL
dim requestedWithPre
dim pre
dim sendTo

pre = "www."

requestedURL = LCase(Request.ServerVariables("SERVER_NAME"))

If InStr(1, requestedURL, "www.", 1) = 1 Then requestedWithPre = True

If (requestedURL <> safeURL) AND (requestedURL <> pre & safeURL) Then

sendTo = "http://"

If requestedWithPre Then
sendTo = sendTo & pre & safeURL
Else
sendTo = sendTo & safeURL
End If

Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", sendTo

End If
End Sub
[/codebox]

My host webcentral has provided this code but I can't get it to work
[codebox]
ASP Driven Multiple Domain Redirection
Web sites are typically attached to an IP address, but sometimes several domain names may point to the same IP. A clever ASP script could display the same page different ways, or one of several different pages, depending on which domain name was typed utilizing the HTTP_HOST server variable. Our site, for example has four domain names tied to the same IP ( domain1.com, domain2.com domain3.com and domain4.com ) and the following script will provide different results depending on what domain name it is called from:

<%response.buffer=true%>
<%
Dim theSRV
theSRV = Request.ServerVariables("SERVER_NAME")
Select Case theSRV
Case "www.domain1.com" Response.Redirect("/page1.htm")
Case "www.domain2.com" Response.Redirect("/page2.htm")
Case "www.domain3.com" Response.Redirect("/directory1")
Case "www.domain4.com" Response.Redirect("/directory2")
End Select
%>

<HTML>

<BODY>

To alter this code, simply replace the sections currently highlighted in blue, with your own domain names and your own directories.

This will mean that when a user enters www.domain1.com they will see www.domain1.com/page1.htm; www.domain3.com will return www.domain3.com/directory1 etc, making the redirection process appear just a little more seamless. You could replace the values with anything you would like, relative path's, as included in the example, or fully qualified remote paths to redirect your users to one of your sites on a completely different server.

The same Code Principles can be used for other variables also, such as redirecting token users based on there user name, or using the Response.Write function instead of Response.Redirect to simply generate a different page on the fly. [/codebox]

I've tried entering the "Case" line as
Case "www.ratb.com.au" Response.Status = "301 Moved Permanently" Response.addheader "Location", "http://www.rockaroun...eblock.com.au/" Response.End

but that doesn't work either!

What I really want to figure out is how to redirect a page by switching out ratb and replacing it with rockaroundtheblock

#306 seadog

seadog

    HR 3

  • Active Members
  • PipPipPip
  • 88 posts

Posted 12 March 2007 - 02:35 PM

I am having the same exact problem with an ASP site. I'll be watching here for any comments. It would be appreciated smile.gif

QUOTE(DeKa @ Feb 10 2007, 12:40 AM) View Post
Hi everyone,

This has been a most informative thread!
My site is ASP based running on II6 at WebCentral.com.au

I have two domains - one is an abbreviated version of the other which is primarily used for email as it's shorter. They are ratb.com.au and rockaroundtheblock.com.au
I can see Google is indexing both domains which is bad, due to duplicate content. I want the long domain used as the web address.

What I want to do is detect which domain has been used to enter the site, and then 301 redirect it to the other. I've put this code into global.asa but it only works when entering the domain - if someone enters via a bookmarked / google indexed link it doesn't work.

[codebox]Sub Session_OnStart
dim safeURL

'***** ONLY CHANGE THE LINE BELOW *****
safeURL = "rockaroundtheblock.com.au" 'domain name
'***** ONLY CHANGE THE LINE ABOVE *****

dim requestedURL
dim requestedWithPre
dim pre
dim sendTo

pre = "www."

requestedURL = LCase(Request.ServerVariables("SERVER_NAME"))

If InStr(1, requestedURL, "www.", 1) = 1 Then requestedWithPre = True

If (requestedURL <> safeURL) AND (requestedURL <> pre & safeURL) Then

sendTo = "http://"

If requestedWithPre Then
sendTo = sendTo & pre & safeURL
Else
sendTo = sendTo & safeURL
End If

Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", sendTo

End If
End Sub
[/codebox]

My host webcentral has provided this code but I can't get it to work
[codebox]
ASP Driven Multiple Domain Redirection
Web sites are typically attached to an IP address, but sometimes several domain names may point to the same IP. A clever ASP script could display the same page different ways, or one of several different pages, depending on which domain name was typed utilizing the HTTP_HOST server variable. Our site, for example has four domain names tied to the same IP ( domain1.com, domain2.com domain3.com and domain4.com ) and the following script will provide different results depending on what domain name it is called from:

<%response.buffer=true%>
<%
Dim theSRV
theSRV = Request.ServerVariables("SERVER_NAME")
Select Case theSRV
Case "www.domain1.com" Response.Redirect("/page1.htm")
Case "www.domain2.com" Response.Redirect("/page2.htm")
Case "www.domain3.com" Response.Redirect("/directory1")
Case "www.domain4.com" Response.Redirect("/directory2")
End Select
%>

<HTML>

<BODY>

To alter this code, simply replace the sections currently highlighted in blue, with your own domain names and your own directories.

This will mean that when a user enters www.domain1.com they will see www.domain1.com/page1.htm; www.domain3.com will return www.domain3.com/directory1 etc, making the redirection process appear just a little more seamless. You could replace the values with anything you would like, relative path's, as included in the example, or fully qualified remote paths to redirect your users to one of your sites on a completely different server.

The same Code Principles can be used for other variables also, such as redirecting token users based on there user name, or using the Response.Write function instead of Response.Redirect to simply generate a different page on the fly. [/codebox]

I've tried entering the "Case" line as
Case "www.ratb.com.au" Response.Status = "301 Moved Permanently" Response.addheader "Location", "http://www.rockaroundtheblock.com.au/" Response.End

but that doesn't work either!

What I really want to figure out is how to redirect a page by switching out ratb and replacing it with rockaroundtheblock



#307 monstergamez

monstergamez

    HR 1

  • New Member
  • Pip
  • 1 posts

Posted 15 April 2007 - 08:43 AM

Hi, iv been trying for a while now.

I have a site: example.net

HTA Acess File Contents:
CODE
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.net [NC]
RewriteRule ^(.*)$ http://www.example.net/$1 [L,R=301]

RewriteEngine on
RewriteRule ^(.*)\.html$ index.php?params=$1 [L]

Redirect 301 http://example.net http://www.example.net



I just found that www.example.net/index.php has Google PR3

I need to redirect that to www.example.net (PR4)

Could someone help me out, PM me please,

Thanks Alot,
Luke.


#308 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 15 April 2007 - 12:05 PM

Welcome Monstergamez ! hi.gif

You'll need to use THE_REQUEST to narrow things down and keep from creating an endless loop.

See this thread for the syntax and some ideas to get ya started.

Also, you don't need multiple RewriteEngine on instructions in your .htaccess. It only needs to be there once.

#309 Andy_Seo

Andy_Seo

    HR 4

  • Active Members
  • PipPipPipPip
  • 237 posts
  • Location:Welwyn Garden City

Posted 03 May 2007 - 03:47 AM

Hi,

I've used a number of re-direct methods on here and I keep getting the following message:

QUOTE
Internal Server Error, this is an error with your script, check your error log for more information.


I have put a .php re-direct on but this should only be temporary as every other file doesn't re-direct to the new site and I need that to happen. The server is Apache - does anyone have any suggestions to how I can easily re-direct every page?

Edit: For any code that I drop into the .htaccess file it returns an 'internal server error'! Any idea's why and how I can over come this? girl_cray2.gif

Edited by Andy_Seo, 03 May 2007 - 04:37 AM.


#310 chrishirst

chrishirst

    A not so moderate moderator.

  • Moderator
  • 6,725 posts
  • Location:Blackpool UK

Posted 03 May 2007 - 07:36 AM

QUOTE
Any idea's why and how I can over come this


Correcting the code that is causing the error would be best.

#311 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 03 May 2007 - 07:47 AM

Post or PM the .htaccess code you're trying to use Andy. 99.9% of the time it's some small syntax error, so we should be able to sort it out fairly quickly.

If I understand you correctly that you're trying to redirect every page of an old domain that's being retired over to a different/new domain, it would look like the following if you can maintain page names:

CODE
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]


If you can't maintain the file naming structure from site to site you'll probably want to simply redirect everything to the home page of the new domain. An example of that would be:

CODE
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/ [R=301,L]



#312 Andy_Seo

Andy_Seo

    HR 4

  • Active Members
  • PipPipPipPip
  • 237 posts
  • Location:Welwyn Garden City

Posted 03 May 2007 - 08:56 AM

Thanks Randy.

However, I doubt I can do anything at the moment as I rang the host provider and he explained that the server is a shared server and therefore can't enable the mod re-write.

I'm trying to get the site moved to a dedicated server rather than a shared one so that the .htaccess file is acceptable.

Does this all make sense?

#313 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 04 May 2007 - 06:53 AM

Yup. If the host has mod_rewrite disabled it would generate the internal server error if you tried to use it.

Personally, I've never had an issue with enabling mod_rewrite on a shared hosting environment, but I have a pretty solid jail system in place that keeps everyone in their own area. Without that I wouldn't want to have mod_rewrite enabled either because of possible security implications.

#314 Andy_Seo

Andy_Seo

    HR 4

  • Active Members
  • PipPipPipPip
  • 237 posts
  • Location:Welwyn Garden City

Posted 08 May 2007 - 04:42 AM

QUOTE(Randy @ May 4 2007, 12:53 PM) View Post
Yup. If the host has mod_rewrite disabled it would generate the internal server error if you tried to use it.

Personally, I've never had an issue with enabling mod_rewrite on a shared hosting environment, but I have a pretty solid jail system in place that keeps everyone in their own area. Without that I wouldn't want to have mod_rewrite enabled either because of possible security implications.


Thanks for the info. I had requested it but they declined it. Is the only other option to move it to a dedicated server?

Edit: Fantastic! It all works. Randy, as always mate, great work! Thanks again. appl.gif

Edited by Andy_Seo, 08 May 2007 - 08:50 AM.


#315 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 08 May 2007 - 08:27 AM

Depends Andy. There's nothing else you can do at the server level that I'm aware of. mod_alias doesn't support anything like a RewriteCond.

Is the site Scripted? (eg written in PHP or ASP, etc) You indicate above that it's a php site.

If so, you could add a simple check in at the File level to trigger the redirect. As an example, let's say you wanted to institute a 301 redirect for the www/non-www issue but didn't have mod_rewrite available. You could stick something like the code below on the server then call it as a PHP Include at the very top of each page (before any html or doctype declarations) and let PHP do the work for you. It's a quick and dirty example, but I've commented the heck out of it so that you can tell what each line or section is doing.

CODE
<?php
/* Scripted www/non-www redirect in PHP when mod_rewrite is not available */
// A variable that is your domain name with the www
// Do Not Include A Trailing Slash Here!
$wwwAddr = "http://www.yourdomain.com";
// Will return a string like yourdomain.com or www.yourdomain.com
$getAddr = $_SERVER['SERVER_NAME'];
// Will return the page name and path from root. eg /index.php
// Note: It will automatically include a leading slash
$getPage = $_SERVER['PHP_SELF'];
// Will variablize any and all query strings in the requested URL
// Doesn't include the leading ampersand
$getQuery = $_SERVER['QUERY_STRING'];

// If the requested url address doesn't begin with www.yourdomain.com
// None of the below will fire if the requested address is in www.yourdomain.com format
if($getAddr != "www.yourdomain.com") {
// First set the 301 header since any redirect will be 301'd
    header("HTTP/1.1 301 Moved Permanently");
// Redirect when a Query String is present  This assumes you have normal looking dynamic URLs
// eg somepage.php?var1=Var1 and not /?var1=Var1
    if(isset($getquery)) {
        $getQuery = "&" . $getQuery;
        $urlString = $wwwAddr . $getPage . $getQuery;
        header("Location: " . $urlString);
// Redirect if no Query String is present, but when a page or filename is present.
// Note we're excluding directly level requests where PHP will pick up
// the /index.php even if it wasn't in the original request.
// This assumes a normal setup where index.php is the default directory level file.
    }elseif(isset($getPage) && $getPage != "/index.php") {
        $urlString = $wwwAddr . $getPage;
        header("Location: " . $urlString);
// Redirect if there is no Query String or non-index page name in the request
// We're adding the trailing slash here if necessary to simplify things above
    }elseif(isset($getAddr)) {
        $urlString = $wwwAddr . "/";
        header("Location: " . $urlString);
    }else{
// Exit out of the process as a safety precaution
    exit();
    }
}
?>


Hope that helps!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

SPAM FREE FORUM!
 
If you are just registering to spam,
don't bother. You will be wasting your
time as your spam will never see the
light of day!