的.htaccess:重定向URL根到子目录,但保留根URL [英] .htaccess: Redirect root url to subdirectory, but retain root url

查看:155
本文介绍了的.htaccess:重定向URL根到子目录,但保留根URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在清理我的域的目录结构(祸是我设立在根目录下!根URL的内容)的过程中,需要对如何正确使用重写规则的一些见解。

I am in the process of cleaning up my domain's directory structure (woe be to me for setting up the root url's content in the root directory!) and need some insight on how to use RewriteRule correctly.

的主旨

我要的 domain.tld 的使用的 domain.tld /子目录/ 的,但仍然出现的 domain.tld 的在URL中。

I want domain.tld to use domain.tld/subdirectory/ but still appear as domain.tld in the url.

我的.htaccess到目前为止

Options +FollowSymlinks
RewriteEngine On

#Force removal of wwww subdomain
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain.tld
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]

#Trailing slash
RewriteRule ^/*(.+/)?([^.]*[^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

#Redirect root url to subdirectory
RedirectMatch 301 ^/subdirectory/$ http://domain.tld/
RewriteRule ^/$ /subdirectory/?$ [L]

该RedirectMatch的伟大工程。不幸的是,T似乎最后重写规则存在的的工作根本不够,但事实并非如此。在根目录下的旧内容的设置仍然出现。

The RedirectMatch works great. Unfortunately, t seems the last RewriteRule there should work simply enough, but it doesn't. The old content setup in the root directory still appears.

我是什么在这里失踪?

更新:解决

简单的修复才能够解释为什么。

Simple fix that I am not experienced enough with the .htaccess / apache to be able to explain why.

我有:

RewriteRule ^/$ /subdirectory/?$ [L]

删除一个斜线固定的一切:

Removing the one slash fixed everything:

RewriteRule ^$ /subdirectory/?$ [L]

所以现在我的问题是:这是为什么

推荐答案

这两个规则的有效

这其中的在服务器配置或虚拟主机环境中使用网址开头斜线的):

RewriteRule ^/$ /subdirectory/ [L]

如果你放在同一个规则的的.htaccess ,然后你必须删除前导斜杠(,因为它是相对于当前文件夹的):

If you place the same rule in .htaccess, then you have to remove leading slash (because it is relative to the current folder):

RewriteRule ^$ /subdirectory/ [L]

P.S。
Floern已经提到关于?$ 它的目标URL的一部分。无需 $ 可言。如果用完成您的网址这告诉Apache不要过度到新的URL(降查询字符串完全)传递的查询字符串。

P.S. Floern already mentioned about ?$ it target URL part. No need for $ at all. If you finish your URL with ? this tells Apache to not to transfer query string over to new URL (drop query string completely).

此外, [QSA] 如果您在目标URL查询字符串(有操纵标志时,才需要),并仍想你当前的查询字符串被追加到新建的URL。这意味着,在这个规则重写规则^ $ /子目录/ [L,QSA] [QSA] 标志什么都不做在所有的,可以安全删除。

Additionally, [QSA] flag is only required if you manipulating with query string (have ? in target URL) and still would like your current query string be appended to the newly build URL. This means that in this rule RewriteRule ^$ /subdirectory/ [L,QSA] the [QSA] flag does nothing at all and can be safely removed.

这篇关于的.htaccess:重定向URL根到子目录,但保留根URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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