2011-04-20 63 views
65

你有任何想法如何从静态页面的URL中去除.html。另外,我需要将.html的任何网址重定向到没有网址的网址(即www.example.com/page.html到www.example.com/page)。如何从URL去除.html

+0

也许与正则表达式? – 2011-04-20 12:18:42

+0

通过“删除.html”,你的意思是“不需要.html存在”? – 2011-04-20 12:26:37

+0

@Tomalak:是的,也可以将“.html”的网址重定向到没有链接的网址。我的问题是,这导致无限重定向。我目前的设置允许www.example.com/page.html和www.example.com/page都可访问,这不是SEO友好的。 – Dave 2011-04-20 22:55:23

回答

10

感谢您的回复。我已经解决了我的问题。假设我有我的网页http://www.yoursite.com/html,以下.htaccess规则适用。

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*).html\ HTTP/ 
    RewriteRule .* http://localhost/html/%1 [R=301,L] 

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*)\ HTTP/ 
    RewriteRule .* %1.html [L] 
</IfModule> 
59

随着apache下的.htaccess你可以做这样的重定向:

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

至于从URL的.html去除,只需链接到页面,而不html的

<a href="http://www.example.com/page">page</a> 
+16

这对我无能为力。有没有理由不起作用? – 2014-02-02 04:02:45

0
RewriteRule /(.+)(\.html)$ /$1 [R=301,L] 

试试这个:)不知道它是否有效。

6

我用这个.htacess从我的网址网站中删除的.html extantion,请确认这是正确的代码:

RewriteEngine on 
RewriteBase/
RewriteCond %{http://www.proofers.co.uk/new} !(\.[^./]+)$ 
RewriteCond %{REQUEST_fileNAME} !-d 
RewriteCond %{REQUEST_fileNAME} !-f 
RewriteRule (.*) /$1.html [L] 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP 
RewriteRule ^([^.]+)\.html$ http://www.proofers.co.uk/new/$1 [R=301,L] 
+0

这似乎对我来说很有用,与此处介绍的其他解决方案不同,谢谢。但是我会补充说,你仍然需要更新HTML中的链接(所以如果你最初是链接到你的.html文件的话,你应该将它更新为,然后它就可以工作。) – Lorenzo 2014-03-01 20:17:24

+0

本地主机。 – Jayo2k 2015-06-03 22:43:30

+0

我的核心变化是'RewriteBase /'位。不幸的是,我不明白它为什么起作用,但我想我很快会学到。 – 2017-02-21 01:52:44

51

这应该为你工作:

#example.com/page will display the contents of example.com/page.html 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule ^(.+)$ $1.html [L,QSA] 

#301 from example.com/page.html to example.com/page 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/ 
RewriteRule ^(.*)\.html$ /$1 [R=301,L] 
+2

我在这个代码中得到了一个404 Godaddy,我修正了它:Options + FollowSymLinks -MultiViews -Indexes在最上面。 – Labanino 2014-02-13 13:10:24

+1

它的工作..真棒。 – 2016-05-01 11:26:21

+0

这对我来说... thnx – 2016-05-19 05:50:32

12

你还需要确保你有Options -MultiViews

以上都不是标准cPanel主机上的工作。

这工作:

Options -MultiViews 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^\.]+)$ $1.html [NC,L] 
43

我觉得乔恩的答案的一些解释将是建设性的。以下:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

检查,如果指定的文件或目录分别不存在,则重写规则进行:

RewriteRule ^(.*)\.html$ /$1 [L,R=301] 

但到底是什么意思呢?它使用regex (regular expressions)。这里有一点我早些时候做的... enter image description here

认为这是正确的。

注意:当测试您的.htaccess不要使用301重定向。使用302直到完成测试,因为浏览器将缓存301s。见https://stackoverflow.com/a/9204355/3217306

更新:我有点误,.匹配除了换行符以外的所有字符,所以包括空格。此外,here is a helpful regex cheat sheet

来源:

http://community.sitepoint.com/t/what-does-this-mean-rewritecond-request-filename-f-d/2034/2

https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules

+2

极好的图表来帮助解释答案。 – 2017-09-26 08:37:08

+0

301s和浏览器缓存的提示是解决了我的问题。 – Mark 2018-02-08 17:10:37

+0

非常丰富。 – Muema 2018-03-04 17:43:45

37

要从网址中删除.html扩展,你可以在根/ htaccess的使用下面的代码:

RewriteEngine on 


RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC] 
RewriteRule^/%1 [NC,L,R] 

RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule^%{REQUEST_URI}.html [NC,L] 

注意:如果您想删除任何其他扩展名,例如删除.php扩展名,只需替换e html无处不在php在上面的代码中。

+6

没有其他答案为我工作,但这一个,非常感谢! – 2016-03-20 01:23:06

+1

它适用于我:) – Javid 2016-08-04 04:56:30

+1

这一个也为我工作。谢谢@starkeen。有一个^投票。 – JeremyS 2016-10-10 18:57:28