htaccess mod-rewrite: link to subdirectory from root - neglect dirs in subdirectory?

风流意气都作罢 提交于 2019-12-11 11:15:16

问题


I asked part of this question a while ago on stackoverflow and this works perfect: es

I have a multi-domain-server each directory is correspondend to a domain-name. However my main-domain name www.mydomain.com is also a directory on my server and not linked to automatically.

This is why I use this in my root of the website …

root/.htaccess

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/my-website/
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -d
RewriteRule ^(.*)$ /my-website/$1 [L]

Now when I call www.my-website.com it automatically runs the /my-website directory. That's just fine. However in this directory I have another .htaccess file with this in it.

root/my-website/.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^/projects/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^/jobs/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^(/downloads/.+)/?$ assets/$1 [L,NC]

So this means that for this website some links like /projects/ are just "neglected" and don't actually exist.

However if I run this website now live on my server and due to the main-htaccess file in my root, the RewriteConditions in my sub-directory don't work and I get a "Not found"

How can I make this work together?


回答1:


Have it this way:

DocumentRoot/.htaccess:

RewriteEngine On
RewriteBase /

RewriteRule ^projects/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^jobs/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^(downloads/.+)/?$ assets/$1 [L,NC]

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

RewriteCond %{REQUEST_URI} !^/my-website/
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -d
RewriteRule ^(.*)$ /my-website/$1 [L]

/my-website/.htaccess:

RewriteEngine On
RewriteBase /my-website/

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



回答2:


I was posting this on serverfault, when question was deleted. You already have a working answer, but I thought I'll still post this here, so you can have some explanations about this:

When you have mod_rewrite instructions in a .htaccess in a subfolder, only those instructions will apply, those of the parent folder will not be applied. You can make the rules that are in the .htaccess of the parent folder apply by using the

RewriteOptions Inherit

directive in the subfolder. But then it could become a bit messy. And you need to remember that the rules in the parent folder are applied after the rewrite rules in the subfolder. Better you go with a solution like proposed in only 1 .htaccess file.

Your 3 last RewriteRule search for a "/" at the beginning, which they never have in a per directory context, but have in a per server context. So if you are in .htaccess file (per dir context) then the comparison is made with a string that has no / at the beginning. See the doc about the RewriteRule.



来源:https://stackoverflow.com/questions/27607911/htaccess-mod-rewrite-link-to-subdirectory-from-root-neglect-dirs-in-subdirect

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