2016-05-13 143 views
0

好了,我想重定向所有WWW非WWW链接,WWW重定向到非WWW与子文件夹和链接

我想www.example.com重定向到example.com,而我也可以这样做使用此代码。

RewriteEngine On 
RewriteBase/
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
RewriteRule ^(.*)$ https://%1/$1 [R=301,L] 

但当有人试图访问www.example.com/example,重定向的站点example.com,

我希望有一个代码,可以重定向www.example.com/example或任何子文件夹复制到example.com/example或任何子文件夹。

有点像通配符条目,该怎么做?

如果可能的话,也可以强制ssl。

谢谢。

+1

您的代码对我来说看起来没问题。尝试清除您的浏览器缓存 – starkeen

+0

也尝试了隐身,努力工作。 – Harsh

回答

1

如果我理解正确你的问题:

www.example.com/dir/anypage.html到example.com/dir/anypage.html(同一页)

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^www.example.com [nocase] 
RewriteRule ^(.*)   http://example.com/$1 [last,redirect=301] 

OR用途:

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

-------- ---------替代

RewriteEngine on 

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

RewriteCond %{HTTP_HOST} ^example\.com$ 
RewriteCond %{REQUEST_URI} !^/subfolder/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /subfolder/$1 [L] 

RewriteCond %{HTTP_HOST} ^example\.com$ 
RewriteRule ^(/)?$ /subfolder/index.php [L] 

要强制SSL:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L] 
+0

与之前不一样,/子文件夹不能重定向。它只会重定向到主页面。 – Harsh

+0

嘿@Harsh,试试这个:RewriteCond%{HTTP_HOST} ^(www \ .example \ .com)?$ RewriteRule ^(。*)$ http://example.com/subfoldername/$1 [R = 301,L ] – Lag

+0

我想要的是动态的,不仅对于单个子文件夹,而且对于所有人。 – Harsh