重定向到subdir(使用REQUEST_URI和perdir中的提取)

时间:2016-01-11 20:54:37

标签: .htaccess mod-rewrite url-rewriting

我通常使用以下.htaccess将不存在的URL映射到index.php:

RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=REWRITE_BASE:%1]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:REWRITE_BASE}index.php/$1 [QSA,L]

这允许我拥有一个动态的重写库。

但是,现在我正在尝试修改此.htaccess,以便我可以将所有请求重定向到子目录,但是现在所有请求都将转到index_dev.php,甚至是现有文件......以下是我目前的情况:

RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::(\2)$
RewriteRule ^(.*)$ - [E=REWRITE_BASE:%1]

# modify request_filename
RewriteCond %{REQUEST_FILENAME} ^(.*?)(/web)*(/.*)$
RewriteRule ^(.*)$ - [E=WEB_REQUEST_FILENAME:%1/web%3]

# If file exists under web / redirect it
RewriteCond %{ENV:WEB_REQUEST_FILENAME} -f
RewriteRule ^(.*)$ %{ENV:REWRITE_BASE}web/$1 [QSA,L]

# If file doesn't exist, redirect to web/index_dev.php.
RewriteCond %{ENV:WEB_REQUEST_FILENAME} !-f
RewriteCond %{ENV:WEB_REQUEST_FILENAME} !/web/index_dev.php$
RewriteRule ^(.*)$ %{ENV:REWRITE_BASE}web/index_dev.php/$1 [QSA,L]

我的文件列出了以下内容:

<root>
|   .htaccess
|   web
|   |   index_dev.php
|   |   js
|   |   |   website.js
|   src
|   |   (PHP Files)
|   vendor
|   |   (PHP Files)

我希望你明白这一点。

1 个答案:

答案 0 :(得分:1)

我建议保持简单并让你的.htaccess像这样:

RewriteEngine On

RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=REWRITE_BASE:%1]

# If file exists under web / redirect it
RewriteCond %{DOCUMENT_ROOT}%{ENV:REWRITE_BASE}web/$1 -f
RewriteRule ^(.*)$ web/$1 [L]

# If file doesn't exist, redirect to web/index_dev.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!web/index_dev\.php).*)$ web/index_dev.php/$1 [NC,L]