2011-11-18 140 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也發佈了更早的規則,其中一個可能會匹配並取代這一個。 –