的.htaccess:重定向URL根到子目录,但保留根URL

编程入门 行业动态 更新时间:2024-10-25 10:27:48
本文介绍了的.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 ^(.*)$ domain.tld/$1 [R=301,L] #Trailing slash RewriteRule ^/*(.+/)?([^.]*[^/])$ %{HTTP_HOST}/$1$2/ [L,R=301] #Redirect root url to subdirectory RedirectMatch 301 ^/subdirectory/$ 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

本文发布于:2023-11-01 23:25:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550710.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子目录   重定向   htaccess   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!