简体   繁体   English

如何使用 htaccess 从 url 中删除 .html

[英]How to remove .html from url using htaccess

I want to remove .html from url.我想从 url 中删除 .html。 For this i am trying following.为此,我正在尝试关注。 But not sure what is wrong, not working in my case.但不确定出了什么问题,在我的情况下不起作用。

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

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

I want to remove .html from this URL http://localhost/demo/blog/test.html .我想从这个 URL http://localhost/demo/blog/test.html删除.html So if someone enter this URL, .html should be removed from URL by htaccess http://localhost/demo/blog/test/ .因此,如果有人输入此 URL,则应通过 htaccess http://localhost/demo/blog/test/从 URL 中删除.html

And need to add this rules only for blog directory.并且只需要为博客目录添加此规则。

I hope someone help me.我希望有人帮助我。

try this尝试这个

Options +FollowSymlinks -MultiViews
    RewriteEngine on

    # to make `/path/index.html` to /path/
    RewriteCond %{THE_REQUEST} ^GET\s(.*/)index\.html [NC]
    RewriteRule . %1 [NE,R=301,L]

    RewriteCond %{THE_REQUEST} ^GET\s.+\.html [NC]
    RewriteRule ^(.+)\.html$ /$1 [NE,R=301,L,NC]

    RewriteCond %{REQUEST_URI} !\.html$ [NC]
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule . %{REQUEST_URI}.html [L]

That should do it.应该这样做。

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

Edit 1编辑 1

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

Edit 3编辑 3

As suggested in the comments, make sure that mod_rewrite is enabled .正如评论中所建议的,确保enabledmod_rewrite Use this link to help you out.使用链接来帮助您。

You can use these rules in /demo/blog/ :您可以在/demo/blog/使用这些规则:

RewriteEngine On
RewriteBase /demo/blog/

RewriteCond %{THE_REQUEST} \s(.+?)\.html?\s [NC]
RewriteRule ^ %1 [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

First copy paste this code into your .htaccess file.首先将此代码复制粘贴到您的 .htaccess 文件中。

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

Then change the web addresses on your navigation bar/link like this ↓ From <a href="about.html"> to <a href="about">然后像这样更改导航栏/链接上的网址↓从<a href="about.html"><a href="about">

It should work, also try not to put capital letters on your file names;它应该可以工作,也尽量不要在您的文件名上使用大写字母; It is not a good practice这不是一个好习惯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM