2012-01-17 92 views
0

AS有時會發生,我已經創建了我所說的在我的htaccess文件中的混亂。我有幾個問題,我知道我有時在內部循環,反正我不擅長。我在幾十個SO問題上瀏覽了數十篇文章,並嘗試了大量的解決方案。最終的結果是,我試圖將多個單獨的解決方案拼湊成一個多部分的問題,而且我做了一個真正的混亂。這幾天完全是我的使命。我卡住了。壞。在Mod_rewrite中添加拖尾斜槓並在虛擬目錄中重定向非拖尾斜槓

  • 我想mydomain.com/handmade-jewelry去MYDOMAIN /手工飾品/(注意斜線)
  • 然後我也有.COM /手工飾品/一些片將珠寶細節.PHP?ID =一些件。這有效,但它也需要一個尾隨斜線。
  • 當我嘗試在「/ some-piece /」上顯示尾部斜線時,它會重定向並找到未找到的文件。

相信我。我知道我的邏輯中必然會有明顯的缺陷,而我自週六以來拒絕提出這個問題。我知道這是超級經常問的。我不會問我是不是死定了。

(rewrite all www and non-www requests to https:// ...works fine) 
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC] 
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L] 
RewriteCond %{HTTPS} !=on 
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L] 
RewriteCond %{SERVER_PORT} ^80$ 
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L] 

(redirecting to the page with all the jewelry) 
RewriteRule ^handmade-jewelry/$ jewelry-inventory-page.php [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
(following was a failed attempt.) 
# RewriteCond %{REQUEST_URI} !rock-jewelry/(.*)/$ 
(following works with no trailing slash, which is needed) 
RewriteRule ^handmade-jewelry/(.*)$ jewelry-details.php?s=$1 [L] 
(if I turn this on, it goes to a 404 looking for "handmade-jewelry/this-piece/index.php?s=thispiece/etc...") 
# RewriteRule ^handmade-jewelry/(.*)?/$ jewelry-details.php?s=$1 [L] 

對不起。我知道很多你,這種問題變得多餘。

回答

2

嘗試添加以下到您的htaccess文件在您的網站

RewriteEngine on 
RewriteBase/

#(rewrite all www and non-www requests to https:// ...works fine) 
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC] 
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L] 
RewriteCond %{HTTPS} !=on 
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L] 

#I believe this is redundant because of the previous rule 
RewriteCond %{SERVER_PORT} ^80$ 
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L] 

#if existing file or directory 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
#do nothing 
RewriteRule^- [L] 


#if it does not end with a slash e.g. handmade-jewelry, redirect to with slash 
RewriteRule ^([-a-zA-Z0-9]+)$ /$1/ [L,R=301] 

#if it does not end with a slash e.g. handmade-jewelry/some-piece, add the slash 
RewriteRule ^([-a-zA-Z0-9]+/[-a-zA-Z0-9]+)$ /$1/ [L,R=301] 

#(redirecting to the page with all the jewelry) 
RewriteRule ^handmade-jewelry/$ jewelry-inventory-page.php [L,NC] 

RewriteRule ^handmade-jewelry/([-a-zA-Z0-9]+)/$ jewelry-details.php?s=$1 [L,NC] 
+0

哦,我的根目錄下!我不能夠感謝你!這絕對是完美的,並且完全按照它的設想工作。非常感謝你爲此所做的一切。 – Cyprus106 2012-01-17 13:55:32