htaccess-重定向到子目录时的规范URL [英] htaccess - canonical URL when redirecting to subdirectory

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

问题描述

我已经在这个问题上解决了很长时间了,但是找不到如何实现我想要的方法.我希望访问我的网站test.com的用户能够立即访问位于子目录www中的index.php,这是我能够做到的.但是当URL是test.com/www时,我希望它只是test.com.

I've been playing around with this problem for quite a while but can't find the way how to achieve what I want. I want user coming to my website test.com to reach index.php located in subdirectory www immediatelly, which I am able to do. But when the URL is test.com/www I want it to be just test.com.

代码段:

.htaccess

.htaccess in /

RewriteRule ^(.*)$ /www/$1 [L]

/p中的

.htaccess

.htaccess in /www

RewriteCond %{REQUEST_URI} ^/www.*$
RewriteRule ^/www/(.*)$ /$1 [L]

给我500内部错误.

更新: 感谢@Justin Iurman的回答,我找到了一个解决方案:

UPDATE: I came to a solution thanks to @Justin Iurman's answer:

.htaccess

.htaccess in /www

RewriteCond %{THE_REQUEST} \ /www/?(.*)\ HTTP/ [NC]
RewriteRule ^(.*)$ /%1 [R=301,L]

还有另一个问题.在服务器计算机上,我有多个网站,并且我想根据根.htaccess文件中的 HTTP_HOST 变量提供文件.我将访问test.com的用户重定向到正确的目录(例如test),但是我想将URL test.com/test重定向到test.com.目录测试包含目录www,在该目录中用户被重定向,并且由于上述解决方案,它不停留在URL上.我只想在Web服务器根目录中编辑.htaccess文件,所以我对项目中网站的域没有任何依赖.

Have another problem tho. On the server machine I have multiple websites and I want to serve files according to HTTP_HOST variable in root .htaccess file. I redirect user accessing test.com to proper directory (say test), but I want URL test.com/test to redirect just to test.com. Directory test contains directory www where user is redirected and it does not stick to URL thanks to solution above. I would like to edit just .htaccess file in webserver root so I don't have any dependancy on websites' domains in projects.

已解决

.htaccess:

.htaccess in webserver root:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Redirect test.com/test (and everything into test folder) to test.com root
    RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
    RewriteCond %{THE_REQUEST} \ /test/? [NC]
    RewriteRule ^.*$ / [R=301,L]

    # Forward (internally rewrite) test.com/one/example/page to test.com/test/www/one/example/page
    RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
    RewriteRule ^(.*)$ /test/www/$1 [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>

.htaccess在测试"目录中:

.htaccess in the "test" directory:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # allow /test in URL only for certain filetypes
    RewriteRule ^(.*\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar\.gz))$ /test/www/$1 [L]

    # allow /test on localhost
    RewriteCond %{HTTP_HOST} ^localhost$ [NC]
    RewriteRule ^(.*)$ /test/www/$1 [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>

非常感谢贾斯汀!

推荐答案

在执行所需操作时,必须避免无限循环.

You have to avoid infinite loop when doing what you want.

将此代码放在您的htaccess中

Put this code in your htaccess

RewriteEngine on

RewriteCond %{THE_REQUEST} \ /www/?(.*)\ HTTP/ [NC]
RewriteRule . /%1 [R=301,L]

RewriteRule ^(.*)$ /www/$1 [L]


考虑到您的更新,这是新代码


taking your update into consideration, here's the new code

RewriteEngine on

# Redirect test.com/test (and everything into test folder) to test.com root
RewriteCond %{HTTP_HOST} ^test.com$ [NC]
RewriteCond %{THE_REQUEST} \ /test/? [NC]
RewriteRule . / [R=301,L]

# Forward (internally rewrite) test.com/one/example/page to test.com/test/www/one/example/page
RewriteCond %{HTTP_HOST} ^test.com$ [NC]
RewriteRule ^(.*)$ /test/www/$1 [L]

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

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