2017-07-21 47 views
0

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

在我的主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中添加了内外目录块中的规则,但没有任何作用,我在哪里出错了?

+0

什么是Apache版本? –

+0

“没有任何作用”的说法是一个非常普遍而广泛的说法,我怀疑这是真的。请更具体地说_exactly_不起作用。最好的办法是提供具体的(匿名)请求URL,您的期望和实际结果。请将这些细节添加到问题中,而不是在评论中。 – arkascha

回答

0

在http服务器主机配置中,您需要保留路径符号中的前导斜杠,RewriteBase没有任何意义,规则需要放置在某些<Directory><Location>部分之外。您必须删除RewriteCond测试%{HTTP_HOST}中的前导斜杠,但是您必须避开那里的点字符。看来Apache HTTP服务器的一些当前版本,根据文档中也总是定义%{HTTPS}变量,你可以测试端口,而不是:

<VirtualHost *:443> 
    ServerName example.com 
    ServerAlias www.example.com 
    ServerAdmin [email protected] 
    # ... 
    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版本服务器,您可能还想用[END]标志替换[L]标志,该标志通常会稍微提高性能,因为它可以防止在不需要时通过重写引擎进行额外的循环。

您可以将重写规则移动到您在问题中提出的单独文件中,这很好,只需在上述示例中将规则放在内联的位置即可。

+0

谢谢忘记站点运行:443我愚蠢地没有把自定义包含文件放在VirtualHost模块中,只有在端口80上的阻塞是问题,但是你帮了我很多谢谢。 – futureweb