pandronic
pandronic

Reputation: 651

Redirecting domain.com to www.domain.com

I want to redirect all requests to http://domain.com to http://www.domain.com, hence I use the following .htaccess placed in the root of my site:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]

This works very nice, except in subfolders where I do some more URL rewriting:

RewriteEngine on
RewriteBase /articles/
RewriteRule ^(.+)$ index.php [L]

In fact nothing happens if I enter the URL: http://domain.com/articles/article.html Expected behavior would be a redirect to http://www.domain.com/articles/article.html

What would be the best way to achieve the result?

Thanks

Upvotes: 0

Views: 150

Answers (2)

LazyOne
LazyOne

Reputation: 165118

You need to use RewriteOptions directive to tell Apache to use .htaccess from parent folder after finishing processing rules from local .htaccess:

RewriteOptions inherit

But parent rules will be process AFTER local .. which you may not like (as result URL may look like http://www.domain.com/articles/index.php which will be visible in address bar).

Ideally you want to move all into single .htaccess -- if you can.

Upvotes: 4

Tomgrohl
Tomgrohl

Reputation: 1767

I would have thought this should work, if you have it in the sub folder:

RewriteBase /articles/
RewriteCond %{http_host} ^website\.com [nc]
RewriteRule ^(.*)$ http://www.website.com/articles/$1 [R=301,NC]

Upvotes: 0

Related Questions