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

    
Can I Combine Rewrite Rules For Multiple Domains And Trailing Slashes
Taryn Garwell




msg:4641943
 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]

 

g1smd




msg:4641948
 8:15 pm on Feb 2, 2014 (gmt 0)

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.
lucy24




msg:4641977
 10:30 pm on Feb 2, 2014 (gmt 0)

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.

Taryn Garwell




msg:4642147
 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.




lucy24




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

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.)

Taryn Garwell




msg:4642180
 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)?$




phranque




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

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.

lucy24




msg:4642221
 11:54 pm on Feb 3, 2014 (gmt 0)

i think lucy24 meant leave off the second condition.

I think you are right. Please feel free to wield the moderatorial scissors.

Taryn Garwell




msg:4642226
 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".


lucy24




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

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.

g1smd




msg:4642243
 2:24 am on Feb 4, 2014 (gmt 0)

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

Taryn Garwell




msg:4642404
 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!

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