如何从根目录重定向到子文件夹,重写子文件夹链接以使其看起来像根目录,然后在htaccess中添加一些例外? [英] how to redirect to a subfolder from root, rewrite subfolder link to look like root, and then add some exceptions in htaccess?

查看:85
本文介绍了如何从根目录重定向到子文件夹,重写子文件夹链接以使其看起来像根目录,然后在htaccess中添加一些例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是一个重复的问题,请原谅.我在网上找到的东西根本让我不满意.

I beg you pardon if this is a duplicate question. What I've found on the net didn't satisfy me at all.

正如我在

As I asked in this question, I successfully rewrote the /public folder to the root url. So If I visit localhost or localhost/public apache redirects me to /public and hides public in the url. To sum up, I did it in this way:

RewriteEngine on

RewriteCond %{THE_REQUEST} /public/([^\s]+) [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_URI} !^/public
RewriteRule ^(.*)$ /public/$1 [NC,L,QSA]

但是,通过这种方式,所有内容都将重定向到子文件夹/public.我需要为不在/public文件夹中的某些子目录保留或添加一些例外.

However in this way everything will be redirected to the subfolder /public. I need to preserve, or to add some exceptions, to some subdirectories that are not in the /public folder.

+---assets
+---images
    \---jp-pattern.jpg
+---javascripts
+---stylesheets
+---template
+---public
    \---products

我需要保留资产,图像,javascript,样式表和模板文件夹.我需要它们成为例如localhost/images等.

I need to preserve assets, images, javascripts, stylesheets, and template folders. I need them to be for example localhost/images and so on.

我首先以这种方式尝试了images文件夹:

I tried with the images folder first in this way:

RewriteCond %{REQUEST_URI} !^/images [NC]

RewriteCond %{REQUEST_URI} !^/images/?$ [NC]

RewriteCond %{REQUEST_URI} !/images [NC]

但是他们都不起作用.浏览器总是向我返回以下消息:

but none of them worked. The browser always returns me this message:

Not Found
The requested URL /public/images/jp-pattern.jpg was not found on this server.

这意味着我无法添加例外.

This means that I couldn't add the exception.

如何添加这些例外?

推荐答案

您可以在RewriteEngine on之后直接添加此块:

You can add this block directly after RewriteEngine on:

RewriteCond %{REQUEST_URI} ^/(images|javascripts|stylesheets|template)/ [NC]
RewriteRule ^ - [L]

这表示如果请求以这些目录中的任何一个开头,请不要重写.使用L标志可以检查最后一条规则.

This says that if the request begins with either of those directories, don't rewrite. Use of the L flag makes that the last rule to be checked.

但是,我建议您实际上进行一个存在性检查.您的HTML可能会请求一个现有文件,因此无论如何都会跳过public检查.

However, I advise that you actually do an existence check instead. Your HTML would likely be requesting an existing file, and so it would skip the public check anyway.

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

这篇关于如何从根目录重定向到子文件夹,重写子文件夹链接以使其看起来像根目录,然后在htaccess中添加一些例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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