【问题标题】:how to add slash after removing html extension删除html扩展后如何添加斜杠
【发布时间】:2012-03-18 23:24:57
【问题描述】:

如何在删除html扩展后使用以下代码在末尾添加/:

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]

【问题讨论】:

  • 我帮不了你,但你应该更具体一点:你应该说你在 apache 和 mod rewrite 方面遇到了问题(这只是一个猜测,但你应该首先告诉它) 并添加正确的标签(应该像 html、apache、mod-rewrite、文件扩展名这样的想法)来帮助人们帮助你......只是一个建议
  • 感谢回复上面的代码在 url example.co.uk/page.htmlexample.co.uk/page 之后的变化,我需要的是 / 在页面的末尾得到 example.co.uk/page
  • 请适当标记您的问题; 'html' 和其他更多,与您的问题完全无关。
  • 我的意思是当你说你在说什么而不是你真正需要什么时,你应该更具体。适当的标记(正如大卫托马斯指出的那样)可以帮助 apache 专家检索您的帖子并帮助您。我只知道 mod_rewrite for apache 可以做你需要的,但我不知道怎么做。其他人可能会这样做,但是如果没有正确的标记和正确的明确问题,您可能会冒着没有人阅读此内容的风险。
  • 现在好了吗?我没有问题,我只需要在我的网址末尾加上斜线

标签: .htaccess mod-rewrite url-rewriting rewrite slash


【解决方案1】:

您需要进行一些更改。

Options +FollowSymLinks -MultiViews

RewriteEngine On    
RewriteBase /

#Code to add forward slash
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !(?:\.\w+|/)$
RewriteRule (.*) $1/ [R,L]

#To check whether a .html appended string is a file existing on the system 
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule (.*)/ $1.html [L]

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

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

这些规则也不能如愿发挥作用:

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

cos,%{REQUEST_URI} 将始终以 / 结尾。如果请求像 domain.com/about 这样的 URI,

它将被重写为:

domain.com/about/

终于到了

domain.com/about/.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-15
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    相关资源
    最近更新 更多