Web Hosting Forum | Lunarpages


*
Welcome, Guest. Please login or register.
Did you miss your activation email?



Login with username, password and session length
February 09, 2012, 06:14:40 PM

Pages: [1]   Go Down
  Print  
Author Topic: .htaccess "/index.php" Redirect Help  (Read 2036 times)
duncan1on1
Newbie
*
Offline Offline

Posts: 4


« on: July 21, 2010, 01:05:51 AM »

Hi,

Would appreciate any help from those with more experience.

I'm helping my friend out with his site, hosted on Lunarpages (calling it site.com) and I noticed that his homepage is being redirected as http://www.site.com/index.php. I want to 301 redirect this to just http://www.site.com, however, my first attempt at a 301 just created an infinite loop (site.com/index.php redirecting to site.com, which calls site.com/index.php, which redirects, etc...)

I read in "The Art of SEO" that I can do a non-infinite-loop redirect using the .htaccess file, along with creating a 'dummy file' (calling it sitehome.php) and copying the contents of my index.php to sitehome.php. Next, I'm supposed to add this to my .htaccess

Code:
DirectoryIndex sitehome.php

Finally, I'm supposed to delete everything in my original index.php file add in this line:

Code:
<? header("Location: http://www.example.com"); ?>

However, I ended up with the following message:

Code:
Warning: Cannot modify header information - headers already sent by (output started at /home/user/public_html/index.php:1) in /home/user/public_html/index.php on line 1

(But unfortunately still with the URL: http://www.site.com/index.php)

Here's my existing htaccess file. Everything else, besides "DirectoryIndex sitehome.php" was already existing, though deleting everything and just leaving "DirectoryIndex sitehome.php" still gives me the above error.
Code:
AddDefaultCharset UTF-8
# BEGIN Mod_Rewrite
RewriteEngine On
RewriteCond %{http_host} ^site.com
RewriteRule ^(.*) http://www.site.com/$1 [R=301,L]
# END Mod_RewriteRewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ "http\:\/\/www\.site\.com\/index\.php" [R=301,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^info/index.html$ "http\:\/\/www\.site\.com\/index\.php" [R=301,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^referral/?$ "http\:\/\/www\.site\.com\/index\.php" [R=301,L]

DirectoryIndex sitehome.php

I would appreciate any advice/help on this!
Thanks,
   -Duncan
Logged
MrPhil
Berserker Poster
*****
Offline Offline

Posts: 5083



« Reply #1 on: July 21, 2010, 08:19:12 AM »

What a mess! Just what are you trying to accomplish here?

The first 3 rewrite lines are pretty straightforward:
Code:
RewriteEngine On
RewriteCond %{http_host} ^site.com
RewriteRule ^(.*) http://www.site.com/$1 [R=301,L]
It changes http://site.com/... to http://www.site.com/... so search engines only index one version.

I have seen problems, at least on LP servers, with firing off more than one 301 redirect at a time. Something just doesn't seem to work right, even though in theory it should be OK. So, if more than one of your 301 redirects fire off, that may cause trouble.

Now, just what are you trying to accomplish with the rest of this code? By default, the DirectoryIndex entry is index.html index.htm index.php home.html on LP servers. Given a directory name but no filename, the server will look for "index" files in that order.

Code:
RewriteRule ^/?$ "http\:\/\/www\.site\.com\/index\.php" [R=301,L]
is unnecessarily complicated. First, the Replacement String on the right is not a Regular Expression, and should not have all those escapes in it:
Code:
RewriteRule ^/?$ http://www.site.com/index.php [R=301,L]
What it's saying is "If the URI is empty or just a slash, send the request to index.php". That could be better done as a rewrite:
Code:
RewriteRule ^/?$ /index.php
But if you leave the DirectoryIndex alone, it will go to index.php anyway. (Note that it will go to the root's index.php, not the index.php in a directory.)

Code:
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^info/index.html$ "http\:\/\/www\.site\.com\/index\.php" [R=301,L]
again can be simplified:
Code:
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^info/index.html$ http://www.site.com/index.php [R=301,L]
and says "In all cases (any domain name) redirect info/index.html to /index.php". It should match only info/index.html, and the RewriteCond can probably be omitted. Do you really want this to be a 301 redirect? That tells search engines to remove any listing they have for info/index.html and replace it with index.php. If you're just doing some SEO, and want info/index.html to continue to be the public address, use
Code:
RewriteRule ^info/index.html$ /index.php
Again, note that it's going to your root index.php.

The same thing holds for the "referral/" entry.

Your last statement redefines DirectoryIndex to look just for sitehome.php -- it doesn't add that name to the list. What did you intend here? Do you want index.html, index.htm, and index.php to be ignored, and look only for sitehome.php if no filename is given?
Logged

duncan1on1
Newbie
*
Offline Offline

Posts: 4


« Reply #2 on: July 23, 2010, 05:19:40 AM »

Hi Mr.Phil,

Thanks for the reply-

Bottom line of what I'm trying to help my friend accomplish, is to redirect his site www.1on1mandarin.com/index.php to www.1on1mandarin.com. However, I was running into an infinite redirect loop, so I was trying to use the solution suggested in 'The Art of SEO'.

The solution given in book was to:

1. Copy the contents of index.php to another file. For this example, we’ll be using sitehome.php.
2. Create an Apache DirectoryIndex directive for your document root. Set it to sitehome.php.
Do not set the directive on a serverwide level; otherwise, it may cause problems with other
folders that still need to use index.php as a directory index.
3. Put this in an .htaccess file in your document root: DirectoryIndex sitehome.php. Or, if you
aren’t using per-directory context files, put this in your httpd.conf:
<Directory /your/document/root/examplesite.com/>
DirectoryIndex sitehome.php
</Directory>
4. Clear out the contents of your original index.php file. Insert this line of code:
<? header("Location: http://www.example.com"); ?>

I had taken a look at the existing .htaccess that was in my friend's website and saw that it was a crazy mess, but decided not to mess with it too much since I'm not too familiar with it, and just added 'Step 3: DirectoryIndex sitehome.php'.

So, in summary- I just wanted the URL to look like "http://www.1on1mandarin.com/"

Thanks again for the help,
  -Duncan
Logged
MrPhil
Berserker Poster
*****
Offline Offline

Posts: 5083



« Reply #3 on: July 23, 2010, 01:41:48 PM »

The default search order (DirectoryIndex) at LP is index.html, index.htm, index.php, and home.html. Does index.php no longer exist, and sitehome.php is the file to go to? Do search engines and users have "index.php" explicitly bookmarked/indexed? Or is there still an index.php on the site that you don't want to get rid of, but want to call sitehome.php ahead of it?

If incoming traffic is explicitly asking for index.php, and it should no longer be run, then either get rid of it or remove it from DirectoryIndex. Add sitehome.php to the DirectoryIndex in the desired order (priority). Then giving just the domain alone will take you to sitehome.php instead of the other index file(s). Then you should be able to intercept requests for index.php and redirect them to sitehome.php.

You can't just add random lines to .htaccess -- you have to understand what the current .htaccess is doing, and you have to know just what you're trying to accomplish. So,
1) are incoming requests for /index.php or just / ?
2) is there an index.php file that still can be run?
3) do you want to run sitehome.php when no file is explicitly given?
4) should incoming requests to run /index.php go to /sitehome.php instead?
5) anything else we should know about?

Throw away your copy of "The Art of SEO". It sounds like it's full of nonsense. There may be much easier and more straightforward ways to accomplish what you want. We need to know all the facts first.
Logged

duncan1on1
Newbie
*
Offline Offline

Posts: 4


« Reply #4 on: July 26, 2010, 04:36:28 AM »

Hi MrPhil,

Thanks again for the help.

Regarding your questions:
1) incoming requests are for just /, but users are being directed to /index.php
2) there is an index.php file that still can be run
3) i still want index.php to be run, when no file is explicitly given. The only reason why I brought sitehome.php into this discussion is because that's what I was recommended to do in the book.
4) see 3)
5) the main purpose for this redirect from /index.php to / is because, to my understanding, search engines treat a homepage at / and a homepage at /index.php as two separate pages. I'd like to consolidate this for linkbuildling and ranking purposes.

I hope that clarifies my intentions.
   -Duncan
Logged
MrPhil
Berserker Poster
*****
Offline Offline

Posts: 5083



« Reply #5 on: July 30, 2010, 07:17:38 AM »

Sorry not to get back to you sooner, but I've been out all week.

I really can't tell what you're trying to accomplish with this sitehome.php and SEO stuff. I think you've gotten all tangled up in your underwear, by not understanding how the system works before you started making changes, and not understanding what the SEO book is trying to accomplish (and even more so, making sure it's compatible with your Lunarpages setup).

Your real "entry point" is still /index.php, right? You're concerned that search engines will see / and /index.php as two separate pages, and hurt your search results? I'm not sure if that's true -- have you actually looked at Google to see if that's happening? Can anyone with more SEO experience than me tell if this is true? Is there any place that you actually create links with /index.php in them? If it is a real problem (that both / and /index.php are being separately cataloged, hurting search results), I would think it easier to do a 301 redirection to tell search engines to index just /index.php, rather than / (if a request comes in for just "/", send back a 301 telling the requestor to explicitly give "/index.php"). An alternative would be to add a new page "meta" tag for "canonical URL" -- explicitly tell search engines to index just / or just /index.php, rather than whatever URL they happen to see at the moment. I would think that would be far easier than all this thrashing around with a dummy sitehome.php and changes to .htaccess and whatever else you're doing.
Logged

duncan1on1
Newbie
*
Offline Offline

Posts: 4


« Reply #6 on: July 31, 2010, 02:18:16 AM »

No problem, I really appreciate all the help thus far.

I took your advice and did some poking around, probably something I should have done in the beginning. I made a Google search (excluding the []): [index.php site:1on1mandarin.com] to see if Google was indexing that page, and- well, /index.php doesn't seem to be indexed, so I guess Google isn't treating the / and /index.php pages as separate.

I would still like to have the URL still show up as www.1on1mandarin.com/ for purely vanity reasons though. But in any case- I'll clean up the existing .htaccess file first, with your previous recommendations.

Thanks again for your help!
Logged
Pages: [1]   Go Up
  Print  
 
Jump to: