簡體   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