【问题标题】:Redirect non-www to www in subdirectory将非 www 重定向到子目录中的 www
【发布时间】:2013-03-13 21:38:06
【问题描述】:

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

# 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>

关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

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

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

    【讨论】:

    • 仍然不起作用,实际上没有发生重定向。每当我访问mysite.com/test 时,它只会加载没有重定向的站点。
    • @spyke01 它适用于我的测试环境。您在此或任何其他 .htacces 中是否有其他规则?
    • 我用完整的 .htaccess 编辑了上面的帖子。主目录下可能有.htaccess文件,但我必须等待访问。
    • @spyke01 其他指令与重写无关。所以,他们应该没有问题。我暂时将 .htaccess 移到了根目录,即使在那里它也会从 mysite.com 重定向到 www.mysite.com。抱歉,那帮不了你了。
    • 没问题,一定是我的服务器出了问题,在主目录下可以正常工作,但在子目录下不行。
    猜你喜欢
    • 2013-09-18
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多