Welcome to WebmasterWorld Guest from 107.22.7.35

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Can I Combine Rewrite Rules For Multiple Domains And Trailing Slashes

     
6:39 pm on Feb 2, 2014 (gmt 0)



I have two domains pointing to the same root directory, example.com and example.net.

I want example.com, example.net, and www.example.net to redirect to www.example.com/.

I also want to use trailing slashes on all directory requests.

My website is hosted on a shared server, so I can only use an .htaccess file to configure settings.

These are two sets of rules I have come up with, but I'm not sure if I'm using the most efficient way to accomplish the two tasks. I will appreciate hearing from anyone who knows a better way, as I am still a newbie when it comes to Apache. Thanks.

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


RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [R=301,L]
8:15 pm on Feb 2, 2014 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Many changes:

Test for NOT www.example.com hostname request.
Escape literal periods.
Remove [NC] flag.
Cater for HTTP/1.0 requests.
Remove ^ and $.

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


The
DirectorySlash
directive should take of the other problem.
10:30 pm on Feb 2, 2014 (gmt 0)

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



RewriteBase /

You don't need this line at all, and you certainly don't need it twice. It's a once-and-for-all directive, placed immediately after RewriteEngine On. The reason you don't need it at all is that a RewriteBase only kicks in when the target of a rule doesn't start in either / (for domain root) or http: et cetera. And this will never happen, because you won't allow it to happen.

Remove ^ and $

He meant to say: remove them from the pattern in the body of the rule ;) By default, regular expressions start as soon as they can and go on as long as you can. You do need anchors in the condition, as shown, because you're matching exact text.

The DirectorySlash directive should take of the other problem.

"What DirectorySlash directive where?" No worries: Apache does this by default. It's one of two jobs done by mod_dir; you don't need to give any special instructions.
5:40 pm on Feb 3, 2014 (gmt 0)




@g1smd and @lucy24, thanks for your help. If I understand correctly, I can use the following lines to redirect all requests to www.example.com/:
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteCond %{HTTP_HOST} !^(www\.example\.net)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

I now notice that Apache does add trailing slashes by default. Thanks for pointing that out.

The help is much appreciated, thanks again.



7:54 pm on Feb 3, 2014 (gmt 0)

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



Awk! Leave off the first condition. You did say you want to redirect any dot net to dot com, right? Then all you need is the second condition, the one that says "If the hostname is anything other than exactly such-and-such or exactly nothing". (The "exactly nothing" part is for HTTP/1.0 requests. Mainly robots but some proxies.)
8:19 pm on Feb 3, 2014 (gmt 0)




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

Thanks for your quick response. I am trying to learn how to use apache directives, but it is a bit confusing at times. I also like less coding to get what I need.

Now that the .net redirects to .com, do I need a condition that sends example.com to www.example.com? Such as
RewriteCond %{HTTP_HOST} !^(\.example\.com)?$



8:46 pm on Feb 3, 2014 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, Taryn Garwell!


Awk! Leave off the first condition.

i think lucy24 meant leave off the second condition.
i would go with what g1smd provided:
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

do I need a condition that sends example.com to www.example.com?

this is covered by g1smd's code.
the condition says not exactly "www.example.com" or not exactly nothing.
11:54 pm on Feb 3, 2014 (gmt 0)

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



i think lucy24 meant leave off the second condition.

I think you are right. Please feel free to wield the moderatorial scissors.
12:20 am on Feb 4, 2014 (gmt 0)




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

Just to clarify, these two lines from g1smd will redirect
dot com and
dot net and
www dot net
to www dot com because of the ! and ? that says
"If the hostname is anything other than exactly such-and-such or exactly nothing".

1:04 am on Feb 4, 2014 (gmt 0)

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



Exactly. :)

Now, the rule can't do anything about humans with HTTP 1.0 connections who type in example dot net, because that still counts as "exactly nothing". But you can't do everything.
2:24 am on Feb 4, 2014 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Yes. Using the "!" for NOT, any hostname request that is not exactly www.example.com will be redirected, e.g.

www.example.org
example.com
www.example.com:8080
etc
6:34 pm on Feb 4, 2014 (gmt 0)



Thanks to all of you for your help and expertise. Learning to work with Apache has been the hardest part for me in setting up a website, but I am determined to get through it.:) Have a great day!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month