【问题标题】:.htaccess: How to redirect all pages except admin to home page?.htaccess:如何将除管理员以外的所有页面重定向到主页?
【发布时间】:2014-07-10 07:37:49
【问题描述】:

我将主页修改为“建设中”页面。现在我想将站点内的所有页面重定向到“建设中”页面,除了管理部分。如何使用 .htaccess 做到这一点?

例如,我想将www.mydomain.com/contactwww.mydomain.com/profile/services 等页面重定向到主页。但我想保持所有管理页面(例如 www.mydomain.com/admin)仍然可以访问,因为我需要登录才能进行更改。

这就是我所拥有的:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>

我尝试了一些不同的方法,但似乎不起作用。

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

    您可以使用这些规则:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
    RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L,NE]
    
    RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
    RewriteRule !^admin / [L,NC,R=302]
    

    【讨论】:

    • 不,它似乎对子页面没有任何影响。相反,它还从主页中删除了图像。
    • 查看更新以修复图像/css(尽管您应该提到这个要求)。您还需要告诉我哪个 URI 没有被重定向到主页。
    【解决方案2】:

    如果是admin,测试一下怎么样,如果是,停止重写规则?

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
    
        # Testing if `admin` then if so, stop rewrite rules
        RewriteRule (.*)/admin(.*) - [QSA,L,NC]
    
        RewriteCond %{HTTP_HOST} !^www\.mydomain\.com
        RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME}/index.html !-f
        RewriteCond %{REQUEST_FILENAME}/index.php !-f
        RewriteRule . index.php [L]
    </IfModule>
    

    【讨论】:

      猜你喜欢
      • 2016-11-30
      • 2020-03-11
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多