.htaccess重定向到不正常工作的子目录

时间:2013-06-25 02:58:58

标签: .htaccess redirect mod-rewrite subdirectory

我是htaccess的新手,我一整天都在努力。请帮忙。我只想要:

  1. www.example.com将重定向至example.com/hu
  2. example.com将被重定向到example.com/hu
  3. 这是我从typo3网站上复制的htaccess文件:

    <FilesMatch "\.(js|css)$">
      <IfModule mod_expires.c>
        ExpiresActive on
        ExpiresDefault "access plus 7 days"
      </IfModule>
      FileETag MTime Size
    </FilesMatch>
    
    
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]
    RewriteRule ^typo3$ typo3/index_re.php [L]
    
    
    
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule .* index.php [L]
    
    </IfModule>
    
    
    RewriteCond %{HTTP_HOST} !^dnaplus-humangenetikaicentrum\.com$
    RewriteRule ^(.*)$ http://dnaplus-humangenetikaicentrum.com/hu/$1 [L,R=301]
    

    它将www.example.com重定向到www.example.com/hu。但不适用于example.com。

1 个答案:

答案 0 :(得分:0)

试试这个

# if hostname starts with "www"...
RewriteCond %{HTTP_HOST} ^www\.(.+)$    #here we get rest of URI as %1

# ...redirect permanently to hostname whitout "www":
RewriteRule (.*) http://%1/$1 [R=301,L]


# if client requested root directory:
RewriteCond %{REQUEST_URI} ^/$

# redirect him to start page:
RewriteRule .* hu [R=307,L]

我建议使用exacly临时重定向(307)来启动页面。浏览器会对301状态进行硬缓存,因此如果您稍后更改起始页,某些用户仍会被重定向到旧页面。