如何从URL去除.html
你有任何想法如何从静态页面的URL中去除.html。另外,我需要将.html的任何网址重定向到没有网址的网址(即www.example.com/page.html到www.example.com/page)。如何从URL去除.html
感谢您的回复。我已经解决了我的问题。假设我有我的网页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>
随着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>
这对我无能为力。有没有理由不起作用? – 2014-02-02 04:02:45
RewriteRule /(.+)(\.html)$ /$1 [R=301,L]
试试这个:)不知道它是否有效。
我用这个.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]
这应该为你工作:
#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]
我在这个代码中得到了一个404 Godaddy,我修正了它:Options + FollowSymLinks -MultiViews -Indexes在最上面。 – Labanino 2014-02-13 13:10:24
它的工作..真棒。 – 2016-05-01 11:26:21
这对我来说... thnx – 2016-05-19 05:50:32
你还需要确保你有Options -MultiViews
。
以上都不是标准cPanel主机上的工作。
这工作:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
我觉得乔恩的答案的一些解释将是建设性的。以下:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
检查,如果指定的文件或目录分别不存在,则重写规则进行:
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
但到底是什么意思呢?它使用regex (regular expressions)。这里有一点我早些时候做的...
我认为这是正确的。
注意:当测试您的.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
要从网址中删除.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在上面的代码中。
也许与正则表达式? – 2011-04-20 12:18:42
通过“删除.html”,你的意思是“不需要.html存在”? – 2011-04-20 12:26:37
@Tomalak:是的,也可以将“.html”的网址重定向到没有链接的网址。我的问题是,这导致无限重定向。我目前的设置允许www.example.com/page.html和www.example.com/page都可访问,这不是SEO友好的。 – Dave 2011-04-20 22:55:23