apache 在 RewriteCond 文件中使用改变的 %{REQUEST_URI} 存在检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/873022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:36:54  来源:igfitidea点击:

Using altered %{REQUEST_URI} in RewriteCond file exists check

apache.htaccess

提问by Alistair

I'm wanting to take a URL such as http://localhost/universe/networks/o2/o2_logo.gifand do the following:

我想要一个 URL,例如http://localhost/universe/networks/o2/o2_logo.gif并执行以下操作:

If it begins with /universe/
Strip /universe/ from it to become /networks/o2/o2_logo.gif
Check if this exists in %{DOCUMENT_ROOT}/networks/o2/o2_logo.gif
If so, silently rewrite the request to this file.

If I use a rule of:

如果我使用以下规则:

RewriteCond %{REQUEST_URI} ^/universe/
RewriteRule ^(.*)$ http://www.example.org/ [L]

http://localhost/universe/networks/o2/o2_logo.gifgets re-written to http://www.example.org/networks/o2/o2_logo.gifwhich is great, but I can't seem to use the

http://localhost/universe/networks/o2/o2_logo.gif被重新写入http://www.example.org/networks/o2/o2_logo.gif这很棒,但我似乎无法使用

How can I go about using this 'changed' %{REQUEST_URI} further down as say $1 or something?

我怎样才能继续使用这个“改变”的 %{REQUEST_URI} 进一步向下说 $1 或其他东西?

回答by Alistair

I was not able to get this right purely with .htaccess but used the following file:

我无法完全使用 .htaccess 来解决这个问题,但使用了以下文件:

RewriteEngine On

# Does the file exist in the content folder?
RewriteCond %{REQUEST_URI} !^/content.*
RewriteCond %{DOCUMENT_ROOT}/universe/%{REQUEST_URI} -f

# File Exists
RewriteRule ^ %{DOCUMENT_ROOT}/universe/%{REQUEST_URI} [L]

# If the request is not a file, link or directory then send to index.php
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L] # Item exists so don't rewrite

# Nothing exists for the request so append a trailing / if needed
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [R=301,L]

RewriteRule ^.*$ index.php [NC,L] # Send off as-is to index.php

Then I went into my document_root folder and did the following:

然后我进入我的 document_root 文件夹并执行以下操作:

cd /var/www/journal
ln -s journal/public universe
cd /var/www/journal/public
ln -s content/ universe

That combined with the htaccess meant everything was working.

结合 htaccess 意味着一切正常。

回答by Gumbo

Try this rule:

试试这个规则:

RewriteCond %{DOCUMENT_ROOT} -f
RewriteRule ^universe/(.*)  [L]