Fix an extra slash beeing added during domain redirect to www with mod_rewrite

Wednesday, 28th July 2010

I have recently added a redirect to www forwarding for a domain using mod_rewrite capabilities.
The exact mod rewrite rules I in my <VirtualHost> used was:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.pc-freak.net
RewriteRule (.*) https://www.pc-freak.net/$1 [R=301,L]

Nevertheless the redirect was okay I have noticed that everytime the redirect has been in move from domain.com to www.domain.com an extra slash has been added included right after the domain, an example of the unwanted behaviour I have encountered is illustrated in the picture below:

Double Splash domain issue in mod_rewrite redirect to www

A help from a good guy in irc.freenode.net #httpd under the alias jink told me that me that in order to solve the extra slash added to the url I need to modify the rewrite rules to look like the one below:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.pc-freak.net
RewriteRule (.*) https://www.pc-freak.net$1 [R=301,L]

Thanks God This solved the issues.

Share this on:

Download PDFDownload PDF

Tags:

One Response to “Fix an extra slash beeing added during domain redirect to www with mod_rewrite”

  1. Chad Cloman says:
    Firefox 3.6.8 Firefox 3.6.8 Windows XP Windows XP
    Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 (.NET CLR 3.5.30729)

    Here’s why that change was necessary.

    If the rewrite rule is in the server configuration file (httpd.conf), then the leading slash is included as part of pattern that gets matched.

    If the rewrite rule is in a per-directory configuration file such as .htaccess, then the path is stripped out before the matching occurs, and there is no leading slash.

    View CommentView Comment

Leave a Reply

CommentLuv badge