[英]mod_rewrite in httpd.conf not working (but does in .htaccess)
[英]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=$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中內部和外部添加了規則塊,但無濟於事,我在哪里出錯?
在http服務器主機配置內部,您需要在路徑符號中保持斜杠, RewriteBase
沒有任何意義,並且規則需要放置在<Directory>
或<Location>
部分之外。 您必須在測試%{HTTP_HOST}
的RewriteCond
刪除斜杠,但是您必須在其中轉義點字符。 似乎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]
標志,這通常會稍微提高性能,因為這樣可以避免在不需要重寫引擎時產生額外的循環。
您可以按照問題的建議將重寫規則移到一個單獨的文件中,也可以,只需在上面的示例中將這些規則放在內聯的位置即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.