2016-08-25 203 views
0

當我在瀏覽器中輸入一個URL地址的形式https://myurl.com/path吧,我想重寫爲https://myurl.com/path/爲什麼我的網址沒有用結尾斜槓重寫?

在.htaccess,我重寫規則如下:

#RewriteEngine On 
#RewriteCond %{HTTP_HOST} !^myurl.com$ 
#RewriteRule ^(.*)$ http://myurl.com/$1 [R=301,L] 
#RewriteCond %{SERVER_PORT} 80 
#RewriteRule ^(.*)$ https://myurl.com/$1 [R,L] 
RewriteEngine On 
RewriteCond %{HTTPS} !=on 
RewriteRule .* https://myurl.com%{REQUEST_URI} [R=301,L] 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L] 
# BEGIN WordPress 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php [L] 
</IfModule> 

回答

1

我在過去的幾天裏,我一直在做相當多的研究。看起來你很接近。請嘗試以下操作:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)([^/])$  /$1$2/ [R=301,L] 

的的RewriteCond將檢查以確保有一個與該名稱的文件,如果沒有,則執行重寫規則。比擁有手動擴展列表更有前瞻性!

這是從以下問題的複製/粘貼:我對.htaccess並不是很了不起,但就像我說過的,過去幾天我一直在做很多研究。 .htaccess Rewrite to Force Trailing Slash at the end

的[R = 301]也將增加301重定向到URL的搜索引擎,讓他們知道要看看未來頁的削減版本 - 這有助於減少重複的內容和整合分析信息。

希望有所幫助。

+0

感謝@MrMr工作:)我沒有親自將規則添加到此.htaccess文件中,別人也這麼做,所以我不能因爲接近解決方案而承擔功勞..我不確定爲什麼.html規則已添加,所有頁面的格式爲https:// myurl/path/page /。您是否建議我更換以下行: 'RewriteCond%{REQUEST_FILENAME} .html -f'和 'RewriteRule!。*。html $%{REQUEST_FILENAME} .html [L]'只有? – Mattypants

+0

我現在遇到的一個問題是我的XML站點地圖現在正在得到一個尾部斜線,即/sitemap.xml/。有趣的是 - 謝天謝地 - pdf文件沒有得到一個斜線。 – Mattypants

相關問題