简体   繁体   English

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

[英]htaccess redirect to subfolder with a trailing slash

I successfully managed to redirect all requests to a subfolder of my web server. 我成功地将所有请求重定向到Web服务器的子文件夹。

This is the code I'm using: 这是我正在使用的代码:

/.htaccess /.htaccess

RewriteEngine on
RewriteBase /

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

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

/public/.htaccess /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

Although it redirects correctly, the actual URL in the address bar is: 尽管重定向正确,但地址栏中的实际URL为:

example.com/public/foo/

So my question is: 所以我的问题是:

How can I have a clean URL when redirecting from a URL without a forward slash? 从没有重定向斜杠的URL重定向时,如何获得一个干净的URL

Hope it makes sense. 希望有道理。

Place this code in root .htaccess: 将此代码放在根.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]

Then just remove /public/.htaccess since that is not needed. 然后只需删除/public/.htaccess因为这是不需要的。

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

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