【问题标题】:.htaccess main domain to subfolder, but /subfolder is still reachable.htaccess 主域到子文件夹,但 /subfolder 仍然可以访问
【发布时间】:2017-07-11 02:47:39
【问题描述】:

我在托管平台 http://example.com 上有一个托管域 http://example2.com

问题是,如果我转到http://example2.com/example2/fr,我不会被重定向到http://example2.com/fr,我已经尝试了解决方案,但仍然有重定向循环。

在 public_html 文件夹中,有一个子文件夹 /example2,其中包含 http://example2.com 的文件

> public_html
  - .htaccess
  - index.php
  > assets
  > css
  > js
  > example2
    - .htaccess
    - index.html
    > fr
      - index.html
    > en
      - index.html

现在,在 public_html 文件夹中,我的 .htaccess 文件中的重定向规则是:

<IfModule mod_rewrite.c>
  RewriteEngine on

  # Redirect the example2 to the subfolder /example2
  RewriteCond %{HTTP_HOST} ^(www\.)?example2\.com$ [NC]
  RewriteRule !^example2/ /example2%{REQUEST_URI} [L,NC]
</IfModule>

在 /example2 文件夹中,我的 htaccess 文件重定向如下:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

  RewriteRule ^index\.html$ / [R=301,L]
  RewriteRule ^(.*)/index\.html$ /$1 [R=301,L]

  #RewriteRule ^(.*)$ /$1 [R=301,L]
</IfModule>

【问题讨论】:

  • 如果您删除 /subfolder/.htaccess 会发生什么?

标签: apache .htaccess mod-rewrite


【解决方案1】:

你的/example2/.htaccess应该是这样的:

RewriteEngine On

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

RewriteRule ^index\.html$ / [R=301,NC,L]

RewriteRule ^(.*)/index\.html$ /$1 [R=301,NC,L]

# remove /example2/ from URLs
RewriteCond %{THE_REQUEST} \s/+example2(/\S*)\s [NC]
RewriteRule ^ %1? [R=301,L,NE]

【讨论】:

  • 非常感谢。像魅力一样工作:)
猜你喜欢
  • 1970-01-01
  • 2016-05-26
  • 2016-01-18
  • 2014-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多