繁体   English   中英

如果没有www前缀,则htaccess将所有URL重定向到带有www前缀的新URL

[英]htaccess redirect all url if they have no www prefix to a new url with www prefix

我在HTACCESS中有这个。

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

这有效,但是如果该域具有某些指示,则该域不起作用。

A:    site.com/users/profiles/index.php?id=1

B:    site.com/index.php?id=1

“ A”重定向失败,并且按原样返回,而“ B”重定向到www.site.com/index.phpid=1

这是site.com/user/中htaccess的内容

Options -Indexes +FollowSymLinks
RewriteEngine On
###########music#######
RewriteCond %{THE_REQUEST} \ /+index\.php\?artist=([^&]+)&title=([^&]+)&a=([^&\ ]+)
RewriteRule ^ /audio/%1/%2/%3? [L,R=301]
RewriteCond %{THE_REQUEST} \ /+audio\.php\?artist=([^&]+)&title=([^&\ ]+)
RewriteRule ^ /audio/%1/%2? [L,R=301]
#################internallly redirect////////////
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?artist=$1&title=$2&a=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^audio/([^/]+)/([^/]+)/([^/]+)$ index.php?artist=$1&title=$2&a=$3 [L,QSA]
###############gzip######
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/png A2592000
ExpiresByType image/x-icon A2592000
#ExpiresByType text/css A86400
#ExpiresByType text/javascript A86400
ExpiresByType application/x-shockwave-flash A2592000
#
<FilesMatch "\.(gif¦jpe?g¦png¦ico¦swf)$">
Header set Cache-Control "public"
</FilesMatch>
AddType text/cache-manifest appcache

我该如何修改才能全部重定向?

由于您在/users/目录中也有.htaccess,因此对于包含/users/的请求,父目录中的任何.htaccess都不会生效。

/users/.htaccess中的RewriteEngine On行下面插入此规则:

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

PS:有一种方法可以从子.htaccess继承父重写规则,但是这些规则是在应用当前.htaccess之后执行的,这可能会导致错误的结果,因为您还要内部重写某些URI并更改%{REQUEST_URI}值。

参考文献:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM