SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Redirect Doesn't Work

  1. #1
    SitePoint Member
    Join Date
    Nov 2009
    Location
    Canada
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Redirect Doesn't Work

    Greetings,

    I want to redirect visitors if the URL doesn't match. I'm trying this:

    Code:
    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^(https?:\/\/)?(w{3}\.)?popupsinc\.com\/intranet\/?$ [NC]
    RewriteRule (.*) http://www.custompopups.com
    </IfModule>
    However, this redirects everything. Can anyone see what I'm doing wrong? I suspect it might be my rule patten "(.*)", but I don't know how to change/fix this.

    Thanks,
    Shane.
    PHP version 5.2.9
    Apache/2.0.54 (Debian GNU/Linux 4)

  2. #2
    Do. Or do not. There is no try silver trophy
    SitePoint Award Recipient ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,347
    Mentioned
    87 Post(s)
    Tagged
    2 Thread(s)
    %{HTTP_HOST} only contains the host name, nothing else. So it does not (like you seem to assume) contain the schema (http/https) and also does not contain the requested path.

    What you need to do is forget about http/https, and spread the host and path over %{HTTP_HOST} and %{REQUEST_URI}, like so

    Code:
    RewriteCond %{HTTP_HOST} !^(w{3}\.)?popupsinc\.com$ [NC,OR]
    RewriteCond %{REQUEST_URI} !^/intranet/?$ [NC]
    RewriteRule .? http://www.custompopups.com/ [L,R=301]
    Note that I've removed the <IfModule mod_rewrite.c>...</IfModule> since they are quite abusive of the server (why ask for something over and over again when you already know the answer?)

    Also, I added [L,R=301] to the last rule to make it a 301 (permanent) redirect instead of the default 302 (temporary).
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Member
    Join Date
    Nov 2009
    Location
    Canada
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up It Works

    OK, I got it working:

    Code:
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/intranet([/\w\.-]*)*\/?$ [NC]
    RewriteRule .* http://www.custompopups.com
    This redirects everything except visits to the intranet subdirectory. I also see that slashes didn't need to be escaped. Thanks for the tip.

    Do you think the "RewriteEngine on" directive is necessary?

    Thanks,
    Shane.
    PHP version 5.2.9
    Apache/2.0.54 (Debian GNU/Linux 4)

  4. #4
    Do. Or do not. There is no try silver trophy
    SitePoint Award Recipient ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,347
    Mentioned
    87 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Shanester View Post
    Do you think the "RewriteEngine on" directive is necessary?
    Yup. Without the RewriteRule doesn't work at all
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •