重定向主页而不影响其他规则

时间:2015-01-10 17:45:13

标签: apache .htaccess mod-rewrite redirect

我现在的.htaccess如下。

# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on

# Send request via index.php (if it's not an existing file/folder)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

<IfModule mod_php5.c>
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_php5.c>
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

我想将我的主页重定向到另一个域,但维护所有/ sub /目录。 所以改变了以下规则。

# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on

# Send request via index.php (if it's not an existing file/folder)
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d

#redirect
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://example.com/ [L,R=301]    

<IfModule mod_php5.c>
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_php5.c>
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

现在所有/ sub /目录都出现了internel服务器错误。日志表明它正在进行循环。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

RewriteCond仅适用于下一个RewriteRule

使用:

# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on

#redirect
RewriteRule ^$ http://example.com/ [L,R=301] 

# Send request via index.php (if it's not an existing file/folder)
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

<IfModule mod_php5.c>
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_php5.c>
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>