user1767734
user1767734

Reputation: 31

Rewrite rule gives two trailing slashes

I hope someone can help me with a mod_rewrite rule, that works apart from adding trailing slashes.

This is the rule

<IfModule rewrite_module>

Options Indexes FollowSymLinks +IncludesNOEXEC 
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.org$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.org/$1 [L,R=301]

</IfModule>

The purpose is to rewrite the URL mydomain.org in the form www.mydomain.org

This works. but then www.mydomain.org// shows in the browser address bar. Checking the rewrite log shows that the // is explicitly created by the rule

Questions are:

  1. Do (or could) the two slashes matter?
  2. If the answer to (1) is yes, can I redo the rule so that the trailing slashes are omitted?

Upvotes: 3

Views: 411

Answers (2)

anydot
anydot

Reputation: 1539

  1. No, two slashes generally doesn't matter, it doesn't confuse software anyhow. It just means you have to transfer one byte more (which can matter in some situations).
  2. Just change your rewrite rule to RewriteRule ^/?(.*)$ http://www.mydomain.org/$1 [L,R=301]. That /? is what does the trick.

Upvotes: 1

RichardTheKiwi
RichardTheKiwi

Reputation: 107736

Most servers ignore the double slash and treat it as a single. See for example this question (double-slash) https://stackoverflow.com//questions/13027041

To fix your RewriteRule, I believe you just need to change it to

RewriteRule ^/?(.*)$ http://www.mydomain.org/$1 [L,R=301]

The /? part makes it optional (root access), and if it is found, it gets stripped since it is not part of the captured (.*) section.

Upvotes: 1

Related Questions