homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Where have I gone wrong with my redirect?
.htaccess catch-all not working...

 11:41 am on Mar 15, 2014 (gmt 0)

Hi all,

I have a fairly complicated .htaccess file in my root, which controls a number of domains, amongst other things...

Here is the domain handling section:

#If visiting demo1.com, redirect elsewhere.
RewriteCond %{HTTP_HOST} ^(www\.)?demo1\.com
RewriteRule (.*) http://example.com/somewhere/$1 [R=301,L]
# If visiting one of these URLs, don't redirect.
RewriteCond %{HTTP_HOST} !^((www\.)?demo2\.com\.?(:[0-9]+)?)?$
RewriteCond %{HTTP_HOST} !^((www\.)?demo3\.com\.?(:[0-9]+)?)?$
RewriteCond %{HTTP_HOST} !^((www\.)?demo4\.com\.?(:[0-9]+)?)?$
# Don't redirect calls to files in this folder.
RewriteCond %{REQUEST_URI} !^/(_folder)/
# Don't redirect calls to these files.
RewriteCond $1 !^(page\.php|page\.html)$
# RewriteCond $1 !^(page\.php|sitemap\.xml|sitemap\.xml\.gz|robots\.txt|page\.html)$
# Send everything else here...
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]

For the most part, it's working.

However, if I do something like ([url]www.)example.com/test(/)[/url] or ([url]www.)test.example.com[/url], both of which are non-existant URLs, it doesn't redirect you to example.com.

Where am I going wrong, please?

Thank you for your time.



 6:49 pm on Apr 5, 2014 (gmt 0)

Did you ever get this sorted? Also, why even check for this if you are not going to redirect? Just leave this section out.

# If visiting one of these URLs, don't redirect. 
RewriteCond %{HTTP_HOST} !^((www\.)?demo2\.com\.?(:[0-9]+)?)?$
RewriteCond %{HTTP_HOST} !^((www\.)?demo3\.com\.?(:[0-9]+)?)?$
RewriteCond %{HTTP_HOST} !^((www\.)?demo4\.com\.?(:[0-9]+)?)?$


 8:24 pm on Apr 5, 2014 (gmt 0)

Heh, I was about to comment on the same lines but from a different angle. The conditions obviously have to be present-- but why such detail? All you need is a single

RewriteCond %{HTTP_HOST} !\b(demo2|demo3|demo4)\.com

without anchors. And you only need the leading \b if you've got overlapping domain names on the same server so you need to watch for false positives.

Incidentally: commenting is useful. But commenting in mid-rule may make things harder to read. Rearranging the originally quoted rule, it seems to say:

RewriteCond %{REQUEST_URI} !^/(page\.php|page\.html)$
RewriteCond %{REQUEST_URI} !^/_folder/
RewriteCond %{HTTP_HOST} !(demo2|demo3|demo4)\.com
RewriteRule (.*) http://sub.example.com/$1 [R=301,L]

That's assuming you left out the / before (page\.php|page\.html), but that the ^ is intentional. In fact the two exclusions can be combined as
RewriteCond %{REQUEST_URI} !^/(folder|page\.php|page\.html)
leaving off the closing anchor. This leaves you with just two conditions instead of five. No point to the $1 in the condition, since you can't possibly be dealing with internal rewrites at this stage.


 11:21 am on Apr 7, 2014 (gmt 0)

Hi coopster and lucy,

I've finally had some time to sit down and slowly go through this. After so long, this stuff still puzzles me greatly.

Many thanks for the tips. I've wittled down my code substantially.

RewriteCond %{HTTP_HOST} ^(www\.)?demo1\.za\.org
RewriteRule (.*) http://sub.domain.com/cat/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !\b(demo2|demo3|demo4)\.(za.net|za.org|co.za|com|net)$
RewriteCond %{REQUEST_URI} !^/(_folder|page\.php|anotherpage\.html)
RewriteRule (.*)$ http://sub.domain.com/$1 [R=301,L]

It all seems to be working, but my original problem remains...

If someone visits(www.)example.com/test(/) or (sub|www).test.example.com, both of which are non-existant URLs, it doesn't redirect you to domain.com. :(


 4:15 pm on Apr 7, 2014 (gmt 0)

Can you please put a blank line between your two rulesets? This is purely for the reader's sanity; blank lines have no meaning in htaccess. (They are just about the only thing that doesn't!)

This is admittedly tricky to illustrate within the confines of "example dot something". But I do think you don't need this bit:
RewriteCond %{HTTP_HOST} ^(www\.)?demo1\.za\.org
unless there are subdomains of "demo1" that you need to exclude? Otherwise it's just
without opening anchor.

In this group
RewriteCond %{HTTP_HOST} !\b(demo2|demo3|demo4)\.(za.net|za.org|co.za|com|net)$
just how many domains are on your server? You can leave off the whole tld-plus-closing-anchor part unless there are other domains, like maybe another .org, that you need to exclude from the rule.

It all seems to be working

What, exactly, is "working"? What types of requests are getting successfully redirected at this point?

Do you have wildcard subdomains, so any request for blahblah.example.com will arrive at your server? You can't do anything about requests that don't reach the server in the first place. That is, you can do something about them, but not within htaccess.


 5:51 pm on Apr 7, 2014 (gmt 0)

Many thanks, Lucy.

I have trimmed my RewriteCond as shown and have learnt yet another trick. :)

I have 5 domains pointing to my server. Some are active, some are parked, some redirect, etc.

I don't use all of the TLDs, but I have added all of the main ones just as a precaution.

Lastly, I've just read up on wildcard domains and DNS and have learnt something again. Consider me educated on the matter.

I am indebted.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved