【问题标题】:Htaccess, redirect if folder existsHtaccess,如果文件夹存在则重定向
【发布时间】:2015-09-23 16:16:20
【问题描述】:

如果文件夹安装存在,我会尝试重定向用户,但到目前为止我没有运气。

下面是我的 htaccess 文件。

RewriteEngine On
RewriteBase /enc/project/
# If the setup directory exists in the document root...
RewriteCond %{DOCUMENT_ROOT}/install -d
# ...and you're not in isntall..
RewriteCond %{REQUEST_URI} !(install) [NC]
# ...redirect to setup - with a 302 (temporary redirect) to make sure the browser won't cache the redirect.
RewriteRule ^(.*) /install [L,redirect=302]
# ...rest of htaccess for codeigniter
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

codeigniter 项目在我服务器的/enc/project/ 中,这就是 RewriteBase 的原因。

Project Structure is

htdocs/enc/project/
|-- application/
|   |-- project
|
|-- system/
|   |-- project
|
|-- install/
|   |-- test/
|   |   |-- test_main.php
|   |   
|   |-- index.php
|
|-- htaccess

【问题讨论】:

  • localhost/enc/project
  • 我用我的项目结构编辑了这个问题
  • 我现在就试一试,然后告诉你。谢谢
  • 是的,问题出在这个问题上,尽管我得到一个空白页并将我尝试加载的每个 css 请求或文件重定向到安装文件夹

标签: php regex .htaccess codeigniter mod-rewrite


【解决方案1】:

这是一种有效的编写方式:

RewriteEngine On
RewriteBase /enc/project/
# If the setup directory exists in the document root...
RewriteCond %{DOCUMENT_ROOT}/enc/project/install -d
# ...and you're not in isntall..
RewriteCond %{REQUEST_URI} !(install) [NC]
# ...redirect to setup - with a 302 (temporary redirect) to make sure the browser won't cache the redirect.
RewriteRule ^(.*) /enc/project/install/ [L,redirect=302]
# ...rest of htaccess for codeigniter
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

【讨论】:

  • 是的,问题出在这个问题上,尽管我得到一个空白页并重定向每个 css 请求或我试图加载到安装文件夹的文件
【解决方案2】:

试试这些规则

RewriteEngine On
RewriteBase /enc/project/

# If the setup directory exists in the document root...
RewriteCond %{DOCUMENT_ROOT}/enc/project/install -d
# ...and you're not in isntall..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# ...redirect to setup - with a 302 (temporary redirect) to make sure the browser won't cache the redirect.
RewriteRule !^install/ install/ [L,NC,R=302]

# ...rest of htaccess for codeigniter
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

【讨论】:

    猜你喜欢
    • 2015-03-18
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多