Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

advanced htaccess redirect

how to create redirect from one domain to another with adding extensions



5:41 pm on Feb 8, 2013 (gmt 0)

10+ Year Member

Hi there,

I have a website that merged with another, i have setup a wildcard redirect to it, however, I need to add .html extension at the end of the URLs under one directory. So for example:

Website A needs to move and merge into website B.

Website A www.a.com
Website B www.b.com

The wildcard redirect I have put is:

RewriteCond %{HTTP_HOST} ^property-www.a.com/$
RewriteRule ^(.*)$ http://www.b.com\/$1 [R=301,NC,L]

This is working perfectly, however, on Website A all files under one of the of the directories does not end with an extension, and on the new website it needs to have extension.

So all pages under www.a.com/directory/something-here/ need to redirect to www.b.com/directory/something-here.html

What should I add to the htaccess file in addition to the code above?

Thank you in advance.



8:53 pm on Feb 8, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

It's not a "what" question, simply a "where" question.

Put the directory-specific rule before the catch-all rule. This in fact is a basic principle for any redirect or rewrite: order your rules from most specific to most general.

Incidentally, the anchors ^(.*)$ in your rule aren't necessary. They won't do any harm, but they have no effect. By default, Regular Expressions begin capturing as soon as they can and continue as long as they can.


5:12 pm on Feb 11, 2013 (gmt 0)

10+ Year Member

HI Lucy, thank you for this, very helpful.

My main problem is with adding the ".html" at the end. currently what happes is it redirect to the new domain and adds the ".html" but after a slash, so the result is www.b.com/directory/something-here/.html (note the /.html)

My current code is

RewriteEngine on

RewriteOptions inherit

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^directory?(.*)$ "http\:\/\/www\.b\.com\/directory\/$1\.html" [R=301,L]
RewriteRule ^(.*)$ "http\:\/\/www\.b\.com\/$1" [R=301,L]

Please let me know how such issue can be resolved.



9:12 pm on Feb 11, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

The RewriteCond is both meaningless and unnecessary, since it means "the host is anything or nothing". That about covers all possibilities doesn't it ;)

Leave a blank line after each RewriteRule. Not for Apache's sake but for your own; among other things it reminds you that a RewriteCond applies only to the immediately following rule.

The following are unnecessary:
-- quotation marks in the target
-- all escapes in the target (some characters have to be escaped in patterns and conditions, but never in targets)
-- escaped colon (NEVER necessary in Apache afaik, and probably not in any other RegEx dialect)
-- escaped slashes (necessary in those Apache mods and programming languages that use / itself as a RegEx delimiter, otherwise not needed)
-- as previously noted, the closing anchor in both patterns, and the opening anchor in the second pattern

Aside from that, the first rule as written
RewriteRule ^directory?(.*) http://www.b.com/directory/$1.html


Request for
optionally followed by more stuff-- meaning that the pattern could have said "director(.*)" except that this isn't what you meant anyway --will be redirected to



is captured as
which redirects to


is captured as
which redirects to


is captured as
which redirects to

In short: It's adding the extra slash because you told it to. The obvious fix is to include the slash in your capture as


It's .+ rather than .* because if the request is for directory/ or directory (no slash) alone, mod_dir steps in and you don't need to do anything on your own. The closing anchor becomes necessary the moment you add the [^.] meaning "don't do this if the request already contains a literal period". It's to prevent the RegEx from stopping halfway and pretending it hasn't seen the upcoming period.

Which reminds me that you will need a preceding RewriteCond to deal with requests for /directory/index.html. There should not be any, of course, but you have to code for them. If nothing else, search engines will sometimes ask for this file just to see what will happen. The exact format of the Condition will depend on your existing index file name.

Featured Threads

Hot Threads This Week

Hot Threads This Month