【问题标题】:htaccess - remove www and .html from subdomain pageshtaccess - 从子域页面中删除 www 和 .html
【发布时间】:2016-10-05 04:11:49
【问题描述】:

假设我的域是:www.site.name.jp
我想从子页面中删除 www 和 .html,如:www.site.name/about.html

我该怎么办?我一直在使用下面的代码,实际上,它正在工作!但我有点担心它会对我的 Google 抓取分析产生副作用。

Options +MultiViews

RewriteCond %{HTTP_HOST} ^www\.web.roidesign\.jp [NC]
RewriteRule ^/(.*)       http://web.roidesign.jp/$1 [L,R=301]

<IfModule mod_deflate.c>
  #Force deflate for mangled headers
  <IfModule mod_setenvif.c>
    <IfModule mod_headers.c>
      SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
      RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
    </IfModule>
  </IfModule>

  #HTML, TXT, CSS, JavaScript, JSON, XML, HTC:
  <IfModule filter_module>
    FilterDeclare   COMPRESS
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/html
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/css
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/plain
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/x-component
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/javascript
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/json
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/xhtml+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/rss+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/atom+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/vnd.ms-fontobject
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $image/svg+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/x-font-ttf
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $font/opentype
    FilterChain     COMPRESS
    FilterProtocol  COMPRESS  DEFLATE change=yes;byteranges=no
  </IfModule>

  <IfModule !mod_filter.c>
    #Legacy versions of Apache
    AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
    AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml 
    AddOutputFilterByType DEFLATE application/atom+xml
    AddOutputFilterByType DEFLATE image/svg+xml application/vnd.ms-fontobject 
    AddOutputFilterByType DEFLATE application/x-font-ttf font/opentype
  </IfModule>
</IfModule>

【问题讨论】:

    标签: .htaccess google-search-console google-crawlers no-www


    【解决方案1】:

    您可以将此规则用作从 URL 中删除 www.html 的第一条规则:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
    RewriteCond %{THE_REQUEST} \.html[\s?] [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^(.+?)(?:\.html)?$ http://%1/$1 [R=301,L,NC,NE]
    
    # now add .html internally
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.+?)/?$ $1.html [L]
    

    【讨论】:

    • 它似乎奏效了,但实际上我得到了这个:site.name/about。 (最后有个点)
    • 对不起,我有一个。错字。清除浏览器缓存后尝试我编辑的答案。
    • 感谢您更正代码,@anubhava,但它仍然无法正常工作:/ 当我在我网站的主页上并单击主菜单的任何链接时,我收到了一些东西像“site.name/about”一样,witch 是正确的,但页面没有显示任何内容。我试图在我的链接(href)中输入两种格式:有和没有.html。结果是一样的——页面不显示。
    • 显示的东西可能是由于 css/js/images 使用了亲属路径。在禁用缓存的 Chrome 开发工具中进行测试,并在“网络”选项卡中检查您得到的 404 是什么。
    • 您能否展示您现有的完整 .htaccess,以便我可以给您一个答案以适应它。
    猜你喜欢
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 2016-10-01
    • 1970-01-01
    • 2017-08-28
    • 2012-08-15
    • 2015-07-29
    相关资源
    最近更新 更多