简体   繁体   English

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

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

I have this in HTACCESS. 我在HTACCESS中有这个。

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

This works but if the domain has some directry it doesnt work. 这有效,但是如果该域具有某些指示,则该域不起作用。

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

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

"A" fails to redirect and is returned as it is while "B" is redirected to www.site.com/index.phpid=1 “ A”重定向失败,并且按原样返回,而“ B”重定向到www.site.com/index.phpid=1

Here is the content of htaccess inside site.com/user/ 这是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

How do I modify this to redirect all? 我该如何修改才能全部重定向?

Since you have .htaccess in /users/ directory also therefore any .htaccess in parent directory is not in effect for a request containing /users/ . 由于您在/users/目录中也有.htaccess,因此对于包含/users/的请求,父目录中的任何.htaccess都不会生效。

Insert this rule just below RewriteEngine On line in /users/.htaccess : /users/.htaccess中的RewriteEngine On行下面插入此规则:

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

PS: There is way to inherit parent rewrite rules from child .htaccess but those rules are executed after application of current .htaccess , something that might give wrong results since you're also internally rewriting some URIs and changing value of %{REQUEST_URI} . PS:有一种方法可以从子.htaccess继承父重写规则,但是这些规则是在应用当前.htaccess之后执行的,这可能会导致错误的结果,因为您还要内部重写某些URI并更改%{REQUEST_URI}值。

References: 参考文献:

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

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