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

时间:2017-07-21 11:12:11

标签: apache 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里面添加了规则和超出目录块但没有任何作用,我哪里错了?

1 个答案:

答案 0 :(得分:0)

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

%{HTTPS}

如果您运行最新版本的apache http服务器,您可能还希望将<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> 标志替换为[L]标志,这通常会略微提高性能,因为它会阻止通过重写引擎的额外循环当没有必要时。

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