2017-07-21 80 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