htaccess to rewrite file in subfolder (2nd level) to folder (1st level)

ⅰ亾dé卋堺 提交于 2019-12-24 16:40:20

问题


Currently, I'm using the below htaccess code to load http://www.domain.com/folder/file.php from http://www.domain.com/file, removing the folder name "/folder/", and the file extension ".php", from the URL.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI}\.php -f [OR]
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI} -d
RewriteRule ^(.*)$ /folder/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Now, I'm trying to do almost the same, but with a file that is stored in a subfolder.

So, in other words, I want to call http://www.domain.com/folder/subfolder/file.php from http://www.domain.com/subfolder/file

In this case, hiding the extension file is not a problem as the 2 last lines from the code above works globally, the issue is with the Rewriting which doesn't work simply by adding the 'subfolder' name. I thought this was going to work, but it wasn't:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/folder/subfolder%{REQUEST_URI}\.php -f [OR]
RewriteCond %{DOCUMENT_ROOT}/folder/subfolder%{REQUEST_URI} -d
RewriteRule ^(.*)$ /folder/subfolder/$1.php [L]

I appreciate any help.


回答1:


Replace both of your rules with this:

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/folder/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /folder/$1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]


来源:https://stackoverflow.com/questions/25563255/htaccess-to-rewrite-file-in-subfolder-2nd-level-to-folder-1st-level

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