how to redirect to a subfolder and then rewrite subfolder link to root in htaccess?

扶醉桌前 提交于 2019-12-31 02:24:17

问题


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.

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

I found out how to redirect the root to the /app folder in the .htaccess in this way:

RewriteEngine on
RewriteRule ^$ /app [L]

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/

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]

however this just doesn't work and apache return the 500 internal server error.

I tried:

RewriteEngine on
RewriteRule ^$ /app [L]  
RewriteRule %{REQUEST_URI} !^/app/
RewriteRule ^(.*)$ app/$1 [L]

and

RewriteEngine on
RewriteRule ^$ /app [L]  
RewriteRule %{REQUEST_URI} !^/app/.*
RewriteRule ^(.*)$ app/$1 [L]

and both return 500 internal server error.

I also tried with:

RewriteEngine on
RewriteRule ^$ /app [L]  
RewriteCond %{REQUEST_URI} !^/app/
RewriteRule ^(.*)$ app/$1 [L]

but /app is still in the url!!

How can I do that?


回答1:


Try :

 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]


来源:https://stackoverflow.com/questions/35304201/how-to-redirect-to-a-subfolder-and-then-rewrite-subfolder-link-to-root-in-htacce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!