htaccess www到非www的子文件夹

时间:2015-06-09 09:50:59

标签: .htaccess mod-rewrite redirect

我有一个最近被问到的问题(但我找不到合适的解决方案)

我正在努力实现我之前可以使用www和非www显示的域名仅显示非www。

示例:http://www.domain.tld/folder1http://domain.tld/folder1可以打开并显示相同的结果。

我想只有非www可用。换句话说,我确实找到了解决方案

RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

像魅力一样工作。有什么方法可以以某种方式交换它吗?我总是非www而不是www?

我已经尝试过12种不同的htaccess代码,但不适用于我。最后一个

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

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

甚至没有将我重定向到http://www.domain.tld/folder1/,而是我收到错误“在此服务器上找不到请求的URL / folder1 /。”

也许我只是过于关注那个我监督解决方案的问题,所以希望有人可以捅我的鼻子:)

请求完整的htaccess:

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

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

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

# 480 weeks
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=290304000, public"
</FilesMatch>

# 2 DAYS
<FilesMatch "\.(xml|txt)$">
Header set Cache-Control "max-age=172800, public, must-revalidate"
</FilesMatch>

# 2 HOURS
<FilesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>

<IfModule mod_deflate.c>
    <filesMatch "\.(js|css|html|php|jpg|jpeg)$">
        SetOutputFilter DEFLATE
    </filesMatch>
</IfModule>

0 个答案:

没有答案
相关问题