开发者

Redirecting domain.com to www.domain.com

开发者 https://www.devze.com 2023-03-15 10:19 出处:网络
I want to redirect all requests to http://domain.com to http://www.domain.com, hence I use the following .htacce开发者_开发百科ss placed in the root of my site:

I want to redirect all requests to http://domain.com to http://www.domain.com, hence I use the following .htacce开发者_开发百科ss 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


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.


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]
0

精彩评论

暂无评论...
验证码 换一张
取 消