【问题标题】:Unable to add trailing /无法添加尾随 /
【发布时间】:2015-02-22 23:05:37
【问题描述】:

我是设置 .htaccess 文件的新手,非常感谢您在这个问题上的帮助。

我无法在我网站的所有 URL 中添加尾随 /。我已经尝试了 StackOverflow 和这篇文章的大部分答案,但它对我不起作用,要么样式损坏,要么没有添加 /,要么在尝试这些解决方案时没有添加 /

可能规则有冲突?

这是我的.htaccess 现在的样子:

# do not allow anyone else to read your .htaccess file
<Files .htaccess>
deny from all
</Files>

# forbid viewing of directories
Options All -Indexes

# hide this list of files from being seen when listing a directory
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

# disable the server signature- helps with preformance
ServerSignature Off

RewriteEngine On
RewriteBase /

# specific rule to show 1 URL but other URL is active
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC] 
RewriteRule ^([^/]+) $1.html [L]

# hide /index
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

# add trailing /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !http://example.com
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]

我想在除 http://example.com 之外的所有 URL 中添加尾随 /(我正在使用默认的 /index)。

我该如何解决这个问题?

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    您的最后一个看起来不正确,而且您的规则顺序错误。试试这个代码:

    # do not allow anyone else to read your .htaccess file
    <Files .htaccess>
    deny from all
    </Files>
    
    # forbid viewing of directories
    Options All -Indexes
    
    # hide this list of files from being seen when listing a directory
    IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
    
    # disable the server signature- helps with preformance
    ServerSignature Off
    
    RewriteEngine On
    RewriteBase /
    
    # hide /index
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index($|\ |\?)
    RewriteRule ^ /%1/ [R=301,L,NE]
    
    # add trailing /
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule ^[^.]+$ %{REQUEST_URI}/ [L,R=301,NE]
    
    # specific rule to show 1 URL but other URL is active
    RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC] 
    RewriteRule ^([^/]+) $1.html [L]
    

    【讨论】:

    • 它可以工作,但只有在我每次打开新页面时都刷新之后。某些页面刷新后没有加载内容,因为它正在服务器中寻找相应的文件。
    • 现在可以使用了。但是 Javascript 代码似乎不起作用。有什么想法,为什么?您可以在该链接上查看。
    • 我已修复 Javascript 部分以通过 AJAX 提取数据。现在的问题是 - 尝试单击导航栏中的一些链接,然后单击徽标以返回主页。现在有 2 个/。我该如何摆脱它?
    猜你喜欢
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多