2016-08-25 207 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

相关问题