【问题标题】:.htaccess file extension removal not working (lots of solutions tried).htaccess 文件扩展名删除不起作用(尝试了很多解决方案)
【发布时间】:2012-01-18 11:33:33
【问题描述】:

我已经在 Google 上搜索了几天,现在试图找到解决我的问题的方法,修补代码段无济于事。没有任何效果。我只想从我的 URL 中删除文件扩展名 .shtml 但没有任何效果。我已经确保文件正在更新,并且我的网站没有从缓存中加载以及所有这些恶意。有人能告诉我哪里出错了吗?

我唯一能想到的是我的虚拟主机 (Hostgator) 没有启用 Apache mod_rewrite?

RewriteEngine On

#Remove file extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.shtml -f
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.shtml [L]

#Add trailing slash
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !#
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mythofechelon.co.uk/$1/ [L,R=301]

我们将不胜感激。

【问题讨论】:

  • 首先要确保您的 .htaccess 正在被读取。放一些垃圾文本,看看你是否在浏览器中收到 500 错误。
  • 我的问题中的块只是文件的一部分。其他组件(例如,DirectoryIndex 和 ErrorDocument)工作正常。
  • 但我还是这样做了,是的,我收到了 500 内部服务器错误。

标签: apache file .htaccess webserver


【解决方案1】:

使用以下代码代替您的扩展删除代码:

# To externally redirect /dir/foo.shtml to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s.+\.shtml [NC]
RewriteRule ^(.+)\.shtml$ /$1 [R=301,L,NC]

# To internally redirect /dir/foo to /dir/foo.shtml
RewriteCond %{REQUEST_URI} !\.shtml$ [NC]
RewriteCond %{REQUEST_FILENAME}.shtml -f
RewriteRule . %{REQUEST_URI}.shtml [L

添加斜杠

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]

但是请记住,即使是 http://domain.com/foo#aaa 的 URI 也会变成 http://domain.com/foo/#aaa,因为无法从 mod_rewrite 中检测到 #,因为它完全由浏览器处理。

【讨论】:

  • 效果很好!谢谢!你还知道如何在 URL 的末尾加上斜杠吗?并且该代码是否适用于您在此处手动键入的任何其他文件扩展名(我假设您必须为每个扩展名创建一个块)
  • 不客气。我添加了添加斜杠的代码,但需要注意的是# 也会有斜杠。
  • 当时的代码不起作用。 :/ 不用担心,我的主要目的是消除文件扩展名。谢谢!
【解决方案2】:
Options +MultiViews

做同样的事情。

【讨论】:

  • 刚刚将其添加到#Remove 文件扩展名块中,但它不起作用。
  • 删除了除该代码之外的所有内容,但仍然无法正常工作。 :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-04
  • 2023-03-22
  • 2017-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多