mod_rewrite httpd.conf 中的规则无效

mod_rewrite rules in httpd.conf not working

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

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

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

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= [L]
 RewriteRule ^/mobiles/(.*)/contract-deals /dealsnew.php?slug=&deal-type=1&device-type=Phone&network=all [L]
 RewriteRule ^/mobiles/(.*)/upgrades /dealsnew.php?slug=&deal-type=2&device-type=Phone&network=all [L]
 RewriteRule ^/mobiles/(.*)/contract-deals/(.*) /dealsnew.php?slug=&deal-type=1&device-type=Phone&NETWORK= [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/ 并删除所有前缀/这不起作用我还从目录块中删除了规则这不起作用,我什至在主 [=29] 中添加了规则=] 目录块内外,但没有任何效果,我哪里出错了?

在 http 服务器主机配置中,您需要在路径符号中保留前导斜杠,RewriteBase 没有任何意义,规则需要放在某些 <Directory> 或 [= 之外13=] 部分。您必须删除 RewriteCond 测试 %{HTTP_HOST} 中的前导斜线,但您必须转义其中的点字符。似乎某些当前版本的 apache http 服务器 not 总是定义 %{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= [L]
  RewriteRule ^/mobiles/(.*)/contract-deals /dealsnew.php?slug=&deal-type=1&device-type=Phone&network=all [L]
  RewriteRule ^/mobiles/(.*)/upgrades /dealsnew.php?slug=&deal-type=2&device-type=Phone&network=all [L]
  RewriteRule ^/mobiles/(.*)/contract-deals/(.*) /dealsnew.php?slug=&deal-type=1&device-type=Phone&NETWORK= [L]

  <Directory "/home/shopmobilephones/public_html">
    # .....
  </Directory>
</VirtualHost>

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

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