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.
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.
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.
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 all too often. I guess old age is setting in.
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:
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}).
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.
(.*) 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.
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:
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):
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 ^/?.