【问题标题】:mod_rewrite rules in httpd.conf not workinghttpd.conf 中的 mod_rewrite 规则不起作用
【发布时间】:2017-12-27 10:03:33
【问题描述】:

我正在尝试将我的重写规则添加到 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 里面添加了规则和超出目录块但没有任何效果,我哪里出错了?

【问题讨论】:

  • 什么是 Apache 版本?
  • “没有任何作用”的说法是一个非常笼统和广泛的说法,我怀疑这是真的。请更具体地说明究竟是什么不起作用。最好是提供特定(匿名)请求 URL、您的期望和实际结果。请在问题中添加此类详细信息,而不是在 cmets 中。

标签: apache mod-rewrite httpd.conf


【解决方案1】:

在 http 服务器主机配置中,您需要在路径符号中保留前导斜杠,RewriteBase 没有任何意义,并且需要将规则放在某些 &lt;Directory&gt; 或 &lt;Location&gt; 部分之外。您必须删除 RewriteCond 测试 %{HTTP_HOST} 中的前导斜杠,但您必须转义其中的点字符。似乎某些当前版本的 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] 标志,这通常会略微提高性能,因为它可以防止在不存在时通过重写引擎的额外循环需要那个。

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

【讨论】:

  • 感谢忘记该站点在 :443 上运行,我愚蠢地没有将自定义包含文件放在 VirtualHost 块中,仅用于端口 80 上的块,这是问题,但您对我的帮助很大,谢谢.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
  • 2013-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多