Apache将所有子页面重定向到主页,特定页面除外 [英] Apache redirect all sub pages to home page except specific pages

查看:30
本文介绍了Apache将所有子页面重定向到主页,特定页面除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的网站结构:

www.domain.comwww.domain.com/subpage-awww.domain.com/subpage-bwww.domain.com/termswww.domain.com/request-information

我正在尝试配置一个 Apache 重写规则,它将所有子页面指向主页,禁止特定页面.

使用上面的示例,我需要将站点的所有子页面重定向到主页,比如 termsrequest-information 页面.>

我一直在寻找 StackOverflow,我发现了一些没有得到解答的问题,例如 这个这个.第二个链接似乎可以做我想做的事,但我不确定如何覆盖特定页面的规则.

我的配置

目前,我的 Apache 文件如下所示:

服务器名称 www.domain.co.ukProxyPreserveHost On重写引擎开启重写规则 ^/(.*)$ http://server:8000/VirtualHostBase/http/%{HTTP_HOST}:80/root/site/VirtualHostRoot/$1 [L,P]</虚拟主机><虚拟主机 *:80>服务器名称 domain.co.uk永久重定向/http://www.domain.co.uk/</虚拟主机>

为了让它工作,我附加了这个,只是为了看看我是否可以得到一个子页面来重定向:

服务器名称 http://www.domain.co.uk/subpage-a永久重定向/http://www.domain.co.uk/</虚拟主机>

这没有用,但由于该站点有很多子页面,我真的希望有更多的通用规则,某些子页面将忽略该规则.

这可能吗?有人有什么想法吗?

谢谢.

TL;DR

我正在寻找一种包罗万象"风格的 Apache 重写规则,它将重定向每个子页面,将某些页面禁止到域的主页.

编辑:

关于下面的答案,我尝试了以下方法:

答案 #1

我修改了我的代码,如下所示:

服务器名称 sub.domain.co.ukProxyPreserveHost On重写引擎开启RewriteCond %{REQUEST_URI} !^/(terms|request-information)(/.*|)$ [NC]重写规则 ^/(.*)$ http://server:8000/VirtualHostBase/http/%{HTTP_HOST}:80/root/site/VirtualHostRoot/$1 [L,P]</虚拟主机><虚拟主机 *:80>服务器名称 domain.co.uk永久重定向/http://www.domain.co.uk/</虚拟主机>

但是似乎 404 包含破折号的页面,我可以忍受,但不幸的是,它需要我列出我想重定向的所有页面来代替 terms|request-information 以上.

答案#2

我必须保留原来的重写规则,多个重写规则似乎在我的浏览器中给我一个这个网站没有正确重定向"的消息,我对此代码进行了复制和粘贴,修改了详细信息根据需要,但它导致了错误.

解决方案

www.domain.co.ukVirtualHost 部分中,在 下方插入此代码>RewriteEngine On 行:

RewriteCond %{REQUEST_URI} !^/(terms|request-information)(/.*|)$ [NC]重写规则 ^/[L,R=301]

多个重写规则好像会导致重定向错误,只用一个.

I have a site structure such as this:

www.domain.com
www.domain.com/subpage-a
www.domain.com/subpage-b
www.domain.com/terms
www.domain.com/request-information

I am trying to configure an Apache rewrite rule which will point all sub pages to the home page, bar specific pages.

Using the example above I would need to redirect all the sub pages of the site to the home page, bar say the terms and request-information pages.

I have been looking on StackOverflow and I've found a few questions that went unanswered, such as this one and this one. The second link seems to kind of do what I want but I'm not sure how to override my rule for specific pages.

My configuration

Currently, my Apache file looks like this:

<VirtualHost *:80>
 ServerName www.domain.co.uk

 ProxyPreserveHost On
 RewriteEngine On
 RewriteRule ^/(.*)$ http://server:8000/VirtualHostBase/http/%{HTTP_HOST}:80/root/site/VirtualHostRoot/$1 [L,P]
</VirtualHost>

<VirtualHost *:80>
 ServerName domain.co.uk
 Redirect permanent / http://www.domain.co.uk/
</VirtualHost>

In an attempt to get this working I appended this, just to see if I could get a sub page to redirect:

<VirtualHost *:80>
 ServerName http://www.domain.co.uk/subpage-a
 Redirect permanent / http://www.domain.co.uk/
</VirtualHost>

This didn't work but as there are a lot of sub pages to this site, I really would like more of a catch-all rule which would be ignored for certain sub pages.

Is this possible and does anyone have any ideas?

Thanks.

TL;DR

I'm looking for a 'catch-all' style Apache rewrite rule which will redirect every subpage, bar certain pages to the home page of the domain.

EDIT:

With regards to the answers below I have tried the following:

Answer #1

I modified my code to look like the following:

<VirtualHost *:80>
 ServerName sub.domain.co.uk

 ProxyPreserveHost On
 RewriteEngine On
 RewriteCond %{REQUEST_URI} !^/(terms|request-information)(/.*|)$ [NC] 
 RewriteRule ^/(.*)$ http://server:8000/VirtualHostBase/http/%{HTTP_HOST}:80/root/site/VirtualHostRoot/$1 [L,P]
</VirtualHost>

<VirtualHost *:80>
 ServerName domain.co.uk
 Redirect permanent / http://www.domain.co.uk/
</VirtualHost>

However it seems to 404 the pages containing a dash, I can live with this however unfortunately it requires me to list all of the pages I would like to redirect in place of the terms|request-information above.

Answer #2

I must preserve the original re-write rule, multiple re-write rules seem to give me a "This website is not redirecting correctly" message in my browser, I did a copy-and-paste on this code amending the details as necessary but it resulted witg the error.

解决方案

In the VirtualHost section of www.domain.co.uk insert this code just below RewriteEngine On line:

RewriteCond %{REQUEST_URI} !^/(terms|request-information)(/.*|)$ [NC]
RewriteRule ^ / [L,R=301]

EDIT: Multiple rewrite rules seem to cause a redirection error, only use one.

这篇关于Apache将所有子页面重定向到主页,特定页面除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆