Forum Moderators: phranque

Message Too Old, No Replies

301 Redirects - htaccess vs php redirects

Do they do the same functions?

         

Evolve123

3:09 pm on Aug 20, 2008 (gmt 0)

10+ Year Member



Hello,

I'm in a bit of a dilemma here. I'm having trouble redirecting my dynamic url through htaccess, but I am able to do it using PHP.

Now the question is: does a htaccess 301 redirect do the same funtion as a PHP 301 redirect? Does it keep all ranks in the search engines, and are the search engines able to read the 301 redirect and eventually change the links just like any other htaccess redirect would do?

I see many people opt to using a htaccess 301 redirect rather than a php/python or any other 301 redirect that's available. Why is that? I know that when any visitor (being a bot or person) visits a website, the server always requests the .htaccess, is this the reason why? Wouldn't the server request the PHP redirect when a bot or person vists the website, and have the same results as the htaccess redirect?

PHP 301 redirect I'm using:

<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: [domain.com"...] );
?>

Thanks for your help in this matter.

coopster

3:24 pm on Aug 20, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Evolve123.

If Apache can handle the redirect without having to pass the process further on down the line you are "saving" resource by not asking your server to continue processing the request, invoke the PHP module for processing, etc.

Yes, .htaccess files are read for every directory they reside in, for every request. So, if the process can be handled there, great. If not, let your script do the work.

Evolve123

3:36 pm on Aug 20, 2008 (gmt 0)

10+ Year Member



Thanks for your reply coopster! Here is the redirect I'm using within htaccess, and it results in a loop.

I want to be able to redirect:

www.domain.com/index.php?network=something

to

www.domain.com/something

========================================

Here is what I used in htaccess:

RewriteEngine on

RewriteCond %{QUERY_STRING} ^network=something$
RewriteRule ^index\.php$ /something [L]

I get this error:

The server is redirecting the request for this address in a way that will never complete.

.. how can I redirect using htaccess?

coopster

3:50 pm on Aug 20, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Looks to me like you are going backwards in your thought process here, but I could be wrong. See the thread titled Changing Dynamic URLs to Static URLs [webmasterworld.com] in our Apache Forum Library for a more-detailed description of this whole process.

Evolve123

4:12 pm on Aug 20, 2008 (gmt 0)

10+ Year Member



I read that :).

Here is what I did:

RewriteRule ^([^/]+)/?$ /index.php?network=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?network=([^&]+)\ HTTP/
RewriteRule ^index\.php$ [domain.com...] [R=301,L]

I get a Internal Server Error using the above. Do you, or anyone else know how I can do this redirect properly with htaccess?

Once again, I want to be able to redirect:

www.domain.com/index.php?network=something
to
www.domain.com/something

coopster

4:30 pm on Aug 20, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



So, the link in your HTML document is

<a href="http://www.example.com/index.php?network=something">My link</a>

... and that is what you want to show in the address bar of the web browser?

Evolve123

4:38 pm on Aug 20, 2008 (gmt 0)

10+ Year Member



What's this HTML Document thing your talking about? I have a dynamic link - www.example.com/index.php?network=something and through htaccess I want to be able to redirect that url to www.example.com/something

coopster

4:40 pm on Aug 20, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



How does one get to the resource designated by that dynamic link? Do they just randomly decide they are going to visit your site page and make up an address to type into their browser?

g1smd

5:27 pm on Aug 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** I want to be able to redirect: ***

Err. A redirect should include the target domain name and [R=301].

Your second code has neither and therefore was for a rewrite, not a redirect.

.

In your third example, you have coded it with rewrites first and redirects last. That will expose internal filepaths to the outside world.

You need to place all the redirects before all of the rewrites in your code.

.

*** I want to be able to redirect: ***

*** www.domain.com/index.php?network=something ***

As coded it will only work for a URL like:

www.domain.com/index.php?network=something&

.

You should omit "index.php" from URLs.

This URL:

www.domain.com/?network=something&

is for the same resource, but is a Duplicate.

Evolve123

5:50 pm on Aug 20, 2008 (gmt 0)

10+ Year Member



So, it is coded as www.domain.com/index.php?network=something&
what would be the 301 redirect I would put into the htaccess for www.domain.com/index.php?network=something& to go to www.domain.com/something

jdMorgan

6:07 pm on Aug 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This code is correct for the question as asked.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?network=([^&\ ]+)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/%1? [R=301,L]

You will need "RewriteEngine on" and possibly also "Options +FollowSymLinks" ahead of your list of mod_rewrite rules, though.

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?network=([^&\ ]+)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/%1? [R=301,L]

The Options directive may be needed, or it might not be allowed -- You have to test and find out.

Examine your server error log when you get a server error - It will ofthen tell you exactly what is wrong.

Jim

[edited by: jdMorgan at 6:07 pm (utc) on Aug. 20, 2008]