CyberPanel之伪静态规则略解(附WordPress和Opencart的伪静态规则)

原创 qtwrk  2017-11-24 06:38  阅读 1,285 次 评论 0 条

CyberPanel使用了OpenLiteSpeed(OLS)为web服务器,OLS可以兼容apache的.htaccess文件,但是略有不同

主要差别为  OLS需要添加一个斜杆(/)

比如  WordPress自带的.htaccess内容为

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

然而这份规则并不完全兼容OLS,开启固定链接后会导致404错误。

此时应当修改规则为

RewriteEngine On
RewriteBase /
RewriteRule ^/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

注意第三行的代码
原代码为:RewriteRule ^index\.php$ - [L] ,修改为 RewriteRule ^/index\.php$ - [L]

 

唯一的差别就是那个红色的斜杆(/)

注意修改后需要重启OLS以生效。

 

附Opencart 2.3.0.2的伪静态规则。

RewriteRule ^/.*\.(log|ini|tpl|txt)$ - [F,L,NC]
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/download/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^/([^?]*) index.php?_route_=$1 [L,QSA]

 

对比一下OC2.3.0.2的原始规则,在最后一行你会发现 多了一个斜杆。。。

其中第一行的RewriteRule ^/.*\.(log|ini|tpl|txt)$ - [F,L,NC]规则解释为:直接返回403,禁止访问任何后缀为log,ini,tpl和txt的文件。

当然,OLS还支持另外一种在vhost编辑文件内的伪静态规则。

在cyberpanel中,我们可以在面板中直接编辑vhost配置文件。

在配置文件中找到

rewrite {
enable 1
rules <<<END_rules
rewriteFile /home/你的域名/public_html/.htaccess
END_rules
}

将其中的rewriteFile /home/你的域名/public_html/.htaccess 直接替换为你的伪静态,以WP的伪静态为例,修改后的代码为:

enable 1
rules <<<END_rules
RewriteEngine On
RewriteBase /
RewriteRule ^/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
END_rules
}

这样就可以通过无需.htaccess文件实现伪静态了。

需要注意的是,在vhost配置文件中的伪静态,并不支持 rewrite base ,需要进行修改。
比如你的WP在/wordpress子目录下,那么 .htaccess内的伪静态为:

RewriteEngine On
RewriteBase /wordpress
RewriteRule ^/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

如果需要添加到vhost配置文件中,那么需要修改为:

RewriteEngine On
RewriteBase /
RewriteRule ^/wordpress/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

注意第三行内容, 将 第一行的wordpress,移动到了 index前面,这就是唯一需要修改的地方。

如果有任何疑问,可以留言评论,微信群,QQ群和论坛里留言。

本文地址:https://vps.group/cyberpanel-ols-rewrite-20171124.html
版权声明:本文为原创文章,版权归 qtwrk 所有,欢迎分享本文,转载请保留出处!

发表评论


表情