htaccess 重定向到带有斜杠的子文件夹 [英] htaccess redirect to subfolder with a trailing slash

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

问题描述

我成功地将所有请求重定向到我的网络服务器的一个子文件夹.

I successfully managed to redirect all requests to a subfolder of my web server.

这是我正在使用的代码:

This is the code I'm using:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

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

/public/.htaccess

# redirect all urls that doesn’t have a 
# trailing slash to urls with a trailing slash
# (this is my try)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$

RewriteRule ^(.*)$ $1 [NC]

它工作得很好,所以以下重定向工作:

It works perfectly, so the following redirects work:

example.com                 -> example.com/public/
example.com/foo/            -> example.com/public/foo/
example.com/foo/about.html  -> example.com/public/foo/about.html

顺便说一下,下面的重定向有问题:

By the way, the following redirect has a problem:

example.com/foo             -> example.com/public/foo

虽然重定向正确,但地址栏中的实际 URL 为:

Although it redirects correctly, the actual URL in the address bar is:

example.com/public/foo/

所以我的问题是:

希望这是有道理的.

推荐答案

将此代码放在根目录 .htaccess 中:

Place this code in root .htaccess:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R=301,L]

RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1/ [L]

然后删除 /public/.htaccess 因为不需要.

Then just remove /public/.htaccess since that is not needed.

这篇关于htaccess 重定向到带有斜杠的子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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