为什么我的.htaccess文件没有正确重定向?

时间:2018-02-01 16:57:11

标签: php .htaccess redirect

这是我的代码:

Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"

RewriteEngine on
RewriteCond %{REQUEST_URI} !(admin)
RewriteRule !maintenance\.php$ /maintenance.php [R=301,L]

我想将网站上的所有流量重定向到maintenance.php,除了/ admin中的文件请求。我尝试了很多从未奏效的组合。无论如何,它总是重定向。我无法弄清楚是什么导致了这一点。

更新: 我想出了什么。如果我在admin文件夹中注释掉.htaccess,Ben的代码就可以了。这是代码:

AuthType Basic
AuthName "Restricted area"
AuthUserFile "/home1/user/.htpasswds/public_html/example/admin/passwd"
require valid-user

我可以对此.htaccess文件做些什么来让它工作?

2 个答案:

答案 0 :(得分:1)

Don't use permanent redirect R=301 in this purpose, you site is not maintenance permanently. Use temporary redirect R=302 only, otherwise your users will always see maintenance page even if you remove this rule, because R=301 is cached by browser.

%{REQUEST_URI} starts with /.

RewriteCond %{REQUEST_URI} !^/admin
RewriteCond %{REQUEST_URI} !^/maintenance.php$
RewriteRule ^ /maintenance.php [R=302,L]

答案 1 :(得分:0)

为什么不在.htaccess中使用此行public function actionTest() { $model = new Campaign(); if ($model->load(Yii::$app->request->post())) { //start transaction block $transaction = Yii::$app->db->beginTransaction(); try { if (!$model->save()) { //throw an exception if any errors throw new \Exception(implode("<br />",\yii\helpers\ArrayHelper::getColumn($model->errors, 0,false))); } //commit the transaction if there arent any erorrs to save the record $transaction->commit(); } catch (\Exception $ex) { //roll back the transaction if any exception $transaction->rollBack(); //catch the error and display with session flash Yii::$app->session->setFlash('danger',$ex->getMessage()); } } $this->render('test'); } 才能重定向到此页面。如果您的网站已准备就绪,您可以通过评论上述行来删除维护模式