Forum Moderators: phranque

Message Too Old, No Replies

rewriterule struggle

index redirect to root

         

dmdickie

12:55 pm on May 3, 2009 (gmt 0)

10+ Year Member



Hello

I am editing the httpd.conf file on my sme server
running 2.0.52 apache

here is part of the file that is relevant

RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^(TRACEŠTRACK)
RewriteRule .* - [F]

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

##example.com is redirected to www.example.com
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com$1 [R=301,L]

example.com is redirected to www.example.com and is working fine

but I cannot get www.example.com/index.php to redirect to www.example.com

I am a newbie to rewrites and am in the middle of the second day trying to solve this problem. I have studied many examples and feel that this should work.

Any pointers to why it's not working would be greatfully appreciated

thanks very much

Dickie

g1smd

6:07 pm on May 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The second rule should work to index filenames just fine. Clear your cache and try again.

The third rule appears to have a slash missing, directly before the $1. The third rule does not fix www requests that include a port number.

jdMorgan

6:12 pm on May 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've apparently been taking code examples intended for use in per-directory .htaccess files, and trying to use them in a server config file, outside of a <Directory> container.

The URL-path "seen" by RewriteRule differs in these contexts, so the patterns must be adjusted:


RewriteEngine on
#
## Deny HTTP TRACE and TRACK methods
RewriteCond %{REQUEST_METHOD} ^(TRACEŠTRACK)
RewriteRule .* - [F]
#
## Externally redirect /<any-directory>/index.php to www.example.com/<any-directory>/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule [b]^/([/b]([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]
#
## Externally redirect all non-canonical example.com hostnames to canonical www.example.com hostname
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule [b]^/([/b].*)$ http://www.example.co[b]m/$[/b]1 [R=301,L]

The extra RewriteCond plus the change to the pre-existing one enables the third rule to correct *all* hostname canonicalization problems, such as "example.com" www.example.com:80, www.example.com." and www.example.com.:80, all of which are perfectly-valid but non-canonical hostnames.

The "!=www.example.com" pattern is identical to the fully-anchored regex pattern "!^www\.example\.com$" but uses a simple exact-match string compare instead of regular expressions. This is a slightly more efficient way to look for exact matches.

Important: Replace all broken pipe "Š" characters with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

g1smd

7:11 pm on May 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I missed the fact that you were using this in httpd.conf - must read the question more thoroughly next time.

dmdickie

7:39 pm on May 3, 2009 (gmt 0)

10+ Year Member



thanks very much to you both - g1sdm and jim

I have read many of your other post replies and have learnt plenty from them ..

and love all the efficient coding etc plus URL inc port numbers

I have plenty to learn

It was a joy to see my problem solved!

Dickie