Help with mod_rewrite 301 appending extra /

Posted on 2009-04-21
Last Modified: 2012-05-06
I have the following mod_rewrite rule :
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^example\.com\.au$ [NC]
    RewriteRule ^(.*)$$1 [R=301,L]

The problem is when it redirects to it appends an extra / so the user ends up at

I know I can use no slash before $1:
    RewriteRule ^(.*)$$1 [R=301,L]

But I'm not sure if this is the best way to do it.
Question by:TotallyMe
    LVL 27

    Accepted Solution

    > I know I can use no slash before $1:

    There's no problem with that; this is common practice if the rule is used in th httpd.conf. Another way would be to strip the slash from the matched pattern:
    RewriteEngine On
    # if you're looking for a direct match, use the lex eq. operator instead of a regular expression
    RewriteCond %{HTTP_HOST} ! [NC]
    RewriteRule ^/(.*)$$1 [R=301,L]

    Open in new window


    Author Comment

    That works great :) I really like the simplicity of  !
    And I'm using ^/(.*)$ just incase my URLs went back to a single slash and stopped working.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    tomcat LDAP authentication issue 3 110
    How to analyze web traffic logs 10 76
    PHP Error Settings 12 66
    LAMP problem identifier tool ? 9 95
    If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
    In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now