homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

How to redirect index.php - 301 compliant?

 11:27 pm on Jul 1, 2007 (gmt 0)


I found this piece of code, posted a couple of years ago, elsewhere on webmasterworld.com. It redirects index.php to the homepage without sending everything into an infinite loop if you have DirectoryIndex setup in .htaccess:

The problem with this is that any request for "/" is internally rewritten to index.php by Apache mod_dir if you have index.php in the DirectoryIndex list (the usual set-up), so it's difficult to avoid an infinite loop.

It can be done with mod_rewrite in .htaccess like this

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

Firstly, can anyone confirm if this is 301 compliant and/or will not be penalised by Google? I gather that's the R=301 bit?

Secondly, I have a dynamic search page on my site that was somehow picked up by Google. Using .htaccess, what would I insert to redirect /search.php%3Ggo=adv to the homepage?


[edited by: glenster73 at 11:39 pm (utc) on July 1, 2007]



 11:38 pm on Jul 1, 2007 (gmt 0)

What were the results when you tested it?

The "Live HTTP Headers" extension for Firefox/Mozilla browsers will answer your question quite unequivocally, without your having to take anyone's word for it.

It's a great tool, and I recommend it.



 11:40 pm on Jul 1, 2007 (gmt 0)

index.php redirected correctly to the homepage. I'd just like to know if this is the safe way to do things.


 12:03 am on Jul 2, 2007 (gmt 0)

OK. Yes it is.

But without testing with a server header checker [livehttpheaders.mozdev.org], you're just taking "some guy on a Web site's word for it"...



 12:26 am on Jul 2, 2007 (gmt 0)

i've got this piece of php code which does the same thing - redirects example.com/index.php to example.com/
just stick it at the top of your page

function index_url(){
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$captures[1]);


 2:42 am on Jul 2, 2007 (gmt 0)

Thanks for the tips guys.

Just in response to my second query:

> Using .htaccess, what would I insert to redirect /search.php%3Ggo=adv to the homepage?

This one is unusual in that obviously the % would normally be a?, so I am not sure where Google got that string from.

Changing?strings can be done with the following example, but I can't get it to accept a % instead of a?

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^productid=100001&cat=&page=1$
RewriteRule ^mydir/customer/product\.php$ http://www.example.com/mydir/customer/product.php?productid=100002&cat=&page=1 [R=301,L]

Anyone with any clues on what I could try?


[edited by: jdMorgan at 6:14 pm (utc) on July 2, 2007]
[edit reason] example.com [/edit]


 6:25 pm on Jul 2, 2007 (gmt 0)

Using .htaccess, what would I insert to redirect /search.php%3Ggo=adv to the homepage?

This one is unusual in that obviously the % would normally be a?, so I am not sure where Google got that string from.

It's even more unusual in that a question mark would normally be hex-encoded for transmission as "%3F", not "%3G", so there is a fairly big/serious bug somewhere in the code that generated that link.

Because the server uses "?" to distinguish between the end of the URL and the beginning of the query string, and because the "?" is not present --having been replaced by a badly-coded hex-escape routine with "%3G"-- you won't be able to use the normal RewriteCond %{QUERY_STRING} solution. Instead, try %{THE_REQUEST} -- a variable which contains the raw (un-decoded) client request string:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /search\.php\%3Ggo=adv\ HTTP/
RewriteRule ^search\.php$ http://www.example.com/? [R=301,L]



 11:15 pm on Jul 2, 2007 (gmt 0)

That should read "search\.php\%3Fgo=adv"

Sorry me bad. Seems my typing skills have a bug in them!

In this case, do you know what the rewrite look like when the "?" is being converted to %3F? I tried both of the above without success..



 12:03 am on Jul 3, 2007 (gmt 0)

Actually, this gets a little more complicated.

Although I want to rid the search engines of "search.php%3Fgo=adv" and redirect it to the homepage, I actually have a valid page on my site named "search.php?go=adv" that I want to keep. Would any manipulation of %3F also cause the real page to redirect also?

So, should I redirect the %3F version to the? version of the same page? If so, is there a correct syntax for doing that?


 12:27 am on Jul 3, 2007 (gmt 0)

# Redirect to fix invalid query string delimiter on any php page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^.]+\.php\%3[Ff]([^\ ]*)\ HTTP/
RewriteRule ^([^.]+)\.php$ http://www.example.com/$1.php?%1 [R=301,L]

This rule will correct the "%3F" problem for any query string appended to any php page URL. It will take no action if the query string delimiter is valid.



 1:06 am on Jul 3, 2007 (gmt 0)

Worked Jim!

Well, sort of. The only thing I changed was the $1.php?%1 at the end of the domain. Your initial code caused /search.php%3Fgo=adv to redirect to /index.php?go=adv so I removed $1.php?%1 and it now redirects to the homepage. I don't think it affects anything else..


 1:29 am on Jul 3, 2007 (gmt 0)

> Your initial code caused /search.php%3Fgo=adv to redirect to /index.php?go=adv

No... the code I posted didn't do that, since the "page name" is clearly copied directly from whatever page was requested. However, if you placed this code *after* another RewriteRule or used some other mod_alias Redirect or RedirectMatch directive, it is possible that the two interfered with each other. Standing alone, the rule will work exactly as described, changing nothing in the URL-path but the "%3F" to "?".

I recommend you resolve this "mystery" because it threatens other unexplained problems down the road...



 1:41 am on Jul 3, 2007 (gmt 0)

Yep, there's a whole heap of other stuff going on in my .htaccess so I will take a good hard look at that shortly.

Thanks for the heads-up Jim.

Just one other very strange URL I noticed in Google. Looks like it picked up an old cookie page that is no longer valid:


How would one convert that to just the homepage?



 1:50 am on Jul 4, 2007 (gmt 0)

My goal here is to help you learn to write your code, and not to write it for you. I simply can't afford to spend more than a few minutes a day here as a volunteer, and must allocate my time among many requests. And the same is true for the other contributors to this forum.

So I would encourage (and ask) you to take the code above as an example, and try coding it yourself.

For background information, please see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Give it a shot, test it, and if you have problems, then please feel free to post back here.



 11:33 pm on Jul 4, 2007 (gmt 0)

cheers, I'll give it a go. I wasn't aware of the WebmasterWorld library - great resource!

thanks for your efforts throughout the forums mate!


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved