Issue
I am trying to add my rewrite rules to httpd.conf
and disable .htaccess
in order to improve server performance. For some reason everything I have tried doesn't work.
in my main httpd.conf
I have disabled all instances of AllowOverride
setting them to none.
I have an include file for each site within its virtual host block
Include "etc/apache2/conf.d/site.com/rewrite.conf"
then in rewrite.conf
I have something like this:
<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
I have tried adding RewriteBase / and removing all prefixing / this doesn't work I have also taken the rules out of the directory blocks this doesn't work, I have even added the rules in the main httpd.conf inside and out of directory blocks but nothing works, where am I going wrong?
Solution
Inside the http servers host configuration you need to keep the leading slash in path notations, a RewriteBase
does not make any sense and the rules need to be placed outside some <Directory>
or <Location>
section. You have to remove the leading slash in the RewriteCond
testing the %{HTTP_HOST}
, but you have to escape the dot characters in there. It seems some current versions of the apache http server do not always define the %{HTTPS}
variable as claimed in the documentation, you can test for the port instead:
<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>
If you operate a recent version of the apache http server you probably also want to replace the [L]
flag with the [END]
flag which typically increases performance slightly since it prevents an additional loop through the rewriting engine when there is no need for that.
You can move the rewriting rules into a separate file as you suggest in your question, that is fine, just include it where the rules are placed inline in the example above.
Answered By - arkascha