htaccess redirect folder to subfolder

与世无争的帅哥 提交于 2020-01-22 03:13:27

问题


Notice: I wrote the solution. Thank you anyway. :)

I know it seems duplicated question: I searched a lot, I applied tens of rewrite rules but they don't work.

I need to redirect from

www.domain.com/folder1

to

www.domain.com/folder1/subfolderA

Note: folder1 is emtpy. in subfolderA I have index.html, index.php, css file, js files. If I go to "www.doman.com/folder1/subfolderA/" I see a simple web page with correct css and js provided by index.php file.

My .htaccess file is in www.domain.com/ (I've tried to put it in folder1 but it doesn't work, even removing "RewriteBase /" line). File permssions: 0755.

"Option -Indexes","Option +Indexes" generate error "Option indexes not allowed here", so I deleted them.
"Options +FollowSymLinks" generates error "Option FollowSymLinks not allowed here" and I deleted it.

First try: endless url

RewriteBase /
RewriteRule ^/folder1(/.*)?$ /folder1/subfolderA$1 [L,R]
(rif: https://stackoverflow.com/a/32525136/1315873) ('R' is just for debug)


Results:
www.domain.com/folder1/subfolderA/subfolderA/subfolderA/subfolderA/subfolderA/

Second try: adding a condition to stop endless loop:

RewriteBase /
RewriteCond %{HTTP_HOST} !subfolderA [NC]
RewriteRule ^/folder1(/.*)?$ /folder1/subfolderA$1 [L,R]

Perfect for http://htaccess.madewithlove.be/ but not for my server. Results:
www.domain.com/folder1/subfolderA/subfolderA/subfolderA/subfolderA/subfolderA (unchanged)

Third try: using $ after 'folder1'

RewriteBase /
RewriteCond %{HTTP_HOST} !subfolderA [NC]
RewriteRule ^/folder1/$ /folder1/subfolderA/ [L,R]

Results:
Forbidden
Log says: "No matching DirectoryIndex (index.html,index.htm,index.php,index.php3,index.phtml,index.shtml,index.wml) found, and server-generated directory index forbidden by Options directive"

If I add to the url "index.php" or "index.html" the result is "404 Not found" (that's true actually).

I don't know what else to do. I hope in your help.
It's an impossibile redirect to do?
Thank you.


回答1:


You can create an index.php or index.html page in folder1 that will redirect users to subfolderA

e.g index.php can contain the following code

              <?php
                header("location: subfolderA");
                 ? >



回答2:


I find the right way to do :P The .htaccess file look like this:

RewriteBase /
RewriteCond %{REQUEST_URI} !subfolderA [NC]
RewriteRule ^/?folder1(/.*)?$ /folder1/subfolderA$1 [L,R]

As I don't wont to change url in browser (one must see "www.domain.com/folder1/"), I remove ",R" from last line.

I hope it can be of help to someone else.



来源:https://stackoverflow.com/questions/44873033/htaccess-redirect-folder-to-subfolder

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