mod_rewrite httpd.conf 中的规则不起作用



我正在尝试将重写规则添加到httpd.conf并禁用.htaccess以提高服务器性能。由于某种原因,我尝试过的所有方法都不起作用。

在我的主要httpd.conf中,我已经禁用了所有AllowOverride实例,将它们设置为无。

我在其虚拟主机块中的每个站点都有一个包含文件

Include "etc/apache2/conf.d/site.com/rewrite.conf"

那么在rewrite.conf我有这样的东西:

<Directory "/home/shopmobilephones/public_html">
RewriteEngine On
ServerSignature off
# HTTP > HTTPS #########################################################################
RewriteCond %{HTTPS} off
RewriteRule (.*) https://shopmobilephones.co.uk%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^/www.shopmobilephones.co.uk [NC]
RewriteRule (.*) https://shopmobilephones.co.uk%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.* 
RewriteRule .* ? [F,L]
RewriteRule ^/mobiles/deals/cash-back /cashback.php [L]
RewriteRule ^/mobiles/deals/cheap-mobile-phones /cheapmobilephones.php [L]
RewriteRule ^/mobiles/deals/clearance-deals /clearance.php [L]
RewriteRule ^/mobiles/deals/contracts /contractphones.php [L]
RewriteRule ^/mobiles/deals/12-months-free-line-rental /12month.php [L]
RewriteRule ^/mobiles/deals/top-deals /dailydeals.php [L]
RewriteRule ^/mobiles/deals/free-phones /freephones.php [L]
RewriteRule ^/mobiles/deals/new-mobile-phones /ladeals.php [L]
RewriteRule ^/mobiles/deals/sim-free-phones /simfree.php [L]
RewriteRule ^/mobiles/colours/(.*) /colours.php?colour=$1 [L]
RewriteRule ^/mobiles/(.*)/contract-deals /dealsnew.php?slug=$1&deal-type=1&device-type=Phone&network=all [L]
RewriteRule ^/mobiles/(.*)/upgrades /dealsnew.php?slug=$1&deal-type=2&device-type=Phone&network=all [L]
RewriteRule ^/mobiles/(.*)/contract-deals/(.*) /dealsnew.php?slug=$1&deal-type=1&device-type=Phone&NETWORK=$2 [L]
</Directory>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.
<IfModule mime_module>
AddType application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

我尝试添加 RewriteBase/并删除所有前缀/这不起作用我也从目录块中删除了规则,这不起作用,我什至在目录块内外的主 httpd.conf 中添加了规则,但没有任何效果,我哪里出错了?

在 http 服务器主机配置中,您需要在路径表示法中保留前导斜杠,RewriteBase没有任何意义,规则需要放置在某个<Directory><Location>部分之外。您必须删除测试%{HTTP_HOST}RewriteCond中的前导斜杠,但您必须转义其中的点字符。似乎某些当前版本的 apache http 服务器并不总是像文档中声称的那样定义%{HTTPS}变量,您可以改为测试端口:

<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster@example.com
# ...
ServerSignature off
DocumentRoot "/home/shopmobilephones/public_html"
RewriteEngine On
# HTTP > HTTPS & www host
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://shopmobilephones.co.uk%{REQUEST_URI} [R=301]
RewriteCond %{HTTP_HOST} ^www.shopmobilephones.co.uk$
RewriteRule ^ https://shopmobilephones.co.uk%{REQUEST_URI} [R=301]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.* 
RewriteRule .* ? [F,L]
RewriteRule ^/mobiles/deals/cash-back /cashback.php [L]
RewriteRule ^/mobiles/deals/cheap-mobile-phones /cheapmobilephones.php [L]
RewriteRule ^/mobiles/deals/clearance-deals /clearance.php [L]
RewriteRule ^/mobiles/deals/contracts /contractphones.php [L]
RewriteRule ^/mobiles/deals/12-months-free-line-rental /12month.php [L]
RewriteRule ^/mobiles/deals/top-deals /dailydeals.php [L]
RewriteRule ^/mobiles/deals/free-phones /freephones.php [L]
RewriteRule ^/mobiles/deals/new-mobile-phones /ladeals.php [L]
RewriteRule ^/mobiles/deals/sim-free-phones /simfree.php [L]
RewriteRule ^/mobiles/colours/(.*) /colours.php?colour=$1 [L]
RewriteRule ^/mobiles/(.*)/contract-deals /dealsnew.php?slug=$1&deal-type=1&device-type=Phone&network=all [L]
RewriteRule ^/mobiles/(.*)/upgrades /dealsnew.php?slug=$1&deal-type=2&device-type=Phone&network=all [L]
RewriteRule ^/mobiles/(.*)/contract-deals/(.*) /dealsnew.php?slug=$1&deal-type=1&device-type=Phone&NETWORK=$2 [L]
<Directory "/home/shopmobilephones/public_html">
# .....
</Directory>
</VirtualHost>

如果您操作最新版本的 apache http 服务器,您可能还希望将[L]标志替换为[END]标志,这通常会略微提高性能,因为它可以防止在不需要时通过重写引擎的额外循环。

您可以按照问题中的建议将重写规则移动到单独的文件中,这很好,只需将其包含在上面示例中内联放置规则的位置即可。

最新更新