【问题标题】:redirecting files with html extension to files without extension (in url)将带有 html 扩展名的文件重定向到没有扩展名的文件(在 url 中)
【发布时间】:2012-03-04 21:44:26
【问题描述】:

我最近使用 htaccess 更改了我的网站 url,这样我的 url 就不会显示文件扩展名。现在我的问题是因为我创建了一个新的 xml 站点地图,所以我的 url 将是无扩展名的!!!谷歌网站管理员工具告诉我重复内容问题!! IE。 page 和 page.html 具有相同的标题....所以我的问题是如何将带有文件扩展名 html 的 url 重定向到没有扩展名的 url !!! 这是我的网站网址的示例,带有 html 扩展名

http://www.shenazhpeyk.co.uk/coding-machines.html

我想重定向并改成

http://www.shenazhpeyk.co.uk/coding-machines

这样就可以解决 Google 网站管理员工具的问题(请提供我在 htaccess 文件中使用的代码)

非常感谢

【问题讨论】:

标签: .htaccess redirect


【解决方案1】:

也找到了这个代码。不确定它是否会完成同样的事情。似乎对我有用,就像上面的一样(对于 PHP)。

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ /$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

我想知道尾随的斜杠以及这些斜杠应该存在还是省略?

【讨论】:

  • 我知道我迟到了,但你能问一下你是如何将它重定向回没有文件扩展名的页面的吗?我真的很喜欢你的代码,现在正在使用它。
【解决方案2】:

试试这个:

Options +FollowSymLinks -MultiViews
DirectorySlash Off

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]

RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]

【讨论】:

    【解决方案3】:

    尝试将以下内容添加到您的站点重定向 URL 的根目录中的 .htaccess 文件中,并删除它。

    RewriteEngine on
    RewriteBase / 
    
    #redirect to remove the .html extension
    RewriteRule ^(.+)\.html$ $1 [L,NC,R=301] 
    

    【讨论】:

    • 感谢回复但我收到以下错误:页面没有正确重定向这是我以前的内容,它可以工作,但它不会将 html 页面重定向到没有扩展名的 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.html [NC,L]
    • 我找到了解决方案,以防其他人有同样的问题!哈哈!选项 +FollowSymLinks -MultiViews DirectorySlash Off RewriteEngine On RewriteCond %{SCRIPT_FILENAME}/ -d RewriteCond %{SCRIPT_FILENAME}.html !-f RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L] RewriteCond %{ENV: REDIRECT_STATUS} ^$ RewriteRule ^(.+)\.html$ /$1 [R=301,L] RewriteCond %{SCRIPT_FILENAME}.html -f RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L] 注意: 不确定它是否正确,但它有效!
    • 阿米尔:恭喜!如果此解决方案有效,请将其作为官方答案与评论放入。这样人们会更容易看到它! :)
    • 我很好奇的是如何让这种事情发挥作用,但仍然使用内容协商(+MultiViews)来处理图像等内容。
    猜你喜欢
    • 2023-03-14
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 2010-10-15
    • 2014-03-15
    • 1970-01-01
    • 2016-12-24
    相关资源
    最近更新 更多