2011-11-18 142 views
0

我遇到了(应该是)简单的.htaccess重定向问题。我需要将任何具有“for_homes”的网址更改为“for_home”。.htaccess重定向问题

RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301] 

然而,当我去到任何网页与for_homes我得到一个404


重写规则:

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/
    RewriteCond %{REQUEST_URI} ^system.* 
    RewriteRule ^(.*)$ /index.php?/$1 [L] 
    RewriteCond %{REQUEST_URI} ^application.* 
    RewriteRule ^(.*)$ /index.php?/$1 [L] 

    #request to index.php 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php?/$1 [L] 
    RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301] 
    RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301] 
</IfModule> 
+0

是否'for_homes'总是出现在URL的开始?你的正则表达式意味着它('^ for_homes') –

+0

你的代码中有'RewriteEngine on'吗?规则看起来不错! – Gerben

+0

RewriteEngine叙述在 RewriteBase/ 的RewriteCond%{REQUEST_URI} ^系统。* 重写规则^(。*)$ /index.php?/$1 [L] 的RewriteCond%{REQUEST_URI} ^应用。 * 重写规则^(。*)$ /index.php?/$1 [L] #request到index.php 的RewriteCond%{} REQUEST_FILENAME!-f 的RewriteCond%{} REQUEST_FILENAME!-d 重写规则^(。* )$的index.php?/ $ 1 [L] \t重写规则^ for_homes(。*)$ for_home $ 1 [L,R = 301] \t重写规则^ for_business(。*)$ for_businesses $ 1 [L,R = 301] Jeff

回答

0

问题是前面的规则是重写到index.php。在此块在这里:

#request to index.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

### This rule rewrites "for_homes" to "index.php?/for_homes" 
RewriteRule ^(.*)$ index.php?/$1 [L] 

### These rules never get applied 
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301] 
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301] 

因此,或许RewriteCond %{REQUEST_FILENAME} !-d后,加入条件,排除后2次重写,所以它看起来是这样的:

#request to index.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !^/for_home(s)? 
RewriteCond %{REQUEST_URI} !^/for_business(es)? 
RewriteRule ^(.*)$ index.php?/$1 [L] 
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301] 
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301] 

如果这是你的意图有index.php文件处理for_homefor_businesses,这样,当有人进入他们的浏览器地址栏中http://domain.com/for_homes/stuff,浏览器被重定向到http://domain.com/for_home/stuff然后它就会在内部改写成/index.php?/for_home/stuff,这样的index.php可以处理请求,你只需要移动301之前重定向在index.php改写:

### Redirect first 
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301] 
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301] 

#request to index.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?/$1 [L] 
+0

是的!订单有所不同,非常感谢! – Jeff

0

也许你只是缺少RewriteEngine On

RewriteEngine On 
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301] 
+0

不幸的是,有几个排队。已经有一些重写规则(来自CMS)可以正常工作。我也做了一个改变,以测试我的更改是否被注意到。 – Jeff

+0

@Jeff也发布了更早的规则,其中一个可能会匹配并取代这一个。 –