301 redirect host problem

My site is hosted by Fasthosts. I have recently been trying to create a redirect from rugbyboats.co.uk/index.php to rugbyboats.co.uk/ (or vice versa - it doesn’t matter) as at the moment the Googlebots etc regard this as two separate pages.

The code I inserted into my .htaccess file was:

Options +FollowSymLinks
RewriteEngine 0n

index.php to /

RewriteCond %{THE REQUEST} [1]{3,9}\ /.index\.php\ HTTP/
RewriteRule ^(.
)index\.php$ /$1 [R=301,L]

When uploaded it returns an Internal Server Error. I then contacted Fasthosts to be told that their Linux servers did not support 301 redirects as it is a security issue. Is this true? Or are Fasthosts unusual in this regard? And is there a workaround? I have emailed their technical support but so far they have not come back with any solution.


  1. A-Z ↩︎

You’re a useful guy to know. I’ve taken all that on board and it works, so thank you very much.

The “the” you refer to was only there as a definite article by the way:)

Right, I have been up and running with Clook for about 2 weeks now and they have been extremely helpful when I ran into one or two problems with moving over my domain.

The code I am using in the .htaccess file to redirect http://www.rugbyboats.co.uk to simply http://rugbyboats.co.uk is:

RewriteEngine On
RewriteOptions inherit
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.rugbyboats.co.uk [NC]
RewriteRule ^(.*)$ http://rugbyboats.co.uk/$1 [L,R=301]

It works. Now I need to work out how to adapt that code so that http://www.rugbyboats.co.uk/index.php redirects to http://.rugbyboats.co.uk/

Thanks to you all.

It is a compliment, and a sincere one. And absolutely no offence taken whatsoever!

cbm,

Thanks! I’ll take that as a compliment. I believe that when I tell someone NOT to use another’s code, it’s imperative that I fully explain my reasons. As SitePoint staff, I must consider ALL members who might read a post, not just the OP or the responder.

I’m not sure which “the” you’re referring to but please know that I mean noone any offense even though I seem to :smashy: all too often. :blush: I guess old age is setting in.

Regards,

DK

cbm,

I’ll move this to the Apache forum as that’s where mod_rewrite is discussed (and that’s my excuse for not responding before this).

Redirect is a mod_alias command which is in Apache’s core. It is NOT a security issue. Neither is mod_rewrite, for that matter.

However, since all the posters here were using mod_rewrite, I’ll comment on those codes:

  1. IMHO, (almost) NEVER use the {THE_REQUEST} variable as all the ridiculous extra information (GET/POST and the HTTP version with spaces setting off the {REQUEST_FILENAME}).

  2. RewriteBase is used to UNDO a mod_alias redirect for mod_rewrite to work on the {REQUEST_URI}. IMHO, unless you’re using a Redirectx statement, there is NO benefit to using RewriteBase and it will confuse the mod_rewrite locations.

  3. (.*) is the well known garbage collector (I’ve labeled it the :kaioken: EVERYTHING :kaioken: atom because it will match NOTHING OR EVERYTHING) but its main problem is that it will generate loops when used incorrectly.

Specifically:

Options +FollowSymLinks
# that should be in the httpd.conf

RewriteEngine 0n
# good

# index.php to /
# [COLOR="Gray"]RewriteCond %{THE REQUEST} ^[A-Z]{3,9}\\ /.*index\\.php\\ HTTP/[/COLOR]
# this accomplishes exactly NOTHING and
# is likely the cause of the 500

RewriteRule ^(.*)index\\.php$ /$1 [R=301,L]
# other than the :kaioken: EVERYTHING :kaioken: atom,
# the leading / in the redirection should NOT be used

Fasthost’s code:

RewriteEngine On
RewriteOptions inherit
# WHY bother?
RewriteBase /
# WHY bother?
RewriteCond %{HTTP_HOST} ^www\\.rugbyboats.co.uk\\.com$ [NC]
# WRONG!  I've never heard of a domain tld .co.uk.com
# AND it's irrelevant
RewriteRule ^(.*)$ http://rugbyboats.co.uk/$1 [L,R=301]
# That does exactly NOTHING - WHY bother?

Encrypted’s code (PLEASE DON’T wrap code in quotes - that’s what the code is for):

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

# index.html to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\\ /([^/]+/)*index\\.html\\ HTTP/
# Ditto the above
RewriteRule ^(([^/]+/)*)index\\.html$ http://www.[COLOR="Magenta"]codeegg[/COLOR].com/$1 [R=301,L]
# hmmm, it could work to send {garbage}/index.html to {garbage}
# I don't believe that's what the OP wants to do!

To address the original question, though, WHY force the removal of the DirectoryIndex from a {REQUEST_URI} string? That’s something which is determined by the server’s configuration file. If you need to pursue this, please say so and I’ll help with the code to remove the DirectoryIndex as desired.

Regards,

DK

Exactly what I’m in the process of doing. I’m shifting over to Clook after some people on here pointed me in that direction. Thanks.

I suggest you get in touch with current web host and try to find out the problem.

Good luck in solving that. Just keep us infomed if you might have any question.

I suggest finding a new host then. There’s no reason for blocking a 301 redirect.

I’ve tried that - the index.php version so thank you for the help.

Sadly, it doesn’t work, although unlike the earlier version, at the least the site still works. If I go to my site and add “index.php” to the url it does not redirect, and I end up with my home page. Google is definitely seeing this as a different page because rugbyboats.co.uk/ has a PR of 3 while rugbyboats.co.uk/index.php has a PR of 2.

But then the code that Fasthosts grudgingly gave me doesn’t do anything either:headbang:

adulti,

Do NOT use canalboarman’s code above, use:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\\.example\\.com [NC]
RewriteRule .? http://example.com%{REQUEST_URI} [L,R=301]

Reason:

RewriteBase is designed to UNDO a mod_alias Redirect. If you don’t use Redirects, you don’t need this (and it can confuse YOU if it changes the directory) because it uses processor time to do NOTHING.

RewriteOptions is designed to do, er, NOTHING! It’s only option is “inherit” which is the default so, once again, you’re wasting processor time to do NOTHING.

Using the :kaioken: EVERYTHING :kaioken: atom to capture the {REQUEST_URI} string is yet another waste of time! I’ve used a “placeholder” regex to match zero-or-one character in the {REQUEST_URI} then direct to the preferred format of the domain name (without www in this case). Note that there is NO slash between the domain and {REQUEST_URI} because Apache 2.x includes it automatically.

canalboatman, please take note of the above - at least if you’re on a shared server (where others will be impacted by your code).

I think you did NOT mean to leave the . in your “redirection” but I’m concerned with omitting the DirectoryIndex (usually, NOT a good idea), however, this is as simple as the above, i.e., BEFORE the above, insert (after the RewriteEngine on):

RewriteCond %{HTTP_HOST} ^www\\.rugbyboats\\.co\\.uk [NC]
RewriteRule ^index\\.php$ http://rugbyboats.co.uk [L,R=301]

As usual, please note that this is for Apache 2.x; Apache 1.x must replace the ^ with ^/ and, if you’re not sure which version you have, replace ^ with ^/?.

Regards,

DK

i’m really interested in this, can you get us posted about your host and the code you used for redirect?

My hosting company seems to not allow this either. I want to do this for seo consideration too, but still not succeed yet.

Haven’t fully transferred to Clook yet but the files are uploaded. More news as it happens…

So how it is going now? Do you have some problems again?

Use this: (replace your url)

index.php to /

RewriteCond %{THE_REQUEST} [1]{3,9}\ /([^/]+/)*index\.php\ HTTP/

RewriteRule ^(([^/]+/)*)index\.php$ http://www.codeegg.com/$1 [R=301,L]

index.html to /

RewriteCond %{THE_REQUEST} [2]{3,9}\ /([^/]+/)index\.html\ HTTP/
RewriteRule ^(([^/]+/)
)index\.html$ http://www.codeegg.com/$1 [R=301,L]

Also, redirects are not security issue. I’m not sure where they pulled that statement from. lol


  1. A-Z ↩︎

  2. A-Z ↩︎

To which, if anyone’s interested or having a similar problem, their response was:

RewriteEngine On
RewriteOptions inherit
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.rugbyboats.co.uk\.com$ [NC]
RewriteRule ^(.*)$ http://rugbyboats.co.uk/$1 [L,R=301]

Now I just need someone to tell me whether this will work or not:)