问题描述
我很抱歉,如果这个问题是一个重复的,但我看了网上的一切困惑我很多,所以我再次发布了这个问题,指定我的需求。
I am sorry if this question is a duplicate but everything I looked for online confused me a lot so I am posting again this question specifying my needs.
因为我希望我的网站结构是干净的,我想分开配置的内容,所以我把所有页面中的 /应用程序
文件夹(很像一个Rails应用程序)。该网站的结构将是pretty就像这样:
Since I want the structure of my website to be clean, I want to separate content from configurations, so I am putting all pages in the /app
folder (much like a rails app). The structure of the website would be pretty much like this:
.
├── app
│ ├── index.php
│ └── products
│ └── index.php
├── configs
└── layouts
我发现了如何的重定向的根在 /应用程序
文件夹中的的.htaccess
是这样的:
I found out how to redirect the root to the /app
folder in the .htaccess
in this way:
RewriteEngine on
RewriteRule ^$ /app [L]
当我访问服务器,Apache的重定向到 /应用程序
文件夹,网址为:本地主机/应用程序
When I access the server, apache redirects to the /app
folder and the url is: localhost/app
.
我要的重写的网址,以删除 /应用程序
。例如,当我访问 /产品/
文件夹,我不想要的网址看起来像这样:本地主机/应用/产品/
我希望它看起来像这样:本地主机/产品/
I want to rewrite urls in order to delete /app
. For example when I visit the /products/
folder I don't want the url to look like this: localhost/app/products/
I want it to look like this: localhost/products/
我GOOGLE了解决方案,我发现的是,pretty很像它的窝preSS做的,你只需要添加:
I googled the solution and I've found out that, pretty much like it's done for worpress, you only have to add:
RewriteEngine on
RewriteRule ^$ /app [L]
RewriteRule %{REQUEST_URI} ^/app/.*
RewriteRule ^(.*)$ app/$1 [L]
然而,这是行不通的和apache返回 500内部服务器错误
。
我试过:
RewriteEngine on
RewriteRule ^$ /app [L]
RewriteRule %{REQUEST_URI} !^/app/
RewriteRule ^(.*)$ app/$1 [L]
和
RewriteEngine on
RewriteRule ^$ /app [L]
RewriteRule %{REQUEST_URI} !^/app/.*
RewriteRule ^(.*)$ app/$1 [L]
和都返回 500内部服务器错误
。
我也试过用:
RewriteEngine on
RewriteRule ^$ /app [L]
RewriteCond %{REQUEST_URI} !^/app/
RewriteRule ^(.*)$ app/$1 [L]
但 /应用程序
仍然在URL !!
我怎么能这样做?
推荐答案
尝试:
RewriteEngine on
#--Redirect /app/foobar to /foobar--#
RewriteCond %{THE_REQUEST} /app/([^\s]+) [NC]
RewriteRule ^ /%1 [NC,L,R]
#--Rewrite /foobar to /app/foobar--#
RewriteCond %{REQUEST_URI} !^/app
RewriteRule ^(.*)$ /app/$1 [NC,L,QSA]
这篇关于如何重定向到子文件夹,然后重写子文件夹的链接到根htaccess的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!