Welcome to WebmasterWorld Guest from 3.227.233.78

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Use trailing slash on basic rewrite?

     
5:30 pm on Apr 19, 2008 (gmt 0)

New User

10+ Year Member

joined:Jan 10, 2005
posts: 30
votes: 0


Sorry to ask the most basic question, but I've seen this done both ways.
For the simple .htaccess rewrite (eliminate www.) should I use the slash?

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com$1 [L,R=301]

or

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

On one host they both seem to work, so which is better to use 'all the time'?

Thank you

[edited by: jdMorgan at 3:35 pm (utc) on April 20, 2008]
[edit reason] example.com [/edit]

1:39 pm on Apr 20, 2008 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


It depends on whether the code is for use in httpd.conf or conf.d, or in .htaccess.

In httpd.conf or conf.d:


RewriteEngine on
#
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule (.*) http://example.com$1 [R=301,L]
-or-

RewriteEngine on
#
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule [b]^/([/b].*)$ http://example.co[b]m/$[/b]1 [R=301,L]

In .htaccess:

RewriteEngine on
#
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule (.*) http://example.co[b]m/$[/b]1 [R=301,L]

You could also make a version that would work in either location, with a loss of efficiency:

RewriteEngine on
#
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule [b]^/?([/b].*)$ http://example.co[b]m/$[/b]1 [R=301,L]

The additional RewriteCond is a "safety net." It prevents an 'infinite' redirection loop in the case where a request is received without a "Host:" header. It is not needed on pure named-based shared hosting (because a request without a Host: header would never reach such a server ), but is needed on any shared hosting with a 'private' server IP address, on a VPS, or on dedicated hosting. Basically, if the host can be reached with an HTTP/1.0 request, then that RewriteCond is required. If it's only accessible with HTTP/1.1 (or later), then that line is not needed.

I deleted the "RewriteBase /" directive, as it was specifying the default value and therefore was not useful in this context.

Jim