homepage Welcome to WebmasterWorld Guest from 54.167.182.201
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

    
Rewriting .net domain to .com domain
How to use mod_rewrite to do a 301 redirect from your .net domain to .com
lobo235

10+ Year Member



 
Msg#: 3898696 posted 3:28 pm on Apr 23, 2009 (gmt 0)

I decided to purchase the .net version of my domain to go with the .com since godaddy is having a .99 sale on new domains. My site is setup as a virtual host and I have the following rewrite rule that doesn't seem to apply to the .net domain for some reason. Any help is appreciated.

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

It looks like this should work to me. The way I understand this is that when the HTTP_HOST does not match www.example.com for this virtual host it will do a 301 redirect to the canonical domain. I added www.example.net and example.net each as a ServerAlias for the virtual host.

 

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3898696 posted 3:46 pm on Apr 23, 2009 (gmt 0)

Nearly! Add one more $ to the code.

RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Since RewriteBase / is the default, it can be omitted.

lobo235

10+ Year Member



 
Msg#: 3898696 posted 3:56 pm on Apr 23, 2009 (gmt 0)

Hmmm, I tried adding the $ at the end like you mentioned but it's still not working for some reason. Is it because I setup the VirtualHost with the .net domain as a ServerAlias? Is there a better way to tie multiple domains to the same VirtualHost?

Thanks for your help.

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3898696 posted 3:10 pm on Apr 24, 2009 (gmt 0)

Is the ".net domain" hosted as an "add-on" domain? That is, did you or your host set it up using a Control Panel "add-on domain" function?

If so, then it's likely that the control panel configured the server to put the .net domain's files into a subdirectory of your main site's filespace. If so, then you'll have to put redirect code into a .htaccess file in that subdirectory.

Part of .htaccess implementation is "what code is in it." The other part is, "Where is this .htaccess file located?" The code may be 100% correct, but if it's in a file outside of the scope of the directory tree accessed by the current HTTP request, then that code won't get executed.

Jim

lobo235

10+ Year Member



 
Msg#: 3898696 posted 4:16 pm on Apr 24, 2009 (gmt 0)

I run my own server and I use Webmin to add new VirtualHosts. Here is the config for this VirtualHost. Maybe it will help with troubleshooting:

<VirtualHost *:80>
DocumentRoot "/var/www/sites/example/www"
ServerName www.example.com
ServerAlias example.com example.net www.example.net
<Directory "/var/www/html/sites/example/www/">
AllowOverride None
Options All -Indexes
AddType application/x-httpd-php .php .php4 .php3 .phtml .html .htm
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml text/javascript application/x-javascript application/json
Header append Vary Accept-Encoding

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^archive_(.*)_(.*).html$ archive.php?y=$1&m=$2
RewriteRule ^(.*)_category.html$ category.php?catname=$1
RewriteRule ^comments/(.*) comments.php?page_name=$1
RewriteRule ^directory/(.*) dir/directory.php?dname=$1

ErrorDocument 404 /404.php
</Directory>
</VirtualHost>

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3898696 posted 5:02 pm on Apr 24, 2009 (gmt 0)

In a server config context, you'll need to prepend a slash to all URL-path patterns. I.e. " RewriteRule (.*) http://www.example.com/$1 [R=301,L]" becomes "RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]" and "RewriteRule ^comments/(.*) comments.php?page_name=$1" becomes "RewriteRule ^/comments/(.*) comments.php?page_name=$1".

Also, look into using a more-specific pattern whenever possible to replace ".*" -- and especially when more than one ".*" is used in a pattern (this can cause performance problems due to all of the back-off-and-retry pattern-matching attempts that it causes).

For example, "RewriteRule ^archive_(.*)_(.*).html$ archive.php?y=$1&m=$2" could be better implemented as "RewriteRule ^archive_([^_]+)_([^.]+)\.html$ archive.php?y=$1&m=$2" in most cases. Here we use a negative-match to say, "stop matching when you find an underscore," and "Stop matching when you find a period." Therefore, the URL-path can be matched to the pattern in a single left-to-right pass.

Jim

lobo235

10+ Year Member



 
Msg#: 3898696 posted 6:16 pm on Apr 24, 2009 (gmt 0)

This is what I did in order to get it working finally:

<VirtualHost *:80>
DocumentRoot "/var/www/sites/example/www/"
ServerName www.example.com
ServerAlias example.com example.net www.example.net
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]
<Directory "/var/www/html/sites/example/www/">
AllowOverride None
Options All -Indexes
AddType application/x-httpd-php .php .php4 .php3 .phtml .html .htm
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml text/javascript application/x-javascript application/json
Header append Vary Accept-Encoding
RewriteRule ^(.*)_category\.html$ category.php?catname=$1 [L]
RewriteRule ^archive_([^_]+)_([^.]+)\.html$ archive.php?y=$1&m=$2 [L]
RewriteRule ^comments/(.*) comments.php?page_name=$1 [L]
RewriteRule ^directory/(.*) dir/directory.php?dname=$1 [L]
ErrorDocument 404 /404.php
</Directory>
</VirtualHost>

So it looks like the canonical domain rewriting needed to happen in the VirtualHost section instead of the Directory section.

As far as your other suggestion about prefixing the rules with the / because this is in a server config context, I couldn't get it to work that way. When I put the leading / in I would get 400 bad requests for all those requests. I am not sure why it's working this way (maybe due to the fact that I have a trailing slash already on my DocumentRoot and my Directory?

Caterham

10+ Year Member



 
Msg#: 3898696 posted 6:53 pm on Apr 24, 2009 (gmt 0)

If you're looking for performance: avoid regular expressions, if possible. You could substitute

RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} !^$

with the string comparison function

RewriteCond %{HTTP_HOST} !=www.example.com
RewriteCond %{HTTP_HOST} !=""

When I put the leading / in I would get 400 bad requests for all those requests

Did you prefix the pattern or pattern and substitution?

As far as your other suggestion about prefixing the rules with the / because this is in a server config context, I couldn't get it to work that way.

Your rules
RewriteRule ^(.*)_category\.html$ category.php?catname=$1 [L]
RewriteRule ^archive_([^_]+)_([^.]+)\.html$ archive.php?y=$1&m=$2 [L]
RewriteRule ^comments/(.*) comments.php?page_name=$1 [L]
RewriteRule ^directory/(.*) dir/directory.php?dname=$1 [L]

are currently in directory context, not server context.

Options All -Indexes

Don't mix non-+/- and +/- options [issues.apache.org], this is considered to be unsupported syntax.

Use
Options All
Options -Indexes

instead.

AddType application/x-httpd-php .php .php4 .php3 .phtml .html .htm

Use AddHandler to invoke handler processing and not the AddType myth [theregister.co.uk].

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3898696 posted 7:00 pm on Apr 24, 2009 (gmt 0)

***
DocumentRoot "/var/www/sites/example/www[b]/[/b]"

The manual explicitly warns to omit the trailing slash. You now see why, I think.

lobo235

10+ Year Member



 
Msg#: 3898696 posted 10:45 pm on Apr 24, 2009 (gmt 0)

Thank you for all the great suggestions. I had been running this configuration for months and didn't realize it had problems until I tried adding the .net domain.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3898696 posted 11:28 pm on Apr 24, 2009 (gmt 0)

It is problems like this that can chip away at your sites indexing and ranking and give you little in the way of clues to help you fix it.

lobo235

10+ Year Member



 
Msg#: 3898696 posted 6:48 pm on Apr 27, 2009 (gmt 0)

For some reason the following rule is matching stuff I don't want it to. I cannot figure out why it is working this way.

RewriteRule ^(.*)_category\.html$ category.php?catname=$1 [L]

This will match requests to widget_category.html and requests to widget_categoryxhtml

I thought that by prefixing the period with a backslash in my RewriteRule it would escape the period so it would look for an actual period and not just any character like it appears to be doing now.

Any ideas?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3898696 posted 8:38 pm on Apr 27, 2009 (gmt 0)

Is it another rule that matches instead? That one should not, notwithstanding the usual reminder to flush browser cache before testing...

Again, I would replace (.*) with something more specific here, like ([^_]+) if there are no other underscores within it to match.

lobo235

10+ Year Member



 
Msg#: 3898696 posted 10:15 pm on Apr 27, 2009 (gmt 0)

I finally figured out what was going on. I had some old .htaccess files laying around in different directories that were interfering with rules I was trying to create in the virtual host config. After I removed the rewrite rules from those .htaccess files everything started behaving the way you guys were telling me it should (I had to add the leading slashes).

Thanks for the tip about replacing the (.*) with something else. Unfortunately some of the category names have underscores in them so I can't change that rule just yet. I haven't noticed any performance problems but I will keep your advice in mind when I create new sites/pages so that I could name the pages something like a-long-name_category.html.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3898696 posted 10:42 pm on Apr 27, 2009 (gmt 0)

For multiple underscores, this would work:

^(([^_]+)_)+category\.html$

but you would need to strip the final _ off the pattern copied in $1 before using it.

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3898696 posted 1:17 am on Apr 28, 2009 (gmt 0)

It's probable that ^(([^_]+_)+)category\.html$ was intended.

You could also go with ^([^_]+(_[^_.]+)*)_category\.html$ to solve the trailing-underscore problem.

However, given that the "_category.html" tail is fairly short and that there is only one ".*" sub-pattern in the pattern here, it may not be worth the bother.

Jim

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3898696 posted 7:03 pm on Apr 28, 2009 (gmt 0)

Err, yes, that's what I meant - but at close to midnight not all cylinders were firing.

^(([^_]+_)+)category\.html$

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