Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm having trouble redirecting old site URLs to their new places with a .htaccess file. The conditions don't seem to be matching. I'm sure I'm just doing something stupid, but I can't work out what it is. I've included the whole rewrite section here in case some other part of it is causing the problem, but it's the "redirect old site URLs" section that I assume is the problem.

############################################
## enable rewrites

    Options +FollowSymLinks
    RewriteEngine on

############################################
## force www

    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301] 

############################################
## force SSL

    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

############################################
## redirect old site URLs

    RewriteCond %{REQUEST_URI} ^/Products\.aspx\?pid=3 [NC,OR]
    RewriteCond %{REQUEST_URI} ^/contactus\.aspx [NC,OR]
    RewriteCond %{REQUEST_URI} ^/tipsandadviceinstallation\.aspx [NC,OR]
    RewriteCond %{REQUEST_URI} ^/index\.aspx [NC]
    RewriteRule .* https://%{HTTP_HOST}/ [L,R=301]

    RewriteCond %{REQUEST_URI} ^/PdfFiles/Cattle%20Floor%20Fitting%20Guidelines\.pdf [NC,OR]
    RewriteCond %{REQUEST_URI} ^/Products\.aspx\?pid=41 [NC]
    RewriteRule .* https://%{HTTP_HOST}/flooring/engineered-wood\.html\?manufacturer=4 [L,R=301]

    RewriteCond %{REQUEST_URI} ^/Products\.aspx\?pid=812 [NC]
    RewriteRule .* https://%{HTTP_HOST}/doors/internal.html?manufacturer=131 [L,R=301]

############################################
## workaround for HTTP authorization
## in CGI environment

    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

############################################
## always send 404 on missing files in these folders

    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

############################################
## never rewrite for existing files, directories and links

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l

############################################
## rewrite everything else to index.php

    RewriteRule .* index.php [L]

All of the other rewrite rules are working. It's just that section. Plain old boring

Redirect 301 /Products.aspx?pid=812 https://www.flooringanddoors.co.uk/doors/internal.html?manufacturer=131

works as well, but I want case-insensitive matches, and there's no way I'm writing the number of lines needed to match that.

share|improve this question

1 Answer 1

Turns out that %{REQUEST_URI} doesn't contain the query string as well. This meant that my check to match /Products\.aspx\?pid=812 was missing everything after (and including) the question mark. The correct rules are:

############################################
## redirect old site URLs

    RewriteCond %{REQUEST_URI}?%{QUERY_STRING} ^/Products\.aspx\?pid=3 [NC,OR]
    RewriteCond %{REQUEST_URI} ^/contactus\.aspx [NC,OR]
    RewriteCond %{REQUEST_URI} ^/tipsandadviceinstallation\.aspx [NC,OR]
    RewriteCond %{REQUEST_URI} ^/index\.aspx [NC]
    RewriteRule .* https://%{HTTP_HOST}/? [L,R=301]
    ## The ? at the end of the rule here strips the Query_string from the first condition. Otherwise we would end up at the URL https://%{HTTP_HOST}/?pid=3

    RewriteCond %{REQUEST_URI} ^/PdfFiles/Cattle\ Floor\ Fitting\ Guidelines\.pdf [NC,OR]
    RewriteCond %{REQUEST_URI}?%{QUERY_STRING} ^/Products\.aspx\?pid=41 [NC]
    RewriteRule .* https://%{HTTP_HOST}/flooring/engineered-wood.html?manufacturer=4 [L,R=301]
    ## The \<space> are used to match the %20

    RewriteCond %{REQUEST_URI}?%{QUERY_STRING} ^/Products\.aspx\?pid=812 [NC]
    RewriteRule .* https://%{HTTP_HOST}/doors/internal.html?manufacturer=131 [L,R=301]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.