SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Mad Beach Bum jgsketch's Avatar
    Join Date
    Aug 2002
    Location
    FL
    Posts
    825
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Only one of many redirects is working

    I have a set of redirects in my .htaccess file. Only the first one works and then it appears that the rest are skipped over. So my first thought was the [L] flag. I removed that from the first rewrite, but it had no affect. I feel like there is something in there that I need to take away. But trial and error is not working out. Here is my set of rewrite rules.

    Code:
    # Rewrites
    RewriteEngine on 
    # Canonical Rewrite
    RewriteCond %{HTTP_HOST} ^abc.com [NC] 
    RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R=301] 
    # Canonical Rewrite
    RewriteCond %{HTTP_HOST} ^http://www.abc.com/123.html/ [NC] 
    RewriteRule ^(.*)$ http://www.abc.com/123.html$1 [L,R=301] 
    # Canonical Rewrite
    RewriteCond %{HTTP_HOST} ^http://www.abc.com/456.html [NC] 
    RewriteRule ^(.*)$ http://www.abc.com/456/index.html$1 [L,R=301] 
    # Canonical Rewrite
    RewriteCond %{HTTP_HOST} ^http://www.abc.com/bbb/ [NC] 
    RewriteRule ^(.*)$ http://www.abc.com/bbb/index.html$1 [L,R=301]

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    jg,

    Normally, I'd ask the difference between (.*) and (.*) but your code differentiates via RewriteCond statements within the block statements.

    The Last flag is required to terminate EACH block statement. If not used, then the successive block statements are ANDed and, with each RewriteCond being mutually exclusive, the second block can NEVER be matched.

    The problem with your code is the use of the protocol in the RewriteCond statements (except the first). In other words, you hit the nail on the head: The first block was correct and the others need to have the http:// removed (and the dots escaped).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    Mad Beach Bum jgsketch's Avatar
    Join Date
    Aug 2002
    Location
    FL
    Posts
    825
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help. I tried as you suggested. However it did not work, so i assume I must have the syntax wrong. So I tried a couple other variations but with no luck.

    I am confused as to why I would need to remove the http:// from the other redirects. And does this also apply to the RewriteRule line or just the RewriteCond line?

    Here is the reworked code:
    Code:
    RewriteCond %{HTTP_HOST} ^www\.abc.com/123\.html/ [NC] 
    RewriteRule ^(.*)$ http://www.abc.com/123.html$1 [L,R=301]

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by jgsketch View Post
    Thanks for your help. I tried as you suggested. However it did not work, so i assume I must have the syntax wrong. So I tried a couple other variations but with no luck.

    I am confused as to why I would need to remove the http:// from the other redirects. And does this also apply to the RewriteRule line or just the RewriteCond line?

    Here is the reworked code:
    Code:
    RewriteCond %{HTTP_HOST} ^www\.abc\.com/123\.html/ [NC] 
    RewriteRule ^(.*)$ http://www.abc.com/123.html$1 [L,R=301]
    Using my color coding, you've forgotten to escape the one dot character in the regex for {HTTP_HOST} and you don't understand that the {HTTP_HOST} ONLY contains the domain name, not the / nor the path/filename - that is the error.
    Quote Originally Posted by jgsketch View Post
    Code:
    # Rewrites
    RewriteEngine on 
    # Canonical Rewrite
    RewriteCond %{HTTP_HOST} ^abc\.com [NC] 
    RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R=301] 
    # etc.
    # You can NOT use the /path/file in {HTTP_HOST} and expect to get a match - it will NEVER happen!
    Okay, I was right the first time: Over reliance on the EVERYTHING atom (because you just don't understand regex). PLEASE have a read of the tutorial linked in my signature!

    Then try (after removing the /'s following .html in your links):
    Code:
    # Rewrites
    RewriteEngine on 
    # Canonical Rewrite
    RewriteCond %{HTTP_HOST} ^abc\.com$ [NC] 
    RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R=301]
    # there will be NO www.abc.com's left!
    
    RewriteEngine off
    # Because the rest of your code makes no sense to me
    # AND will generate loops in the mod_rewrite!
    # Please explain what it is that you THINK
    # you're trying to accomplish.
    
    # Canonical Rewrite
    RewriteRule ^123\.html$ http://www.abc.com/123.html [L,R=301] 
    # Canonical Rewrite
    RewriteCond %{HTTP_HOST} ^http://www.abc.com/456.html [NC] 
    RewriteRule ^(.*)$ http://www.abc.com/456/index.html$1 [L,R=301] 
    # Canonical Rewrite
    RewriteCond %{HTTP_HOST} ^http://www.abc.com/bbb/ [NC] 
    RewriteRule ^(.*)$ http://www.abc.com/bbb/index.html$1 [L,R=301]
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    Mad Beach Bum jgsketch's Avatar
    Join Date
    Aug 2002
    Location
    FL
    Posts
    825
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm still trying to learn regex. It's a little harder read.

    I've dropped that code for something that was a little easier to understand and works the way i want it to. Since I'm looking for an exact match and everything is within the same domain, I tried using RedirectMatch instead. I was able to use only one line of code for each item. Thanks.


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
  •