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

Redirect using rewritecond and rewriterule in httpd.conf
redirect www and non www to specific URL

 4:24 pm on Aug 15, 2013 (gmt 0)

I need to do a permanent rewrite to eg:

from both:


The change to be made in httpd.conf (Apache v2.2.11)

I am new to web servers & Apache so I have looked on-line and found that I need to use rewritecond and rewriterule to achieve the above. However I have not been able to achieve the redirect from both the above. Any help would be appreciated.

[edited by: phranque at 10:14 pm (utc) on Aug 15, 2013]
[edit reason] exemplified hostnames [/edit]



 5:05 pm on Aug 15, 2013 (gmt 0)

Do you need a redirect or a rewrite?

RewriteRule can be configured to do either of those things.

What code have you tried already?


 8:51 pm on Aug 15, 2013 (gmt 0)

I need to do a permanent rewrite

Well, there's no such thing as a temporary rewrite :) Or, more exactly, each rewrite is a separate act; the visitor doesn't know they're being rewritten, so permanent vs. temporary makes no difference. Ordinarily I'd assume you meant "redirect" (browser's address bar changes), but since you're talking about an inner directory in the same domain, that looks more like a rewrite (address bar doesn't change).

There's a bit of boilerplate that we post every now and then.

:: shuffling papers ::

Why We Make You Do It Yourself

There are plenty of forums where you can post a "how-to" question and get a fairly immediate answer. The answer may even be correct. But WebmasterWorld is about teaching you how to do it yourself. That way you can roll your own htaccess-- not just for today's problem but for tomorrow's almost identical one.

Here is the analogy:

Your child's room needs cleaning. You know that you can clean it yourself much faster and better than if you have to stand over your child and force him to do it right. But if you do the "make him get it right" part often enough, you will have raised a child who knows how to clean his room-- and who will some day stand glowering over his own children in the same circumstances.

Some day, someone else will post a question in the WebmasterWorld forums and you'll say "Hey, I know the answer to that one!"

Besides, when someone with a very low post count asks a question, that person may have posted the identical question in six different forums. They grab the first answer they get, and never even come back to the others. You can understand that this is infuriating for the person who took the time to compose an answer.

All of this is assuming that you really mean either a rewrite or a redirect. If it's your own server and you want the home page URL to point to a particular physical directory, you may actually be talking about aliasing.


 7:49 am on Aug 16, 2013 (gmt 0)

I have tried:

RewriteEngine On
# To redirect all traffic from http://example.com to http://www.example.com/example/Home.pgm
RewriteCond %{HTTP_HOST} !^www\.example\com\
RewriteRule ^(.*)$ http://www.example.com/example/Home.pgm/$1 [R=301,L]

to test just a single rewrite followed by:

RewriteEngine On
# To redirect all traffic from http://example.com to http://www.example.com/example/Home.pgm
RewriteCond %{HTTP_HOST} !^www\.example\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^(.*)$ http://www.example.com/example/Home.pgm/$1 [R=301,L]

The second attempt resulted in a loop. I have also tried other versions but I have not kept copies of all attempts made over the last few days.
I have been advised to perform a permanent redirect in order to enable SEO which is why, correctly or not, I opted to use the rewritecond / rewriterule.

[edited by: engine at 10:04 am (utc) on Aug 16, 2013]
[edit reason] please use example.com [/edit]


 9:14 pm on Aug 16, 2013 (gmt 0)

The second attempt resulted in a loop.

I should think so :)

The two conditions say:
If the host IS NOT www.example.com OR the host IS www.example.com -- in other words, always. This type of loop will be intercepted by the browser after some number of iterations.

Do you want to redirect all requests all the time, or only requests for the root? The second would be a single conditionless rule, like

RewriteRule ^/$ http://www.example.com/example/Home.pgm/ [R=301,L]

Repace ^/$ with ^$ if the rule is in a <Directory> section or htaccess.

Now, if you want to redirect all requests for

it can be done in mod_rewrite, but are you sure you're not trying to alias the domain to a different directory? If so, you wouldn't want to redirect at all.

I don't understand what your matched pair of Conditions in the second rule were intended to do. If your aim is a global redirect that doesn't go around in circles, you need a single RewriteCond that says

RewriteCond %{REQUEST_URI} !/example/Home.pgm/

All of this is assuming you only have one domain on the server. If there's more than one, each domain-specific RewriteRule needs a condition in the form

RewriteCond %{HTTP_HOST} example\.com

without anchors. If there are multiple conditions, this one goes first.

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