CodeCharge Studio
search Register Login  

Visual PHP Web Development

Visually Create Internationalized Web Applications, Web Reports, Calendars, and more.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 How to change redirect from 302-temporarily to 301-permanently?

Print topic Send  topic

Author Message
Aleister

Posts: 73
Posted: 12/15/2009, 11:42 AM

Hello,

Is there an easy way to change the 302 redirect (temporarily) that is made when $Redirect is set to a 301 (permanently)?

Thank you
View profile  Send private message
damian

Posts: 838
Posted: 12/15/2009, 12:34 PM

can you give an example of what you are doing?

_________________
if you found this post useful take the time to help someone else.... :)
View profile  Send private message
Aleister

Posts: 73
Posted: 12/15/2009, 12:48 PM

Hello damian,

Sure, in a page for example I just redirect the user to the "correct" url:

// Before show page
global $Redirect;
$Redirect=WhereShouldIGo();
// End

When this page is loaded the user is sent to the correct URL but with a 302 redirect:
"The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests"

Sometimes I want him to be redirected with 301 redirect:
"The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs"
View profile  Send private message
Aleister

Posts: 73
Posted: 12/15/2009, 1:25 PM

I fount that the redirection is made in a page's code and not in a common file:

if($Redirect)
{
$CCSEventResult = CCGetEvent($CCSEvents, "BeforeUnload", $MainPage);
header("Location: " . $Redirect);
$inc_header->Class_Terminate();
unset($inc_header);
$inc_footer->Class_Terminate();
unset($inc_footer);
$inc_content->Class_Terminate();
unset($inc_content);
$inc_metatags->Class_Terminate();
unset($inc_metatags);
unset($Tpl);
exit;
}

It is done by a simple
header("Location: " . $Redirect);

So if I want to do a 301 redirect I need to write my own function for redirection.

Now, another question, in which event it is best to put the check if this page is going to be "executed" or redirect (using my routine) to another page? Before Initialize?
View profile  Send private message
datadoit
Posted: 12/15/2009, 3:01 PM

BeforeInitialize or BeforeShow will work.
damian

Posts: 838
Posted: 12/15/2009, 3:04 PM

this redirect function is just using the php redirect so you might need to do it anoother way if the message/method isnt working for you...
why are you redirecting?
why not change the originating link?

_________________
if you found this post useful take the time to help someone else.... :)
View profile  Send private message
Aleister

Posts: 73
Posted: 12/16/2009, 3:35 AM

I am using pages as view templates and every working "module" is in includable pages.

content.php?id=30 means: show me the content no 30 using "content.php" template. gallery.php?id=10 means: show me gallery no 10. Now gallery.php for example is just a container which loads and displays in a specific way inc_header, inc_footer, inc_gallery, inc_gallery_banners, inc_gallery_votes etc

Any record of any type (content, gallery, articles, products e.t.c.) can be "home page" but a visitor just knows www.domain.com and server knows that index.php must be served. So index.php redirects to the correct template and record number.
View profile  Send private message
Aleister

Posts: 73
Posted: 12/16/2009, 5:00 AM

@datadoit
I currently use BeforeShow event but I am wondering if using BeforeInitialize is "better", meaning that less code is going to be executed.

Thank you both for your answers
View profile  Send private message
datadoit
Posted: 12/16/2009, 5:55 AM

There won't be any measurable difference. The entire events file is
loaded into memory, so no savings there. If there is a lot of variable
or memory block assignments taking place in BeforeShow, then perhaps
some resources could be spared by having your redirect in BeforeInitialize.

If you don't know it already, make certain that you include an 'exit'
after your header() function call. If you don't, then code will
continue to execute until a flush is encountered.

As an internal rule among several developers, I like to keep redirects
based on page entry rules in BeforeInitialize. Helps later when
debugging. We reserve BeforeShow for things like dynamic visibility
rules for components on the page itself. ie: You've made it into the
page, now here's what you get to see.
Aleister

Posts: 73
Posted: 12/16/2009, 1:44 PM

Yes, I found out in the past about the need of exit; after header() :p

I am going to think about changing event to BeforeInitialize because -you are right- it might be better for organization reasons.

Thank you
View profile  Send private message
Benjamin Krajmalnik
Posted: 12/16/2009, 4:51 PM

If you run Apache, then use mod_rewrite rules.

Aleister

Posts: 73
Posted: 12/16/2009, 6:57 PM

In order to know where the redirection must point I need to run a php script which needs to connect to mysql and get "template" name and record id and then redirect.

I don't know much about mod_rewrite but from what I understand I cannot define a rule for apache when redirection -from the same page- is made today to a certain page and tomorrow to another entirely different page without being guessable.

I will be happy to be wrong because then my research will be easier in another task: I just started seeking a way to rewrite urls in a way that the final user selects how the page name will be named. Meaning that, for example, user wants domain.com/article.php?id=1 to be reffered as domain.com/read-all-about-us.html and domain.com/article.php?id=2 to be reffered as domain.com/something/something_else/what-a-nice-view.html
View profile  Send private message
quasimidi


Posts: 151
Posted: 12/16/2009, 10:59 PM

The cleanest way to use mod_rewrite (.htaccess) like it mentioned before.

Here is a htaccess generator for this (and other) purposes:

http://www.generateit.net/error-pages/

Also, there is a SEF url generator for your needs:

http://www.generateit.net/mod-rewrite/

_________________
RS
View profile  Send private message
Aleister

Posts: 73
Posted: 12/17/2009, 1:17 PM

@quasimidi
The links you provided rocks...

I continue to find redirection the simplest way (I am newbie in apache directives) but from your links I very easily found the answer to my second question, how to make custom friendly urls. Thank you.

When you say to use mod_rewrite instead of header() redirection, how do you mean to do it? Landing page can change every day, how does the .htaccess know what is the new redirection?
View profile  Send private message
Benjamin Krajmalnik
Posted: 12/18/2009, 9:58 AM

What we do is haev a background script which generates the .htaccess
redirections.
It can be scheduled to run, or can be run whenever a change to an affected
page takes place.

"Aleister" <Aleister@forum.codecharge> wrote in message
news:54b299e27859c1@news.codecharge.com...
> In order to know where the redirection must point I need to run a php
> script
> which needs to connect to mysql and get "template" name and record id and
> then
> redirect.
>
> I don't know much about mod_rewrite but from what I understand I cannot
> define
> a rule for apache when redirection -from the same page- is made today to a
> certain page and tomorrow to another entirely different page without being
> guessable.
>
> I will be happy to be wrong because then my research will be easier in
> another
> task: I just started seeking a way to rewrite urls in a way that the final
> user
> selects how the page name will be named. Meaning that, for example, user
> wants
> domain.com/article.php?id=1 to be reffered as
> domain.com/read-all-about-us.html
> and domain.com/article.php?id=2 to be reffered as
> domain.com/something/something_else/what-a-nice-view.html
> ---------------------------------------
> Sent from YesSoftware forum
> http://forums.yessoftware.com/
>

datadoit
Posted: 12/20/2009, 2:56 PM

Definitely mod_rewrite would be the way to go longterm. Makes things
quicker and plays nicely on the internet.

Sounds like since your alternative addresses are in a MySQL table, then
perhaps creating a CodeCharge routine that will load and create your
htaccess file(s) for you is in order.
Aleister

Posts: 73
Posted: 12/21/2009, 5:16 AM

@Benjamin Krajmalnik, @datadoit

Thank you both, I will experiment with mod_rewrite and dynamically creating and modifying .htaccess.

The only think that "scares" me is the possibility of something going wrong and server does not finish the modification of .htaccess as this could lead in an empty .htaccess and the reveal of files and directories that now I deny access from within .htaccess.
View profile  Send private message
datadoit
Posted: 12/21/2009, 6:38 AM

Take a peak under the hood at WordPress (www.wordpress.org) and see how
it deals with what they call PermaLinks. It's URL management without
the use of mod_rewrite, which keeps the application cross-platform
compatible (because M$ IIS can only dream of being as slick and secure
as Apache!).
datadoit
Posted: 12/21/2009, 6:47 AM

Mo info: http://codex.wordpress.org/Using_Permalinks

See the PATHINFO section.
Aleister

Posts: 73
Posted: 12/22/2009, 6:14 AM

Thank you datadoit, wordpress implementation looks very interesting from what I saw in the link you provided. I will definitely try their way.
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.