【问题标题】:.htaccess redirect all subpages to parent page.htaccess 将所有子页面重定向到父页面
【发布时间】:2014-03-28 16:15:15
【问题描述】:

我想要实现的是像这样重定向页面:

http://mydomain.com/parent/child_no1
http://mydomain.com/parent/child_no2
http://mydomain.com/parent/child_no3

无论 /parent/ 之后发生什么,我都希望它重定向到 /parent/index.php

我在 stackoverflow 上查看过它,但有很多类似的主题没有答案。所以我试着自己去研究它。

以下是它不起作用的原因:

RewriteRule ^/parent/.*$ http://mydomain.com/parent/ [R=301,L]

RewriteRule ^/parent/(.*)$ http://mydomain.com/parent/ [R=301,L]

RewriteRule ^parent(/(.*))$ http://mydomain.com/parent/ [R=301,L,NC]

RedirectMatch 301 ^/parent/.*$ http://mydomain.com/parent/

问题是访问http://mydomain.com/parent/ 也会被重定向,从而导致重定向循环。

有人对此有解决方案吗?

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

    试试这个,(使用 url-rewriting 转发请求)

    RewriteEngine On
    RewriteRule ^parent/(.*)$ /index.php?$1 [QSA,L]
    

    编辑

    进行实际重定向的另一种解决方案

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/parent/index.php$
    RewriteRule ^parent/.*$ http:/parent/index.php [R=301,QSA,L]
    

    【讨论】:

      【解决方案2】:

      以下应该有效:

      RewriteEngine On
      RewriteCond %{REQUEST_URI} !=/parent/index.php
      RewriteCond %{REQUEST_URI} !=/parent/
      RewriteRule ^parent/(.*)$ http://www.mydomain.com/parent [r=302,L]
      

      除了parent/index.phpparent/ 之外,应该将所有页面重定向到parent/ 的内容

      当您确定重定向正常工作时,将 R=302 更改为 R=301

      【讨论】:

        【解决方案3】:

        这条规则应该照顾它:

        RewriteEngine On
        RewriteRule ^parent/(?!index\.php).+)$ /parent/index.php [L,NC]
        

        【讨论】:

          猜你喜欢
          • 2016-07-20
          • 1970-01-01
          • 1970-01-01
          • 2022-06-30
          • 2021-03-05
          • 1970-01-01
          • 2012-10-29
          • 1970-01-01
          相关资源
          最近更新 更多