2014-12-24 152 views
1

我有一個重定向問題。我有我的htaccess的設置,以從URL執行下列操作... 1.刪除PHP擴展 2.添加自定義錯誤頁 3.強制一些301重定向重定向問題和htaccess

但與重定向一個巨大的問題... 如果用戶去... www.mysite.com/articles/jewelry/types.php ...然後,基於第二次301重定向,他們應該被髮送到... www.mysite.com/article/medieval-jewelry-types-and-functions/ ...但是,它們被重定向到... www.mysite.com/article/medieval-jewelry/types

如果他們去... www.mysite.com/articles/picks/A_1.php ...他們應該被重定向到... www.mysite.com/article/picks-tools/ ...但是他們得到發送... www.mysite.com/article/how-to-pick/

它看起來像重定向正在改變網址的一部分...可能是因爲.php擴展名(但只是猜測 - 因爲我對編輯htaccess文件知之甚少)。有任何想法嗎?

下面是我的大多數htaccess文件中,對什麼可能導致該問題的一些意見。任何想法如何解決它?

# enable the rewrite engine 
RewriteEngine On 
# Set your root directory 
    RewriteBase/

# remove the .php extension 
    RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP 
    RewriteRule (.*)\.php$ $1 [R=301] 

# remove index and reference the directory 
    RewriteRule (.*)/index$ $1/ [R=301] 

# remove trailing slash if not a directory 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_URI} /$ 
    RewriteRule (.*)/ $1 [R=301] 

# forward request to html file, **but don't redirect (bot friendly)** 
    RewriteCond %{REQUEST_FILENAME}.php -f 
    RewriteCond %{REQUEST_URI} !/$ 
    RewriteRule (.*) $1\.php [L] 

ErrorDocument 400 /400.php 
ErrorDocument 401 /401.php 
ErrorDocument 403 /403.php 
ErrorDocument 404 /404.php 
ErrorDocument 405 /405.php 
ErrorDocument 408 /408.php 
ErrorDocument 414 /414.php 
ErrorDocument 500 /500.php 
ErrorDocument 502 /502.php 
ErrorDocument 504 /504.php 

# example of redirects 
Redirect 301 /articles/index.php /articles/ 

# it looks like the redirect below is conflicting with... 
Redirect 301 /articles/jewelry/ /article/medieval-jewelry/ 
# ... this redirect. 
Redirect 301 /articles/jewelry/types.php /article/medieval-jewelry-types-and-functions/ 

# And this redirect... 
Redirect 301 /articles/picks/ /article/how-to-pick/ 
Redirect 301 /articles/picks/index.php /article/how-to-pick/ 
# ... is conflicting with this one... 
Redirect 301 /articles/picks/A_1.php /article/picks-tools/ 

回答

0

問題似乎是規則的,不正確的正則表達式和mod_rewrite規則混合錯誤的順序與Redirectmod_alias)規則。試試這個:

ErrorDocument 400 /400.php 
ErrorDocument 401 /401.php 
ErrorDocument 403 /403.php 
ErrorDocument 404 /404.php 
ErrorDocument 405 /405.php 
ErrorDocument 408 /408.php 
ErrorDocument 414 /414.php 
ErrorDocument 500 /500.php 
ErrorDocument 502 /502.php 
ErrorDocument 504 /504.php 

# enable the rewrite engine 
RewriteEngine On 
# Set your root directory 
RewriteBase/

# example of redirects 
RewriteRule ^articles/index\.php$ /articles/ [L,NC,R=302] 

# it looks like the redirect below is conflicting with... 
RewriteRule ^articles/jewelry/?$ /article/medieval-jewelry/ [L,NC,R=302] 

RewriteRule ^articles/jewelry/types\.php$ /article/medieval-jewelry-types-and-functions/ [L,NC,R=302] 

RewriteRule ^articles/picks/?$ /article/how-to-pick/ [L,NC,R=302] 

RewriteRule ^articles/picks/index\.php$ /article/how-to-pick/ [L,NC,R=302] 

RewriteRule ^articles/picks/A_1\.php$ /article/picks-tools/ [L,NC,R=302] 

# remove the .php extension 
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP 
RewriteRule (.*)\.php$ $1 [R=301] 

# remove index and reference the directory 
RewriteRule (.*)/index$ $1/ [R=301] 

# remove trailing slash if not a directory 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} /$ 
RewriteRule (.*)/ $1 [R=301] 

# forward request to php file, **but don't redirect (bot friendly)** 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteCond %{REQUEST_URI} !/$ 
RewriteRule (.*) $1\.php [L] 
+0

我會檢查出來......我注意到,我正在寫入重定向爲... /articles/picks/A_1.php ...並且您使用了... ^文章/選擇/ A_1 \ .php $ ...你能解釋一下你做了什麼以及你的語法是如何工作的?我試圖更好地理解這一點... – Jabbamonkey

+0

對不起,因爲我在手機上打字時輸入錯字。引導斜槓在htaccess上不匹配,點是需要轉義的特殊字符。請測試一下,讓我知道這是否可行。 – anubhava

+1

看起來不錯。謝謝您的幫助。我需要了解更多關於htaccess文件... – Jabbamonkey