htaccess的沉默重定向到子目录:子目录显示时没有尾随'/' [英] htaccess Silent Redirect to Subdirectory: Subdirectory showing when no trailing '/'

查看:194
本文介绍了htaccess的沉默重定向到子目录:子目录显示时没有尾随'/'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经挖高,围绕谷歌和计算器低,试图找出我的问题,尝试了无数的解决方案,但没有完全奏效。

I have dug high and low around Google and StackOverflow to try and figure out my problem, trying countless solutions but nothing has completely worked.

我在寻找移动主域名的web根我的服务器上的一个子目录。我现在有一台服务器的路径到我的网站根目录:

I'm looking to move the web root of the main domain on my server to a sub-directory. What I have currently for a server path to my web root:

/home/user/public_html/MyWebFilesHere

我在寻找什么有:

What I'm looking to have:

/home/user/public_html/subdir/MyWebfilesHere

当我浏览到mydomain.com,应该有,虽然没有明显的区别(即子目录重定向后不可见)。

When I browse to mydomain.com, there should be no visible difference though (i.e. "subdir" not visible after redirect).

不幸的是,我仅限于使用.htaccess文件纯粹是这样,因为我在共享主机和不具备访问Apache的配置文件,这样的。 :(

Unfortunately, I am restricted to doing this purely with a .htaccess file since I'm on shared hosting and don't have access to Apache config files and such. :(

我现在有在我的public_html的.htaccess是:

What I currently have in my .htaccess in public_html is:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteRule ^(.*)$ /subdir/$1 [L]

本成功重定向所有查询到子目录,但是有一个很奇怪的问题。如果我去

This successfully redirects all queries to the sub-directory, however there's a really weird issue. If I go to

mydomain.com/Contact/

它的伟大工程,重定向查询到的路径/子目录/联系人/但只留下地址栏。如果我去

it works great, redirecting the query to the path /subdir/Contact/ but leaving the address bar alone. If I go to

mydomain.com/Contact

(注意没有尾随'/'的),虽然,是什么在地址栏中显示为

(Note the lack of a trailing '/') though, what shows in the address bar is

mydomain.com/subdir/Contact/

这不是我想要的,因为子目录是显示。

which isn't what I want since "subdir" is showing.

有关我的现场实际工作的例子,尝试浏览到

For a working example on my actual site, try browsing to

colincwilliams.com/Contact/

相比

colincwilliams.com/Contact

难道你们对如何默默带或不带斜线,使这项工作任何想法?

Do you guys have any ideas on how to make this work silently both with and without a trailing slash?

推荐答案

这是可能发生的,因为这会自动将浏览器重定向如果目录的请求缺少斜线,以同样的事情mod_dir(模块的的一个斜线,请参见 DirectorySlash指令在mod_dir

This is probably happening because mod_dir (the module that automatically redirects the browser if a request for a directory is missing a trailing slash to the same thing with a trailing slash. See the DirectorySlash directive in mod_dir

这是怎么回事是:

  1. 您请求: mydomain.com/Contact
  2. 在mod_dir不碰这个,因为 /联系方式不是目录
  3. /联系方式被改写为 /子目录/联系方式内部的重定向
  4. mod_dir看到了 /子目录/联系方式是一个目录,缺少结尾的斜线所以它的浏览器重定向 mydomain.com/subdir/Contact /
  5. 所以,现在,您的浏览器的地址栏中有/子目录/在里面。
  1. You request: mydomain.com/Contact
  2. mod_dir doesn't touch this since /Contact isn't a directory
  3. /Contact gets rewritten to /subdir/Contact and internally redirected
  4. mod_dir sees that /subdir/Contact is a directory and missing the trailing slash so it redirects the browser to mydomain.com/subdir/Contact/
  5. So now, your browser's location bar has the /subdir/ in it.

您可以在你的.htaccess中添加 DirectorySlash关闭关闭mod_dir从重定向。但是,如果你想要的目录有尾随斜线,您可以添加一个单独的条件吧。根据你已经有了,我们可以把它扩展到这一点:

You can add DirectorySlash off in your .htaccess to turn off mod_dir from redirecting. But if you want directories to have trailing slashes, you can add a separate condition for it. Based on what you already have, we can expand it to this:

RewriteEngine on

# Has a trailing slash, don't append one when rewriting
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{THE_REQUEST} ./\ HTTP/1\.[01]$ [OR]
# OR if it's a file that ends with one of these extensions
RewriteCond %{REQUEST_URI} \.(php|html?|jpg|gif|css)$
RewriteRule ^(.*)$ /subdir/$1 [L]

# Missing trailing slash, append one
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{THE_REQUEST} [^/]\ HTTP/1\.[01]$
# But only if it's not a file that ends with one of these extensions
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif|css)$
RewriteRule ^(.*)$ /subdir/$1/ [L]

请注意:!我改变了 ^ / MYDOMAIN / ^ /子目录/ ,想通这是一个错字原因无它,mod_rewrite的将内部无限循环(FOO - > /子目录/富 - > /子目录/子目录/富 - > /子目录/子目录/子目录/富等)。如果我是错的,你可以改回。

Note: I changed !^/mydomain/ to !^/subdir/, figured it was a typo because without it, mod_rewrite would loop internally indefinitely (foo -> /subdir/foo -> /subdir/subdir/foo -> /subdir/subdir/subdir/foo, etc). If I got that wrong, you can change it back.

编辑:看我补充的RewriteCond的匹配对 \(PHP | HTML | JPG | GIF | CSS?)。这些是获得通过没有得到尾随斜线的文件扩展名添加。您可以添加/删除,以满足您的需求。

See my additions of RewriteCond's matching against \.(php|html?|jpg|gif|css). These are the file extensions that get passed through without getting trailing slashes added. You can add/remove to suit your needs.

这篇关于htaccess的沉默重定向到子目录:子目录显示时没有尾随'/'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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