【问题标题】:Clear URL with .htaccess使用 .htaccess 清除 URL
【发布时间】:2013-05-12 08:11:52
【问题描述】:

我对网页设计还很陌生,一直无法弄清楚这一点..

我正在使用 HTML5Boilerplate 作为我的新网站的模板。模板附带的 .htaccess 文件会重写“www.example.com -> example.com”。这对我来说很好。

现在我想重写我所有的 /index.html -> 根文件夹并清除末尾的斜线。

例如:当有人键入或链接指向“www.mysite.com/subfolder/index.html”(或 .htm)时,这将被重写为“mysite.com/subfolder”。

代码在此刻:

# ----------------------------------------------------------------------
# Start rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and

# features. FollowSymLinks must be enabled for this to work.

# Some cloud hosting services require RewriteBase to be set: goo.gl/HOcPN
# If using the h5bp in a subdirectory, use `RewriteBase /foo` instead where
# 'foo' is your directory.

# If your web host doesn't allow the FollowSymlinks option, you may need to
# comment it out and use `Options +SymLinksIfOwnerMatch`, but be aware of the
# performance impact: http://goo.gl/Mluzd

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
  RewriteEngine On
# RewriteBase /
</IfModule>

# ----------------------------------------------------------------------
# Suppress or force the "www." at the beginning of URLs
# ----------------------------------------------------------------------

# The same content should never be available under two different URLs -
# especially not with and without "www." at the beginning, since this can cause
# SEO problems (duplicate content). That's why you should choose one of the
# alternatives and redirect the other one.

# By default option 1 (no "www.") is activated.
# no-www.org/faq.php?q=class_b

# If you'd prefer to use option 2, just comment out all option 1 lines
# and uncomment option 2.

# IMPORTANT: NEVER USE BOTH RULES AT THE SAME TIME!

# ----------------------------------------------------------------------

# Option 1:
# Rewrite "www.example.com -> example.com".

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>

# ----------------------------------------------------------------------

# Option 2:
# Rewrite "example.com -> www.example.com".
# Be aware that the following rule might not be a good idea if you use "real"
# subdomains for certain parts of your website.

# <IfModule mod_rewrite.c>
#   RewriteCond %{HTTPS} !=on
#   RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
#   RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# </IfModule>

【问题讨论】:

    标签: .htaccess mod-rewrite seo rewrite html5boilerplate


    【解决方案1】:

    我想你可以试试这个代码:

    RewriteEngine on
    RewriteCond %{THE_REQUEST} ^.*\/index\.html?\ HTTP/
    RewriteRule ^(.*)index\.html?$ "/$1" [R=301,L]
    

    要删除结尾斜杠,请添加以下代码:

    # remove trailing slash
    RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
    

    【讨论】:

    • 不,没有帮助。至少如果你把它放在我的代码中已经存在的# Rewrite "www.example.com -&gt; example.com". &lt;IfModule mod_rewrite.c&gt; RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L] &lt;/IfModule&gt; 之后。
    【解决方案2】:

    如果您认真对待使用 HTAccess,有一个非常可靠的参考资料可以解决所有可能出现的小问题。它不是免费的 - 但会自行付费。

    http://htaccessbook.com

    【讨论】:

    • 嗨,David,感谢您的提示,但我不认为我会详细介绍 htaccess。我只需要修复这件小事,然后忘记调整 htaccess 文件。
    • 它并没有那么复杂 - 但是你可能会在做一些你不了解细节的事情时遇到最糟糕的麻烦,这在这篇文章中是不可能正确解释的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2017-02-06
    相关资源
    最近更新 更多