htaccess 重定向到带有斜杠的子文件夹

编程入门 行业动态 更新时间:2024-10-20 03:43:01
本文介绍了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 -> example/public/ example/foo/ -> example/public/foo/ example/foo/about.html -> example/public/foo/about.html

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

By the way, the following redirect has a problem:

example/foo -> example/public/foo

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

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

example/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 重定向到带有斜杠的子文件夹

本文发布于:2023-10-31 13:09:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   文件夹   重定向   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!