force www + remove trailing slash - Single Rule.

Discuss practical ways rearrange URLs using mod_rewrite.

force www + remove trailing slash - Single Rule.

Postby kudos_22 » Thu Jun 26, 2008 5:35 pm

My rewrite rules.

----

<IfModule mod_rewrite.c>
RewriteEngine On

#[ FORCE WWW ]#
RewriteCond %{HTTP_HOST} ^(domain\.com)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

#[ SET BOOT ]#
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}/ROOT%{REQUEST_URI} !-f
RewriteRule ^(.*)$ /CONF/boot.php/$1 [QSA,L]

#[ SET ROOT ]#
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /ROOT/$1 [QSA,L]
</IfModule>

--

My Question.

Is it possible to force both a www if its missing and remove a trailing slash (considering it is not a directory "!-d"), with a single rewrite rule.

You can forget about the #[ SET BOOT ]#, #[ SET BOOT ]#
kudos_22
 
Posts: 7
Joined: Tue Dec 05, 2006 2:03 pm

Postby richardk » Sat Jun 28, 2008 4:26 pm

Yes and no. you can have a rule that removes trailing slashes and forces www in one rule, but it will only work when both tasks need to be done (ie. not for directories). So you have to have it as well.
Code: Select all
Options +FollowSymLinks

<IfModule mod_rewrite.c>
  RewriteEngine On

  #[ REMOVE TRAILING SLASH (AND FORCE WWW) ]#
  RewriteCond %{DOCUMENT_ROOT}/ROOT%{REQUEST_URI} !-d
  RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]

  #[ FORCE WWW (FOR EVERYTHING ELSE) ]#
  RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]
  RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

  #[ SET BOOT ]#
  RewriteCond %{ENV:REDIRECT_STATUS} ^$
  RewriteCond %{DOCUMENT_ROOT}/ROOT%{REQUEST_URI} !-f
  RewriteRule ^(.*)$ /CONF/boot.php/$1 [QSA,L]

  #[ SET ROOT ]#
  RewriteCond %{ENV:REDIRECT_STATUS} ^$
  RewriteRule ^(.*)$ /ROOT/$1 [QSA,L]
</IfModule>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 100 guests

cron