重定向到子目录并出子目录 [英] redirect into subdirectory AND out of subdirectory

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

问题描述

我有一个mod_rewrite的重定向问题,我想不通。

i have a mod_rewrite redirection problem i cannot figure out.

从特定域的所有请求得到悄无声息改写成指定的子目录。例如 www.mydomain.net/hello.html 检索 /net/hello.html 文件。下面的.htaccess的(放在我的托管根)的实现这种完美:

all requests from a specific domain get "silently" rewritten into a designated subdirectory. e.g. www.mydomain.net/hello.html retrieves the file in /net/hello.html. the following .htaccess (placed in my hosting root) achieves this perfectly:

RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200 # <-- i will need this later. read to the end of the post.
RewriteRule .* - [L]
rewriteCond %{HTTP_HOST} ^www.mydomain.net$
rewriteCond %{REQUEST_URI} !^/net.*$
rewriteRule (.*) /net/$1 [L]

然而,直接网址进入该目录但明显应该用301到URL的重定向没有的该子目录。例如 www.mydomain.net/net/hello.html 应重定向到 www.mydomain.net/hello.html (这比仍在检索 /net/hello.html 文件)。我.htacces文件中查找该(放在 /净的遗憾的是不工作:

however, direct URLs into this directory however should visibly redirect with a 301 to the URL without that subdirectory. e.g. www.mydomain.net/net/hello.html should redirect to www.mydomain.net/hello.html (which than still retrieves the file in /net/hello.html). my .htacces file for this (placed in /net) unfortunately doesn't work:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule ^(.*) /$1 [R=301,L]    

我得到一个不定式重定向尽管的RewriteCond%{ENV:REDIRECT_STATUS}循环200 块根.htaccess文件...所以有什么不对?

i get an infinitive redirect loop despite the RewriteCond %{ENV:REDIRECT_STATUS} 200 block in the root .htaccess file... so what's wrong?

顺便说一句,我必须使用mod_rewrite,因为该网站是externaly托管和我有到Apache的configs进不去。

btw, i have to use mod_rewrite, because the site is externaly hosted and i have no access to the apache configs.

非常感谢任何指针。

推荐答案

检查的 HTTP请求行中的 THE_REQUEST 的,而不是:

Inspect the HTTP request line in THE_REQUEST instead:

RewriteCond %{THE_REQUEST} ^GET\ /net[/? ]
RewriteRule ^net($|/(.*)) /$2 [L,R=301]

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

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