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

#121 Raphael

Raphael

    The Limey Cowboy

  • Active Members
  • PipPipPipPipPipPip
  • 722 posts
  • Location:New England

Posted 31 March 2005 - 10:32 AM

pps: For the above problem (where I have to have all of my page requests come via the exact same host for session cookies to stick around after the transfer to https://) I solved it like this.

(I don't know if anyone has posted this before, but if not, maybe it will be helpful for anyone else who's looking for a code solution to this problem because their hosting company won't change anything on the server for them)

CODE
<?php
whatTheyTyped = $_SERVER['HTTP_HOST'];
$pageTheyWant = $_SERVER['REQUEST_URI'];
if ($whatTheyTyped == "mysite.com") {
    if (isset($pageTheyWant) && $pageTheyWant != "" && $pageTheyWant != "/" && $pageTheyWant != "//") {
 $urlTheyActuallyWant = "http://www.mysite.com/$pageTheyWant";
    } else {
 $urlTheyActuallyWant = "http://www.mysite.com/";
    }
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: $urlTheyActuallyWant");
    exit();
}
?>


I had to put that second if statement in because I discovered that for some reason, the REQUEST_URI was occasionally coming back with the string "//" and so when I typed in:

mysite.com or mysite.com/

It would occasionally come back with www.mysite.com// which I didn't want.

Anyway, there it is in PHP. It just lives at the top of a file that gets included in every page and gets executed right before session_start() Maybe someone will find it useful - I don't know ASP, so someone else will have to provide a version of it in that wink.gif

#122 tempy

tempy

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 344 posts

Posted 19 April 2005 - 01:08 AM

Hi

Please feel free to avoid the first paragraph of this, if you aren't interested in the background...

I have mod_rewritten some dynamic url pages, so that I can give static looking links to a few database welcome/subsection pages. So far, so good. However, within the DB script that I am using, the dynamically created pages still produce links back to these pages in their (pre-rewritten) dynamic form. To avoid duplicated content issues I have decided that the simplest solution (it's a third part script) is simply to 301 redirect the dynamic urls (5 or 6 of them).

I'm having the biggest ranting.gif of a eek.gif of a bleep.gif trying to get this internal 301 to work.

The url(s) I am trying to redirect are in the following format;

CODE
http://www.mydomainname.com/directory/index.php?a=list&d=7


I want this to redirect to:

CODE
http://www.mydomainname.com/directorylist-7.htm
Which is the rewritten url and works fine when accessed directly.

However, it just won't work.

I have tried the following;

CODE
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomainname.com/directory/index.php?a=list&d=7$
RewriteRule ^(.*)$ http://www.mydomainname.com/directorylist-7.htm$1 [R=301,L]


And various permutations of the above (with the www, without the www and so on..... but I don't think that's the problem).

I have also tried;

CODE
Redirect 301 /directory/index.php?a=list&d=7 http://www. mydomainname.com/directorylist-7.htm


I also tried making a permanent redirect via CPanel, but when I look at the result in .htaccess, it has truncated the original url to something like
CODE
mydomainname.com/directory/index.php
. I.E. It ignores everything after the '?'. So everything goes into a loop of some sort, when the url is accessed.

Is anyone able to understand a single word that I have just typed and willing to make any suggestions?

Thanks.
eek.gif

#123 leadegroot

leadegroot

    Lea de Groot

  • Active Members
  • PipPipPipPipPip
  • 488 posts
  • Location:Brisbane, Australia

Posted 19 April 2005 - 06:08 AM

CODE
RewriteCond %{HTTP_HOST} ^mydomainname.com/directory/index.php?a=list&d=7$
RewriteRule ^(.*)$ http://www.mydomainname.com/directorylist-7.htm$1 [R=301,L]

Its been far too long a day for me to completely untangle this for you, but I can point out some things:
One reason this wont fire is because HTTP_HOST only equals mydomainname.com so you will always return false if you compare it to a longer string. If my tired brain remembers correctly, you dont want to rewrite to a fully qualified address anyway.
How about:
CODE
RewriteRule  ^directory/index.php?a=list&d=7$ /directorylist-7.htm [R=301,L]

I don't think you need a rewritecond in this case at all.
But its late sad.gif

#124 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 19 April 2005 - 06:44 AM

Not sure I'm completely understanding the problem, but I'll give it a whirl. Let's see if this is correct...

1.) Your rewrite is working as expected.
2.) The cart/cms/whatever is still pointing to the dynamic URLs and you would like to change that so that they too point to the rewritten version.

The problem with setting up a 301 for those dynamic URLs is that they still need to be valid for the rewrite to work. If they're not, ie if you're redirecting those, you're basically creating a never ending loop.

It goes something like this:

Someone tries to go to the dynamic URL and get redirected to the rewritten URL, which calls the info from the dynamic URL, which redirects to the rewritten URL, which calls the info from the dynamic URL, which...

You get the picture.

Basically, the best way to do this is to change how your cart/cms/whatever writes the URLs that it produces. That way everybody, humans and spiders alike, are using the same URLs. Then exclude the dynamic URLs that are being rewritten from your robots.txt file.

If that cannot be accomplished for whatever reason, then you'll be left with providing the rewritten URLs as you can for the spiders to crawl, and exclude the dynamic URLs via robots.txt. This isn't the best solution because you're going to lose some link pop from other pages of your site that point to the dynamic URLs, but it is an option.

Lea is correct that you'll want to remove the domain portion from the rewrite, as that can cause some very wonky, and completely unintentional, results.

A slightly off topic question on the rewrite for you tempy...

Does the "7" in your above example change? But everything else remains the same? And is the "7" always a number?

If so, I would use a rewrite rule that looks something like:
CODE
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^directorylist-([0-9]*).htm$ /directory/index.php?a=list&d=$1


It's too early here for me to be tinkering with regex, but I think that should work and allow the d= number to change for you and still end up at the correct page.

#125 Raphael

Raphael

    The Limey Cowboy

  • Active Members
  • PipPipPipPipPipPip
  • 722 posts
  • Location:New England

Posted 19 April 2005 - 09:24 AM

Probably not much help to you, but I'd do it in PHP wink.gif (mostly because I don't understand how mod_rewrite works =)

#126 tempy

tempy

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 344 posts

Posted 19 April 2005 - 09:17 PM

Hi folks.

Thanks for the replies. I'll try and be a bit clearer - these late night sessions are not good for me.

The issue is that a glossary I am setting up is a PHP/mySQL thingy and there is no built in method of sorting out the URLs - it's called 'Glossword', if anyone's interested. And, I know as much about PHP as I do about brain surgery. Anyway...

OK. The result I want is this;

I have accepted that I can't affect all of the url's that this program puts out (an endless stream of difficult to spider ?=?=?=?=?=?=?=?=). So, what I am doing is 'helping' the engines by making the 6 directory subject pages more 'url friendly', in the hope that the links from these 5 pages will encourage the spiders to delve a bit deeper.

The 6 urls are all in the format as follows;

sub-topic 1 - http://www.domain.co....php?a=list&d=5

sub-topic 2 - http://www.domain.co....php?a=list&d=3

...and so on until number 6.

I have created a rewrite that works fine. The .htaccess file looks like this;

CODE
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

RewriteRule glossary(.*)-(.*)\.htm$ /directory/index.php?a=$1&d=$2


This changes all of the above dynamic looking urls, into;
http://www.domain.co...ctorylist-5.htm
http://www.domain.co...ctorylist-3.htm

Etc.... So far so good.

My problem now, is that, further inside the glossary application, there are links back to these 'sub-topic' pages. These links are still in the original dynamic form (obviously because that is how the program is creating them). What I now want to do is 301 those dynamic urls to point at the new urls. But I just can't get this to work.

BTW, I didn't do this rewrite off my own back. I used an automatic online tool. I know nothing about this from a technical point of view. I have looked at all of the suggestions above - but I'm sure I've completely misunderstood some of them sad.gif

I'll be honest, if anyone can provide me a quick cut and paste solution, that would be ideal. And I promise to re-read that page on Apache mod_redirect (maybe, one day) .

Also, does the .htaccess file need to be in the 'directory' directory? Or will everything work fine in the main .htaccess file in root (which is where I currently have it)?

Edited by tempy, 19 April 2005 - 09:46 PM.


#127 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 20 April 2005 - 06:42 AM

On the last question, yes the htaccess will work just fine from the root level. In fact, I usually suggest that to keep everything more sensible.

It's been awhile since I've done anything with Glossword. Ages in fact. I'll grab a fresh copy if I get a chance later today to see if I can figure out which files would need to be tweaked so that the lower level links point back up to your rewritten URL. There's gotta be a way to do that, and it would be the cleanest solution all the way around.

#128 tempy

tempy

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 344 posts

Posted 20 April 2005 - 08:08 PM

Thanks Randy, that would be brilliant. I've tried to struggle with it, but the documentation is not that clear - at least not to a numbnut like me. huh.gif

#129 Dwain Dibley

Dwain Dibley

    HR 3

  • Active Members
  • PipPipPip
  • 84 posts

Posted 25 April 2005 - 09:45 AM

301 check please.

Hi, hopefully this will be a quick yes thats correct Dwain crossfingers.gif

I use store.asp as my homepage and I have a simple redirect :

<% Response.Redirect "store.asp" %>

In my default.asp page, am I better off using a 301 redirect in my default.asp rather than the one I use above?

Thinking of using:

<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "store.asp"
response.end
%>

Thanks

DD

p.s. not my idea to use store.asp instead of default.asp...some developer in a small office somewhere thought it would be a good idea at the time!?

p.p.s unable to change the way this works, i.e. can't just replace default.asp to store.asp. Also can't do anything in IIS to say use store.asp as default page.

#130 chrishirst

chrishirst

    A not so moderate moderator.

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

Posted 25 April 2005 - 10:36 AM

some code for default.asp

<!--#include file="store.asp"-->

or

<%server.transfer("store.asp")%>

#131 Dwain Dibley

Dwain Dibley

    HR 3

  • Active Members
  • PipPipPip
  • 84 posts

Posted 25 April 2005 - 10:45 AM

<!--#include file="store.asp"--> seems a pretty cool idea, thanks chrishirst I'll try that and let you know.


DD

#132 tempy

tempy

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 344 posts

Posted 05 May 2005 - 05:43 AM

Hello again.

I've just re-read this whole thread because I'm going bananas over another issue.

I've always assumed that chucking the following into an .htaccess file will divert *all* pages from the old domain to the new domain;

Redirect 301 / http://www.newdomain.com

However, it is only diverting the top level.

Soooo, I added a few other redirect such as;

Redirect 301 /html/oldfile.html http://www.newdomain.com/newfile/php

Unfortunately the resulting redirect resulted in a 404 error, as follows; http://www.newdomain...ml/oldfile.html


I *know* that I have got this working before. Can someone please hammer me over the head with a virtual wet fish and show me the rror of my ways?

Thanks. wacko.gif

#133 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 05 May 2005 - 06:31 AM

Check your syntax Tempy. On the second example do you really want it to redirect to "newfile/php"?

As to the first one, unless your server has some sort of interesting setup it should redirect all pages to the corresponding page on the new site, but you have a tiny syntax error there. Basically you're missing the trailing slash on the redirect. It should look like this:

CODE
Redirect 301 / http://www.newdomain.com/


Here's what's happening in English...

Since you're currently redirecting to www.newdomain.com instead of www.newdomain.com/ when a page name is appended to the end of the URL it ends up trying to redirect to something like www.newdomain.compagename.php instead of www.newdomain.com/pagename.php

Hope that helps!

#134 tempy

tempy

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 344 posts

Posted 05 May 2005 - 06:40 AM

Thanks Randy, as ever.

Ah yes, my dodgy typing.

That's actually just a typing mistake in my post, rather than in the syntax in the .htaccess file- in both examples - I've just gone back to triple check.

What I'm trying to say is that - on my server, if not in my forum posts - I appear to be doing the right thing, but the redirect is going haywire.

Any ideas?

<--- edit --->

I even tried setting the redirects via the "Add Redirect" in CPanel, which writes to the .htaccess file, and the result is the same.

#135 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 05 May 2005 - 09:15 AM

Are you getting any type of error message tempy? Specifically the one I'm thinking about is a 501 Not Implemented, which is what you'll usually get if it's been disabled at the server.

Oddly, if you need to ask your host about it ask them if mod_alias is enabled, not mod_rewrite. Most think that Redirect directives are part of the mod_rewrite module, but they're actually part of mod_alias.

If it turns out that mod_alias is disabled, but mod_rewrite is enabled, there are other ways to accomplish what you're trying to do using mod_rewrite. But it's a bit more server intensive.




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!