# 301 redirect using .htaccess

I wonder if the following 301 redirect is the best way for my situation. I used this post on SO to give me the following 301 redirect:

RewriteRule locations\.php$compare-biomass.php [R=301]  What we've done is changed a filename from locations.php to compare-biomass.php. The new URL should be: www.mydomain.com/compare-biomass.php?variable1=1&variable2=2&variable3=3. The above SO post mentioned that the old query string gets appended to the new filename, which is exactly what I want. However, I found that I was being redirected so that SERVER["DOCUMENT_ROOT"] was being appended to the domain, so the URL was: www.mydomain.com/home/mydomain/public_html/compare-biomass.php?variable1=1&variable2=2&variable3=3 I worked out that I had to include the following in my .htaccess file RewriteBase /  However, now that I've had a problem and this is the very first time I'm doing anything related to Apache, can you please tell me if the .htaccess file I'm using is optimal for me: Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} ^mydomain.com [NC] RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L] RewriteBase / RewriteRule locations\.php$ compare-biomass.php [R=301]


Since the RewriteBase directive applies to the entire .htaccess file, it should not be buried between two RewriteRules.

You don't need capturing parentheses for the RewriteCond.

Options +FollowSymLinks

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule .* http://www.example.com/$0 [R=301,L] RewriteRule ^locations\.php$ compare-biomass.php [R=301]

@author Darik

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$http://www.mydomain.com/$1 [R=301,L]
RewriteBase /
RewriteRule ^locations.php$compare-biomass.php?$1 [R=301,L]


If your server supports it, you don't have to put Options +FollowSymLinks in your .htaccess
I think the problem is that you forgot a backslash between locations and .php

• hi unfortunately it doesn't, it simply redirects the page back to www,mydomain.com. – luke_mclachlan Mar 14 '15 at 16:28
• It won't even if there is an error it should redirect you to 404 page. The third and the forth lines are the one which redirects you to the home page. Try with out them and see what happens. Plus the web address should match the name you specify in the RewriteRule eg: RewriteRule ^biomass.php$compare-biomass.php?$1 [R=301,L] – Darik Mar 15 '15 at 17:23