homepage Welcome to WebmasterWorld Guest from 54.167.138.53
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

    
How do I add I add trailing slash in .htaccess to requests without it?
I want requests for mydomain.com to resolve to mydomain.com/
Cinemascribe




msg:1520490
 5:25 pm on Jun 16, 2003 (gmt 0)

What do I need to add to my .htaccess file to make all requests for mydomain.com (the main page of my site without the trailing slash) resolve to mydomain.com/ (the same page with the slash included)? On my Mac, all the Web browsers are automatically directed to the address with the trailing slash, but I've noticed on IE 6 for Windows that if a user enters "mydomain.com" that WinIE 6 does not add the trailing slash. This is a problem because all the images in my page are referenced in the HTML as <img src="images/logo.gif"> rather than <img src="/images/logo.gif">, so they are not displayed when the URL does not include the trailing slash. This problem only affects the home page of my site.

What do you think the better course of action would be? Should I simply add the intro slash to all the IMG SRC occurences? Or should I add something to my .htaccess file that automatically redirects all requests for mydomain.com (without the trailing slash) to mydomain.com/ (with the trailing slash)? Any help or advice would be greatly appreciated.

 

Cinemascribe




msg:1520491
 5:37 pm on Jun 16, 2003 (gmt 0)

This is weird. IE 6 has no problems displaying the images on other websites like mine that do not add the into slash to IMG SRC tags and don't include the trailing slash on the domain. Could it be a problem with my server?

jdMorgan




msg:1520492
 6:35 pm on Jun 16, 2003 (gmt 0)

Cinemascribe,

Welcome to WebmasterWorld [webmasterworld.com]!

This *is* weird, because Apache mod_dir (if loaded) should add the trailing slash automatically.

First, try adding the following line to .htaccess in your web root directory:

DirectoryIndex index.html

(This assumes that "index.html" is the proper page - if not, substitute the proper page name. Don't worry, this is not a redirect, and "index.html" won't "show" in the browser address bar.)

If mod_dir is active, this may "force" it to add the trailing slash.

Second, whether that works or not, I suggest you contact your hosting service, and tell them the results of the experiment.

Third, if they are brain-locked, you can try mod_rewrite. But it should not be necessary if the server is set up correctly:

RewriteEngine on
RewriteRule ^$ http://www.yourdomain.com/ [R=301,L]

Refs: Introduction to mod_rewrite [webmasterworld.com], Apache mod_dir [httpd.apache.org], Apache mod_rewrite [httpd.apache.org]

HTH,
Jim

Cinemascribe




msg:1520493
 8:35 pm on Jun 16, 2003 (gmt 0)

Hi, jdMorgan. Thanks for your post.

But I just discovered (before I read your post) that if I take all the hotlink protection stuff out of my .htaccess file that the images show up on the main page of my site in IE 6 even if the trailing slasher is not included. So, that's an easy fix, but now I won't have hotlink protection. I removed all this:

-------------
RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://mysite.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://google.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://images.google.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.mysite.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.google.com/.*$ [NC]
RewriteRule .*\.(jpg¦gif)$ [mysite.com...] [R,NC]
-------------

Oh, well. I guess this is okay -- I don't have too much of a problem with hotlinkers. It's not like they're going to make me exceed my bandwidth. They're just really annoying. I'll check out your ideas later today when I have the time. Much thanks!

jdMorgan




msg:1520494
 9:15 pm on Jun 16, 2003 (gmt 0)

Cinemascribe,

OK, I see where you broke the images with the incorrect end-anchors. Try this instead:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.¦images\.)?google\.com [NC]
RewriteRule \.(jpg¦gif)$ - [NC,F]

Remember to edit the broken vertical pipe characters and change them to solid vertical pipes before use.

Jim

Cinemascribe




msg:1520495
 10:02 pm on Jun 16, 2003 (gmt 0)

That seems to work. Thank you so much!

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