Forum Moderators: phranque
I am trying to get any references to http://example.co.uk rewritten as http://www.example.co.uk and any referece to the index.html removed. so that
http://example.co.uk/index.html will get re-written simply as http://www.example.co.uk/
I have added the folloing line to the base .htaccess file on my server.
RewriteCond %{HTTP_HOST} !^www\.example\.co\.uk [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.co.uk/ [R,L]
Cribbed from the mod_rewrite manual on apache.org
Hovere whatever I try I cannot get it to work correctly. Can anybody offer advice?
Yours
Arthur Coombes
[edited by: jdMorgan at 3:29 pm (utc) on Aug. 12, 2005]
[edit reason] Obscured specifics per TOS. [/edit]
Welcome to WebmasterWorld.
The problem is this line:
RewriteRule ^/(.*)
In the .htaccess file the preceding / is stripped and not used for comparrison, where in the httpd.conf file - where the example is based - the preceding slash is present and will match.
The .htaccess version of the same rule is:
RewriteRule ^(.*)
A shorter version of your ruleset is:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.co\.uk [NC]
RewriteRule (.*) http://www.example.co.uk/$1 [R=301,L]
Cond 1: check to see that the user-agent sends HOST headers.
Cond 2: check to see if the HOST is *not* yoursite.com
Rule: any requested URL - No need for the ^ beginning and $ ending when we are going to store everything.
Note:
Added: $1 to the right side of the rule - this will redirect to the same location as requested without the www
Added: =301 to the R flag - the default Redirect is 302, Generally speaking 301 is better.
Hope this helps - late here, so I hope I did not miss too much.
Justin
RewriteCond %{HTTP_HOST}!^www\.example\.co\.uk [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule .* http://www.example.co.uk/ [R,L]
The syntax you had would probably be correct within an httpd.conf file, but I think for htaccess-level rewrites you should omit the initial slash.
Also you probably don't need the parentheses unless you're planning to use $1 in the second half of the rewrite (and if you're wanting 'index.html' to disappear or something along those lines, then you maybe don't want to use $1).
Hope that helps.
Is there any way i can strip index.shtml from the front of the URL so that
www.moneynet.co.uk/index.shtml becomes www.moneynet.co.uk
I sure i could do it through trial and error but unforunately this is a live site ( hence doing the work through .htaccess rather than httpd.conf.
Yours
Arthur Coombes
# Redirect requests for index.shtml in any directory to "/" in the same directory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([.+]/)?index\.shtml\ HTTP
RewriteRule ^([.+]/)?index\.shtml$ http://www.example.co.uk/$1 [R=301,L]
#
# Redirect requests for resources in non-www domains to same resources in www domain
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.co\.uk [NC]
RewriteRule (.*) http://www.example.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([.+]/)?index\.shtml\ HTTP
RewriteRule ^([.+]/)?index\.shtml$ http://www.example.co.uk/$1 [R=301,L]
[edited by: jdMorgan at 2:16 pm (utc) on Aug. 15, 2005]
[edit reason] No specifics, please. See TOS. [/edit]