Forum Moderators: phranque
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?
cheers,
G.
[edited by: glenster73 at 11:39 pm (utc) on July 1, 2007]
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"...
Jim
function index_url(){
if(preg_match('#(.*)index\.(html¦php)$#',$_SERVER['REQUEST_URI'],$captures)){
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$captures[1]);
};}
index_url();
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?
cheers,
G
[edited by: jdMorgan at 6:14 pm (utc) on July 2, 2007]
[edit reason] example.com [/edit]
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]
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?
# 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]
Jim
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...
Jim
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:
www.example.com/?PHPSESSID=f9f2770d591366bc
How would one convert that to just the homepage?
cheers,
G.
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.
Thanks,
Jim