homepage Welcome to WebmasterWorld Guest from 54.145.252.85
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Mod Rewrite
www.site.com/index.html to www.site.com
biggles

10+ Year Member



 
Msg#: 3906 posted 2:12 pm on Jun 23, 2005 (gmt 0)

RewriteRule ^index.html$ / [R=301,L]

From my woefully limited knowledge of Modrewrite I understand the above code ensures search engines only index http://site.com/ and not http://site.com/index.html

Can someone please advise how this should be applied for WWW addresses.

i.e. what rewrite code should be used in the htaccess file to ensure search engines only index www.site.com rather than www.site.com/index.html.

(All the code examples I've found just are for http://site.com/)

Many thanks

[edited by: jdMorgan at 3:35 am (utc) on Aug. 26, 2005]
[edit reason] De=linked. [/edit]

 

jd01

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3906 posted 8:31 pm on Jun 23, 2005 (gmt 0)

The code you posted should work for both versions. Have you tested it?

The only real adjustment is to \ the .(dot) to match a litteral .(dot) instead of 'any character except the end of a line'

RewriteRule ^index\.html$ / [R=301,L]

Justin

andy_boyd

10+ Year Member



 
Msg#: 3906 posted 10:34 pm on Jun 23, 2005 (gmt 0)

I'm using the following code to redirect all non-www to www ...

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^website.com
RewriteRule ^(.*)$ http://www.website.com/$1 [R=permanent,L]

However, it does not redirect www.website.com/index.php to www.website.com/ ... can anyone say what I need to add to make that happen?

Any help would be really appreciated.

[edited by: jdMorgan at 3:33 am (utc) on Aug. 26, 2005]
[edit reason] De-linked. [/edit]

biggles

10+ Year Member



 
Msg#: 3906 posted 11:08 am on Jun 27, 2005 (gmt 0)

Thanks Justin

I had tested the code & it doesn't work - even with the mod you kindly suggested. What happens is that the redirection times out. When I did a header check it shows that the redirection is going into an endless loop as below:

SEO Consultants Directory Check Server Headers - Single URI Results
Current Date and Time: 2005-06-27T03:49:10-0800
User IP Address:

#1 Server Response: http://www.example.com/index.htm
HTTP Status Code: HTTP/1.1 301 Moved Permanently
Date: Mon, 27 Jun 2005 10:49:10 GMT
Server: Apache
Location: http://www.example.com/
Connection: close
Content-Type: text/html; charset=iso-8859-1
Redirect Target: http://www.example.com/

#2 Server Response: http://www.example.com/
HTTP Status Code: HTTP/1.1 301 Moved Permanently
Date: Mon, 27 Jun 2005 10:49:10 GMT
Server: Apache
Location: http://www.example.com/
Connection: close
Content-Type: text/html; charset=iso-8859-1
Redirect Target: http://www.example.com/

#3 Server Response: http://www.example.com/
HTTP Status Code: HTTP/1.1 301 Moved Permanently
Date: Mon, 27 Jun 2005 10:49:10 GMT
Server: Apache
Location: http://www.example.com/
Connection: close
Content-Type: text/html; charset=iso-8859-1
Redirect Target: http://www.example.com/

Above keeps repeating....

The htaccess file is as follows:

# -FrontPage-

IndexIgnore .htaccess */.?* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^index.htm$ / [R=301,L]
</IfModule>

AuthName www.example.com
AuthUserFile /www/site/_vti_pvt/service.pwd
AuthGroupFile /www/site/_vti_pvt/service.grp

Is there some error in the above causing this issue?

[edited by: jdMorgan at 3:37 am (utc) on Aug. 26, 2005]
[edit reason] De-linked. [/edit]

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3906 posted 10:06 pm on Jul 5, 2005 (gmt 0)

I'd suggest the following to fix both the domain and index duplication, as well as preventing redirect recursion:

# Redirect client index.html requests to "/"
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]
#
# Force preferred domain for all requests to this server
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Using THE_REQUEST ensures that the redirect happens only if the originally-requested URL is "/index.html" -- it won't happen as a result of the internal server processing that substitutes "index.html" for "/". Just for reference, the pattern in that RewriteCond matches the form of THE_REQUEST, an example of which would be:

GET /index.html HTTP/1.1
-or-
PROPPATCH /something.html HTTP/1.0

Jim

biggles

10+ Year Member



 
Msg#: 3906 posted 2:15 am on Jul 6, 2005 (gmt 0)

Hi Jim

Many thanks for your reply which has finally solved the problem. :-)

I tried the code you suggested but it didn't immediately work when I plugged in my domain details.

RewriteEngine on
RewriteBase /
# Redirect client index.htm requests to "/"
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.htm\ HTTP/
RewriteRule ^index\.htm$ http://www.example.com/ [R=301,L]
#
# Force preferred domain for all requests to this server
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.co\.nz
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

However it did work when I changed the order around & replaced the "Force preferred domain for all requests to this server" code with what I'd been using before. The code that seems to be working OK is this:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.co.\nz [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

# Redirect client index.htm requests to "/"
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.htm\ HTTP/
RewriteRule ^index\.htm$ http://www.example.com/ [R=301,L]

Do you see any issues with doing it this way? Hoping I've not made a major cock up doing this because I didn't understand your comments that for reference, the pattern in that RewriteCond matches the form of THE_REQUEST, an example of which would be:

GET /index.html HTTP/1.1
-or-
PROPPATCH /something.html HTTP/1.0

Again, many thanks for your help

[edited by: jdMorgan at 3:41 am (utc) on Aug. 26, 2005]
[edit reason] De-linked. [/edit]

j_h_maccann

10+ Year Member



 
Msg#: 3906 posted 7:57 pm on Jul 15, 2005 (gmt 0)

Can this recipe be generalized so that all requests for

http://www.example.com/anything/index.htm

are redirected to the corresponding

http://www.example.com/anything/

--and also for two levels deep, three levels deep, etc.?

Can one rule handle zero levels of directories (above) and
also n levels of directories in the URL for n=1,2,3, ...?

jd01

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3906 posted 9:45 pm on Jul 15, 2005 (gmt 0)

You should be able to do what you are asking with a little adjustment, depending of course, on you exact file structure, etc.:

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

Condition: Added a catch-all that precedes index.htm, so /anything that ends in index.html will match.

Rule: removed the ^ (hard begining of a line), but left the $ (end of a line), so any request that ends in index.htm will be compared. Also added %1 to the Rewrite URL, so if there is anything stored preceding index.htm, that will be returned in the redirect.

Hope this helps.

Justin

I have not tested this, so it may need a 'tweek' or two.

[edited by: jdMorgan at 3:42 am (utc) on Aug. 26, 2005]
[edit reason] Examplified. [/edit]

j_h_maccann

10+ Year Member



 
Msg#: 3906 posted 10:01 pm on Jul 15, 2005 (gmt 0)

How about this?

Options +FollowSymLinks
RewriteEngine on
#
# goal: add trailing slash to any URI if missing (w/ any num of dirs)
# if the URI is anything except a string of non-dots with final slash
RewriteCond %{REQUEST_URI} !^[^.]*/$
# then rewrite it as the entire string plus final slash
RewriteRule ^(.+)$ $1/ [R=301,L]
#
# goal: remove final index.htm if present (w/ any num of dirs)
# if the URI ends with a slash plus final string of index.htm
RewriteCond %{REQUEST_URI} ^.*/index\.htm$ [NC]
# then remove final string of index.htm
RewriteRule ^(.*/)index\.htm$ $1 [R=301,L]

jd01

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3906 posted 11:13 pm on Jul 15, 2005 (gmt 0)

Using THE_REQUEST ensures that the redirect happens only if the originally-requested URL is "/index.html" -- it won't happen as a result of the internal server processing that substitutes "index.html" for "/". Just for reference, the pattern in that RewriteCond matches the form of THE_REQUEST, an example of which would be

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved