【问题标题】:How do I remove the .html from the end of URL while using SiteGround使用 SiteGround 时如何从 URL 末尾删除 .html
【发布时间】:2021-08-11 14:05:30
【问题描述】:

在得知我的客户正在使用 SiteGround 之前,我最初使用 React 构建了我的网站,因此我不得不将所有内容都更改为基本的 HTML 格式。我的导航栏使用 href = "fileName.html" 来导航页面。一旦离开主页,这将导致 URL 变为 www."siteName".com/"fileName.html"。

这是我目前在 .htaccess 中从末尾删除 .html 的内容:

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

<IfModule mod_rewrite.c>

  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html [L]

</IfModule>

这似乎不起作用。我应该更改我的 .htaccess 文件吗?我应该更改导航栏导航页面的方式吗?

【问题讨论】:

  • 欢迎来到 SO,感谢分享您的代码。所以你想将 url 从http://localhost:80/file.html 重定向到http://localhost:80/file?我的意思是你在浏览器中点击http://localhost:80/file.html 链接吗?请确认一次。

标签: html reactjs .htaccess url nav


【解决方案1】:

使用您显示的示例/尝试,请尝试遵循 .htaccess 规则文件。请确保在测试您的 URL 之前清除您的浏览器缓存。

Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteBase /

###Rules for handling .html to without html uri in browser.
RewriteCond %{REQUEST_URI} \s/([^.]*)\.html\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.*)/?$ $1.html [L]

##Rule from OP's already existing .htaccess file.
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>

【讨论】:

  • @Carlos Benitez,如果上述解决方案对您有用,请告诉我?
猜你喜欢
  • 2023-01-18
  • 2019-06-14
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 1970-01-01
相关资源
最近更新 更多