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

Apache Web Server Forum

    
advanced htaccess redirect
how to create redirect from one domain to another with adding extensions
qandil42




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

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.

Regards,

 

lucy24




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

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.

qandil42




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

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.

Thanks

lucy24




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

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
means:

Request for
www.example.com/directory
or
www.example.com/director
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

http://www.example.com/{stuff-that-was-captured}.html

So:

olddirectory/
is captured as
olddirectory(/)
which redirects to
http://www.example.com/newdirectory//.html

and

olddirectory/filename
is captured as
olddirectory(/filename)
which redirects to
http://www.example.com/newdirectory//filename.html

and

olddirectory/filename.xtn
is captured as
olddirectory(/filename.xtn)
which redirects to
http://www.example.com/newdirectory//filename.xtn.html

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

^directory(/[^.]+)$

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.

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