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.


# 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] 

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.

Okay, I was right the first time: Over reliance on the :kaioken: EVERYTHING :kaioken: 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):

# 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!

[COLOR="Red"]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.[/COLOR]

[COLOR="Gray"]# 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][/COLOR]

Regards,

DK

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.

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

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:


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