简体   繁体   English

如果使用htaccess存在目录,则重定向到目录

[英]Redirect to directory if directory exists using htaccess

I have about 250 directories off of public_html that I want to move into /clients/. 我要移到/ clients /中的public_html大约有250个目录。 Since the original URL's eg mysite.com/bob are already used in the client's printed marketing materials, I need to keep them accessible. 由于原始URL(例如mysite.com/bob)已用于客户的印刷营销材料中,因此我需要使它们保持可访问性。 I'd like for when someone goes to mysite.com/bob (example), htaccess checks if mysite.com/agents/bob exists, then redirect to it, if it does. 我想要有人访问mysite.com/bob(例如)时,htaccess检查mysite.com/agents/bob是否存在,然后重定向到它(如果存在)。 I've checked SO posts and came up with this so far, but am at a block trying to get it to work for any name scenario. 到目前为止,我已经检查了SO帖子并提出了这个建议,但是在试图使其适用于任何名称方案的过程中都处于障碍之中。

Options +FollowSymLinks
RewriteEngine On

ErrorDocument 404 /error_page.php

## check if path exists
## example: mysite.com/agents/bob

RewriteCond %{REQUEST_FILENAME} !-d

## if mysite.com/agents/bob exists, redirect mysite.com/bob 
## to mysite.com/agents/bob    

## The code below works - but just for bob. Want it to work for any name 
## e.g. mysite.com/tom, mysite.com/fred - to be redirected to 
## mysite.com/agents/tom, mysite.com/agents/fred respectively
## if mysite.com/agents/tom, mysite.com/agents/fred exist on the webserver

RewriteCond %{HTTP_HOST} ^mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^bob\/?$ "http\:\/\/mysite\.com\/agents\/bob" [R=301,L]

At your code , Replace last line : 在您的代码中,替换最后一行:

RewriteRule ^(.+)\/?$ "http\:\/\/mysite\.com\/agents\/bob" [R=301,L]

With this : 有了这个 :

RewriteCond %{REQUEST_URI} !/agents/
RewriteCond %{REQUEST_URI} !/error_page.php
RewriteRule ^(.+)\/?$     /agents/$1 [R=301,L]

So, the code above will redirect all request to /agents/ directory and exclude any request contains /agents/ to avoid looping then if the request is wrong error page will handle that and i excluded as well from this rule. 因此,上面的代码会将所有请求重定向到/agents/目录,并排除任何包含/agents/的请求以避免循环,然后,如果请求错误,错误页面将处理该问题,并且我也将此规则排除在外。

If you want to check first in /agents/ then in another location let me know to add another rules. 如果您要先在/agents/检查,然后再在其他位置检查,请告诉我添加其他规则。

Clear browser cached the test it . 清除浏览器缓存的测试吧。

UPDATE : 更新:

Try this instead : 试试这个代替:

RewriteCond %{REQUEST_URI} !/bob/agents/
RewriteRule ^bob/(.+)\/?$     /agents/bob/$1 [R=301,L]

UPDATE 2 更新2

RewriteCond %{THE_REQUEST} !/agents/
RewriteCond %{DOCUMENT_ROOT}/agents%{REQUEST_URI} -f
RewriteRule ^(.*)$    /agents%{REQUEST_URI} [L,R=301]

The code above will check any request not contains /agents/ even to site.com/index then check /agents/ directory , if it is exist ok ,it will be redirect to /agents/ , otherwise will handle it as it is . 上面的代码将检查任何不包含/agents/请求,甚至是对site.com/index请求,然后检查/agents/目录,如果存在,则将其重定向到/agents/ ,否则将按原样处理。

If you want to exclude index let me know 如果您想排除index告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM