Merge two .htaccess files from two projects into one

江枫思渺然 提交于 2020-06-28 03:45:21

问题


I have a project which I have to include within a project. So .. project A is a web site built on top of custom framework which uses this .htaccess rewrite rules:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?url=$1 [PT,L]

The second project B is built on top of another custom framework and I am supposed to make it look like a subfolder to the first one. So I placed project B inside a folder "subfolder". Project B has its own .htaccess which looks like this:

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?request=%{REQUEST_URI}&%{QUERY_STRING} [NE,L]

I want to merge them into a single .htacess file and tell to Apache something like :

if we have a parameter /subfolder redirect everything to subfolder/index.php

else redirect everything to index.php

Any ideas how can I do it?


回答1:


What you're supposed to do is:

For the first project, this htaccess file is in the document root

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]

Then in the second project, this htaccess file is in the subfolder directory:

RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=%{REQUEST_URI} [NE,L,QSA]

If for some reason that's not working for you, I guess a combined htaccess file, one that resides in the document root, would look like this:

RewriteEngine On

# for 2nd project
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?subfolder/(.*)$ /subfolder/index.php?request=$1 [NE,L,QSA]

# for 1st project
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?url=$1 [L]

Will probably need a bit more tweaking.



来源:https://stackoverflow.com/questions/11825018/merge-two-htaccess-files-from-two-projects-into-one

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