Forum Moderators: phranque

Message Too Old, No Replies

mod Rewrite - Annoyance

         

LinuxJunkie

9:54 am on Aug 12, 2005 (gmt 0)

10+ Year Member



I am having terrible trouble getting mod_rewrite to behave.

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]

jd01

11:53 am on Aug 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi LinuxJunkie,

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

s1developer

11:57 am on Aug 12, 2005 (gmt 0)

10+ Year Member



I think the problem could be that by the time the htaccess file is being processed the initial slash has been removed from the request, so you might have better luck with something a bit more general, like this:

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.

LinuxJunkie

1:08 pm on Aug 12, 2005 (gmt 0)

10+ Year Member



Thanks for the feedback. The modified rule worked to replace
domainname with www.domainname in the browser.

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

LinuxJunkie

1:35 pm on Aug 12, 2005 (gmt 0)

10+ Year Member



I should add that removing the $1 isn't helpful.
I would like
http://www.example.co.uk/some_subdirectory/index.shtml

to become

http://www.example.co.uk/some_subdirectory/

[edited by: jdMorgan at 3:32 pm (utc) on Aug. 12, 2005]
[edit reason] Removed specifics per TOS, [/edit]

jdMorgan

3:44 pm on Aug 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you'll need a separate ruleset preceding the domain redirect rule discussed above. And it has to be in a specific form to avoid an infinite loop when interacting with your DirectoryIndex directive:

# 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]

Jim

LinuxJunkie

8:23 am on Aug 15, 2005 (gmt 0)

10+ Year Member



Thanks you for all your help

Yours
Arthur Coombes

LinuxJunkie

8:33 am on Aug 15, 2005 (gmt 0)

10+ Year Member



Jim,
I don't know if I've done something wrong. This rule (to remove the index.shtml) works correctly for the lowest level directory but not for higher level directories. I will copy and past the rule As I have typed it. Can you suggest where I might be going wrong.

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]

jdMorgan

2:19 pm on Aug 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, It's nothing you did, I typo'ed my post above...

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[b](.+/)?[/b]index\.shtml\ HTTP
RewriteRule ^[b](.+/)?[/b]index\.shtml$ http://www.example.co.uk/$1 [R=301,L]

Jim