沃梦达 / 编程问答 / php问题 / 正文

htaccess 将根重定向到子目录,但允许根和查询字符串中的 index.php 起作用

htaccess redirect root to subdirectory but allow index.php in root AND query strings to function(htaccess 将根重定向到子目录,但允许根和查询字符串中的 index.php 起作用)

本文介绍了htaccess 将根重定向到子目录,但允许根和查询字符串中的 index.php 起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的问题,已经回答了.然而,答案引出了另一个概念,现在我有以下问题.(用户anubhava"能够回答原始问题.)

I had a question similar to this, and it was answered. However, the answer led to another concept and now I have the following question. (User "anubhava" was able to answer the original question.)

让我解释一下,我们的情况是这样的:

Let me explain, our situation is this:

我们正在使用 Joomla 启动一个新网站,但是,我们希望旧网站的所有入站链接继续有效(因为我们在旧网站上的 WordPress 中有很多内容).因此,使用 htaccess,我们能够将所有对主域名的请求重定向到包含新 Joomla 网站的子目录.[例如,由于我们想要的 htaccess 代码,对 www.example.com 的请求被重定向到 www.example.com/PORTALsite.]

We are launching a new website using Joomla, however, we wanted all of the old website's in-bound links to continue to function (as we have a lot of content in WordPress on the old website). So, using htaccess, we were able to have all requests to the primary domain name redirect to a sub-directory with the new Joomla website in it. [So as an example, requests for www.example.com get redirected to www.example.com/PORTALsite due to the htaccess code, which is what we want.]

此外,此代码还允许对旧 WordPress 网站主页的请求仍然有效(我们正在替换的旧网站).[例如,www.example.com/index.php 仍然可以运行并加载旧的 WordPress 站点,这正是我们想要的.]

Further, this code also allows for requests to the Home page of the old WordPress website to still function (the old website we are replacing). [So as an example, www.example.com/index.php will still function and load the old WordPress site, which is what we want.]

仅当请求仅针对域名 (www.example.com) 时,它才会重定向到新的 Joomla 网站主页(位于 www.example.com/PORTALsite/),但如果请求是针对旧网站的主页(位于 www.example.com/index.php),它将允许加载旧的 WordPress 网站.

Only if the request is for the domain name alone (www.example.com) will it redirect to the new Joomla website's Home page (located at www.example.com/PORTALsite/), but if the request is for the old website's Home page (located at www.example.com/index.php) it will allow this and load the old WordPress website.

这是 htaccess 代码:

Here's the htaccess code:

RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/+[s?]
RewriteRule ^$ /PORTALsite [R=301,L]

然而,我现在的问题是:有很多入站链接,旧的 WordPress 网站的 index.php 页面被省略,只附加了一个查询字符串(在页面上表示一个页面名称)旧网站通过 WordPress).但是,由于上面的代码将此视为仅对主域名的调用(因为它忽略"了查询字符串),因此它还会将其重定向到新的 Joomla 网站.我们不希望这种情况发生!有什么帮助吗?

HOWEVER, my problem now is this: There are many in-bound links where the old WordPress website's index.php page is omitted and only a query string is attached (indicating a Page name on the old website via WordPress). However, because the code above sees this as a call just to the primary domain name alone (as it "ignores" the query string) it also redirects this to the new Joomla website. We do not want this to happen! Any help, please?

问题示例:

  1. (不是问题)请求主域名成功重定向到新的 Jooma 网站,因此 www.example.com 将重定向到www.example.com/PORTALsite,正如我们希望的那样.

  1. (not a problem) requests for the primary domain name are successfully redirected to the new Jooma website, thus www.example.com will redirect to www.example.com/PORTALsite, as we want and as it should.

(不是问题)www.example.com/index.php 成功加载旧的 WordPress网站的主页,正如我们希望的那样.

(not a problem) www.example.com/index.php successfully loads the old WordPress website's Home page though, as we want and as it should.

然而这是问题:没有文件名但包含一个查询字符串,例如 www.example.com/?page_id=56 也得到重定向到新的 Joomla 网站(例如,www.example.com/PORTALsite/?page_id=56),我们希望这种情况发生.

however this is the problem: in-bound links that have no file name but include a query string, such as www.example.com/?page_id=56 are also getting redirected to the new Joomla website (for example, www.example.com/PORTALsite/?page_id=56) AND WE DO NOT WANT THIS TO HAPPEN.

我们希望 www.example.com/?page_id=56 重定向到www.example.com/index.php?page_id=56 并从旧的 WordPress 网站.如何根据上面的 htaccess 代码实现这一点?

we want www.example.com/?page_id=56 to redirect to www.example.com/index.php?page_id=56 and load the content from the old WordPress site. how do I make this happen based upon the htaccess code above?

在此先感谢您的帮助!

推荐答案

RewriteEngine on
# Match requests for /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/+[s?]
# Don't match requests for page_id=n
RewriteCond %{QUERY_STRING} !page_id=[0-9]+
# Redirect to /PORTALsite
RewriteRule ^$ /PORTALsite [R=301,L]

这篇关于htaccess 将根重定向到子目录,但允许根和查询字符串中的 index.php 起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:htaccess 将根重定向到子目录,但允许根和查询字符串中的 index.php 起作用

基础教程推荐