将非www重定向到子目录中的www

时间:2013-03-13 21:38:07

标签: .htaccess mod-rewrite

我在http://www.mysite.com/test/中有一个.htaccess文件,我试图将http://mysite.com/test/访问http://www.mysite.com/test/的任何人发送到{{3}},但这不起作用,这是我的代码:

# Force Caching
Header unset Pragma
FileETag None
Header unset ETag
<FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf|pdf|flv|mp3)$">
<IfModule mod_expires.c>
 ExpiresActive on
 ExpiresDefault "access plus 14 days"
 Header set Cache-Control "public"
</IfModule>
</FilesMatch>
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
 Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>

# Enable gzip compression
<IfModule mod_mime.c>
 AddType application/x-javascript .js
 AddType text/css .css
</IfModule>
<IfModule mod_deflate.c>
 AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/javascript
 <IfModule mod_setenvif.c>
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
 </IfModule>
 <IfModule mod_headers.c>
  Header append Vary User-Agent env=!dont-vary
 </IfModule>
</IfModule>


# Main Rewrite rules
<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /

    # Always use www.
    RewriteCond %{HTTP_HOST} !^www [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]

    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:1)

.htaccess文件位于/test目录中。这就是/test/的请求网址格式RewriteRule中删除.*前缀的原因。要正确重定向请求,您必须将替换更改为

RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]