【问题标题】:Can't remove .html extension from website page urls无法从网站页面 url 中删除 .html 扩展名
【发布时间】:2017-05-08 04:45:54
【问题描述】:

会尽量简洁。 我已经阅读了建议我可以通过更改 .htaccess 文件将 .html 从 domain.com/page.html 删除到 domain.com/page 的帖子和页面。

建议我将此代码粘贴到文件中(我是通过 Bluehost 的编辑器完成的

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

我使用 bluehost 托管,直到现在都使用过 wordpress 网站。 我正在使用我自己的 html 页面和样式表等创建我的第一个网站。

因此,.htaccess 文件中有很多其他信息:

# Use PHP5.4 as default
# Changed PHP handler from application/x-httpd-php54 to application/x-httpd-phpbeta on Mon Dec 14 19:23:46 MST 2015.
AddHandler application/x-httpd-phpbeta .php



# BEGIN WordPress
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^/wp-content/endurance-page-cache/ - [L]
    RewriteCond %{REQUEST_METHOD} !POST
    RewriteCond %{QUERY_STRING} !.*=.*
    RewriteCond %{HTTP_COOKIE} !(wordpress_test_cookie|comment_author|wp\-postpass|wordpress_logged_in|wptouch_switch_toggle) [NC]
    RewriteCond %{DOCUMENT_ROOT}/wp-content/endurance-page-cache/$1/_index.html -f
    RewriteRule ^(.*)$ /wp-content/endurance-page-cache/$1/_index.html [L]
</IfModule>
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 1 weeks"
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

所以我只是在结尾处粘贴了重写行。 该文件位于 public_html 文件夹中。然后,我的所有网站都有多个文件夹。

那么我做错了什么为什么这不起作用?我已经检查过 Bluehost,我使用的是 Apache 服务器

非常感谢您的帮助!

编辑: 尝试按照.htaccess 文件末尾的建议添加此内容:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

剩下的就是这个:

https://i.stack.imgur.com/qv0GJ.png

山姆

【问题讨论】:

    标签: html .htaccess


    【解决方案1】:

    尝试在您的 .htaccess 中使用它:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)$ $1.html
    

    这应该会从您的网址中删除 .html。请确保在测试之前清除缓存

    【讨论】:

    • 我试过了,并发布了我上面得到的错误。非常感谢您的快速回复
    • 嗨@Sam,从它周围删除&lt;IfModule mod_rewrite.c&gt;&lt;/IfModule&gt;。那是不需要的。也请放置在您的.htaccess 顶部,然后刷新您的缓存。
    • 如果这仍然不起作用,请查看@anubhava 的答案 - 它应该会有所帮助:answer here
    猜你喜欢
    • 2017-02-24
    • 2013-01-09
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多