2016-07-15 35 views
1

首先,我知道有很多類似這個問題的問題。我已經閱讀了所有我能找到的內容,但是我在其他地方看到的解決方案似乎並不適合我。我真的希望有人能在這裏給我一些見解。隱藏文件擴展名並重定向到https

我試圖使用Apache的.htaccess指令強制特定頁面在我的服務器上使用ssl。除了這些指令外,我還使用一些重寫來掩蓋.php.html擴展名。我創建了一個頁面https-test.html。我想那個頁面專門總是重定向所以它使用HTTPS和使.html被剝去,像https://www.example.com/https-test

但是,我似乎總是與一個循環結束了。閱讀Apache文檔6小時讓我更接近,但我仍然錯過了一些東西。

下面是我註釋的htaccess文件。

RewriteEngine on 

# If port is insecure... 
RewriteCond %{SERVER_PORT} ^80$ 
# And requested URI is /https-test... 
RewriteCond %{REQUEST_URI} ^(.*/)https-test$ [NC] 
# Then point the server to the secure url: 
RewriteRule . "https://www.example.com/https-test" [L,R] 


# The next few lines try matching extensionless requests to .php files 
# If the requested file is not a directory... 
RewriteCond %{REQUEST_FILENAME} !-d 
# And we CAN find a .php file matching that name... 
RewriteCond %{REQUEST_FILENAME}\.php -f 
# Then point us to that .php file and append the query string. 
RewriteRule ^(.+)$ $1.php [L,QSA] 


# These next few lines were added by the previous project owner 
# They're supposed to redirect requests like /foo.html to /foo, 
# But I suspect these might be the culprit 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^\.]+)/$ /$1 [R=301,NE,L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^\.]+)$ /$1.html [NE,L] 


# Next few lines are legacy SEO stuff, some pages were linked to as 
# php but now are html 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} .php$ 
RewriteRule ^(.*).php$ /$1.html [L,NE] 

所以這就是我在我的htaccess中的代碼。如果我在Chrome瀏覽器中訪問http://www.example.com/https-test,我會收到www.mysite.com太多次重定向。

回答

1

您應該稍微重寫代碼。您試圖將兩個無擴展名文件匹配到php和html,並且看起來並不像您在考慮每個條件。你應該添加一個條件,以確保他們不試圖做同樣的事情。

備份你的代碼,用這個替換你的代碼並試一試。嘗試之前清除所有緩存。

RewriteEngine on 
# If port is insecure... redirect for a specific page 
RewriteCond %{HTTPS} !^on [OR] 
RewriteCond %{HTTP_HOST} ^example\.com [NC] 
RewriteRule ^http-test/?$ https://www.example.com%{REQUEST_URI} [R=301,L] 

# Next few lines are legacy SEO stuff, some pages were linked to as 
# php but now are html 
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php 
RewriteRule^/%1? [R=301,L] 

# The next few lines try matching extensionless requests to .php files 
# If the requested file is not a directory and php file exists 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.+)$ $1.php [L,QSA] 

#remove trailing slash and is not a php file 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php !-f 
RewriteRule ^([^\.]+)/$ /$1 [R=301,NE,L] 

#finally redirect extensionless URI to html 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^\.]+)$ /$1.html [NE,L] 

注意我還沒有完全測試過。

+0

非常感謝。您的更改肯定會讓事情變得更清晰,更易於理解。 這些規則適用於'.php'和'.html'重寫。不幸的是,我仍然在爲'https:// www.example.com/https-test'獲取重定向循環。我真的不明白。 – wrydere

+1

將301更改爲302以清除緩存並查看是否有幫助。如果有效,那麼你可以將它改回301. –

+0

清除了我的緩存,試過不同的瀏覽器,仍然得到一個循環... – wrydere

相關問題