IP banning via .htaccess not working - .htaccess

Liteserver on a Shared access host here.
I'm trying to get rid of a lot of bots who waste my resources for nothing.
I can successfully restrict access to some of them using a specific User Agent, but I can't ban their IP addresses, together with those of a lot of chinese ones who are constantly scanning my website. I am still seeing AhrefsBot IP (5.10.83.44) in the access log, even if its IP is banned (see the last line of the htaccess file). The rules should already be inherited by /gallery subfolder.
5.10.83.44 - - [07/Sep/2013:00:56:42 +0200] "GET /gallery/addfav.php?pid=858&referer=displayimage.php%3Fpid%3D858
HTTP/1.1" 302 156 "-" "Mozilla/5.0 (compatible; AhrefsBot/5.0;
+http://ahrefs.com/robot/)"
Root's .htaccess
AddDefaultCharset UTF-8
<IfModule mod_headers.c>
<FilesMatch "\.(js|css)$">
Header append Vary Accept-Encoding
Cache-Control: Private
</FilesMatch>
</IfModule>
RewriteEngine on
#inherit from root htaccess and append at last, necessary in root too
RewriteOptions inherit
#block bad bots
RewriteCond %{HTTP_USER_AGENT} ^$ [OR]
RewriteCond %{HTTP_USER_AGENT} ^spider$ [OR]
RewriteCond %{HTTP_USER_AGENT} ^robot$ [OR]
RewriteCond %{HTTP_USER_AGENT} ^crawl$ [OR]
RewriteCond %{HTTP_USER_AGENT} ^(.*)AhrefsBot(.*) [OR]
RewriteCond %{HTTP_USER_AGENT} ^discovery$ [OR]
RewriteCond %{HTTP_USER_AGENT} ^Wget [OR]
RewriteCond %{HTTP_USER_AGENT} 360Spider [OR]
RewriteCond %{HTTP_USER_AGENT} Ezooms/1.0 [OR]
RewriteCond %{HTTP_USER_AGENT} MJ12bot/v1.4.4
RewriteRule ^(.*)$ http://go.away/
#include caching for images
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/jpg "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType image/x-icon "access plus 360 days"
ExpiresByType text/css "access plus 1 day"
ExpiresByType text/html "access plus 1 week"
ExpiresByType text/javascript "access plus 1 week"
ExpiresByType text/x-javascript "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType application/x-javascript "access plus 1 week"
ExpiresByType application/x-shockwave-flash "access plus 1 week"
ExpiresByType font/truetype "access plus 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType application/x-font-otf "access plus 1 month"
</IfModule>
RewriteCond %{HTTP_HOST} ^id.foo.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.id.foo.com$
RewriteRule ^/?$ "http\:\/\/foo\.myopenid\.com\/" [R=301,L]
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#ban bots and stuff
order allow,deny
deny from 113.212.68.114
deny from 77.232.159.95
deny from 1.12.0.0/14
deny from 1.24.0.0/13
deny from 1.32.0.0/16
[...]
deny from 5.10.83.0/23
allow from all
/gallery subfolder .htaccess
RewriteEngine On
#inherit from root htaccess and append at last
RewriteOptions inherit
##/**************************************************
## Coppermine 1.5.x Plugin - sef_urls
## *************************************************
## Copyright (c) 2003-2007 Coppermine Dev Team
## *************************************************
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
## ********************************************
## $HeadURL$
## $Revision$
## $LastChangedBy$
## $Date$
## **************************************************/
#Options FollowSymLinks SymLinksIfOwnerMatch
# RewriteEngine on
# Uncomment the following line if your webserver's
# URL is not directly related to physical file paths.
# Update "YOUR_COPPERMINE_ROOT" (just / for root)
#RewriteBase /gallery
#
# Language translation
#
RewriteRule cerca.html(.*) search.php$1 [NC]
RewriteRule contatti.html(.*) contact.php$1 [NC]
RewriteRule (.*)migliore(.*) $1toprated$2 [NC]
RewriteRule (.*)popolari(.*) $1topn$2 [NC]
RewriteRule (.*)osservazionedi(.*) $1lastcomby$2 [NC]
RewriteRule (.*)osservazione(.*) $1lastcom$2 [NC]
RewriteRule (.*)pagina(.*) $1page$2 [NC]
RewriteRule (.*)listautenti(.*) $1usermgr$2 [NC]
RewriteRule (.*)profilo(.*) $1profile$2 [NC]
RewriteRule (.*)miniature(.*) $1thumbnails$2 [NC]
RewriteRule (.*)mostra(.*) $1displayimage$2 [NC]
RewriteRule (.*)novitadi(.*) $1lastupby$2 [NC]
RewriteRule (.*)novita(.*) $1lastup$2 [NC]
RewriteRule (.*)cerca(.*) $1search$2 [NC]
RewriteRule (.*)top(.*) $1top_display_media$2 [NC]
#
# Rewrite usrmgr urls
#
RewriteRule usermgr-page-([0-9]*).html(.*) usermgr.php?page=$1$2 [NC]
RewriteRule usermgr.html(.*) usermgr.php$1 [NC]
#
# Rewrite index urls
#
RewriteRule index.html(.*) index.php$1 [NC]
RewriteRule index-([0-9]*)\.html(.*) index.php?cat=$1$2 [NC]
RewriteRule index-([0-9]*)-page-([0-9]*)\.html(.*) index.php?cat=$1&page=$2$3 [NC]
#
# Rewrite thumbnail urls
#
RewriteRule thumbnails-lastupby-([0-9]+)\.html(.*) thumbnails.php?album=lastupby&uid=$1$2 [NC]
RewriteRule thumbnails-lastcomby-([0-9]+)\.html(.*) thumbnails.php?album=lastcomby&uid=$1$2 [NC]
RewriteRule thumbnails-lastupby-([0-9]+)-([0-9]+)-page-([0-9]+)\.html(.*) thumbnails.php?album=lastupby&cat=$1&uid=$2&page=$3$4 [NC]
RewriteRule thumbnails-lastcomby-([0-9]+)-([0-9]+)-page-([0-9]+)\.html(.*) thumbnails.php?album=lastcomby&cat=$1&uid=$2&page=$3$4 [NC]
RewriteRule thumbnails-([a-z0-9]*)-([\-]?[0-9]*)\.html(.*) thumbnails.php?album=$1&cat=$2$3 [NC]
RewriteRule thumbnails-page-([0-9]*)-([a-z0-9]*).*\.html(.*) thumbnails.php?album=$2&page=$1$3 [NC]
RewriteRule thumbnails-([a-z0-9]*)-([\-]?[0-9]*)-page-([0-9]*)\.html(.*) thumbnails.php?album=$1&cat=$2&page=$3$4 [NC]
RewriteRule thumbnails-search-keyword-(.*)\.html(.*) thumbnails.php?album=search&keywords=on&search=$1$2 [NC]
RewriteRule thumbnails-search-(.*)\.html(.*) thumbnails.php?album=search&search=$1$2 [NC]
RewriteRule thumbnails-([0-9a-z]*).*\.html(.*) thumbnails.php?album=$1$2 [NC]
#
# Rewrite displayimage urls
#
RewriteRule displayimage-lastcom-([\-]?[0-9]+)-([\-]?[0-9]+)-([\-]?[0-9]+)-page-([\-]?[0-9]+).html(.*) displayimage.php?album=lastcom&cat=$1&pid=$2&msg_id=$3&page=$4$5 [NC]
RewriteRule displayimage-([a-z0-9]+)-([\-]?[0-9]+)-([\-]?[0-9]+)-([\-]?[0-9]+)-([\-]?[0-9]+)-([\-]?[0-9]+)\.html(.*) displayimage.php?album=$1&cat=$2&pid=$3&uid=$4&msg_id=$5&page=$6$7 [NC]
RewriteRule displayimage-([a-z0-9]+)-([\-]?[0-9]+)-([\-]?[0-9]+)-([\-]?[0-9]+)\.html(.*) displayimage.php?album=$1&cat=$2&pid=$3&uid=$4$5 [NC]
RewriteRule displayimage-([a-z0-9]+)-([\-]?[0-9]+)-([\-]?[0-9]+).*\.html(.*) displayimage.php?album=$1&cat=$2&pid=$3$4 [NC]
RewriteRule displayimage-([a-z0-9]+)-([\-]?[0-9]+).*\.html(.*) displayimage.php?album=$1&pid=$2$3 [NC]
RewriteRule displayimage-([0-9]+).*\.html(.*) displayimage.php?pid=$1$2 [NC]
#
# Rewrite users profiles
#
RewriteRule profile-op-([a-z0-9_]+)\.html(.*) profile.php?op=$1 [NC]
RewriteRule profile-([0-9]+).*\.html(.*) profile.php?uid=$1$2 [NC]
I'm sure I'm missing something, but unfortunately I have no clue.
Any hints?

According to that AhrefBot's link, this is all you need to do to stop that particular bot:
user-agent: AhrefsBot
disallow: /
By adding the above to a robots.txt file in your document root.
The other thing is this:
order allow,deny
Is in the wrong order. According to apache's mod_access documentation:
Allow,Deny
First, all Allow directives are evaluated; at least one must match, or the request is rejected. Next, all Deny directives are evaluated. If any matches, the request is rejected. Last, any requests which do not match an Allow or a Deny directive are denied by default.
Because you have "Allow from all", it gets evaluated first and the different "Deny"'s never get evaluated. So you want:
Order Deny,Allow

Related

SSL certificate HTTPS redirect disable via Htaccess

So it redirects to https but as I don't have SSL certificate anymore it needs to be disabled, tried to remove the Force SSL part didn't do anything same for the force web www didn't do any change either
another thing is when I go to my site it automatically adds www. twice so wwww.www.(domainname)
here's the htaccess
<IfModule mod_expires.c>
ExpiresActive on
# Your document HTML
ExpiresByType text/html "access plus 0 seconds"
# Media: images, video, audio
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
# CSS and JavaScript
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType text/css "access plus 1 year"
</IfModule>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
### Force web (www) on all URLs
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
### Force SSL (https) on all URLs
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
### Remove trailing slash from all URLs
RewriteRule ^(.*)/$ $1 [R=301,L]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
RewriteRule ^buy-([a-z0-9-]+)$ buy.php?game=$1 [NC,L,QSA]
# Redirect proper URL to SEO-friendly for buy.php
RewriteCond %{THE_REQUEST} buy\.php\?game=([a-z0-9-]+)\s [NC]
RewriteRule ^ buy-%1? [R=301,L]
# Internally link SEO-friendly with proper URL for buy.php
RewriteRule ^buy-([a-z0-9-]+)$ buy.php?game=$1 [L]
# Redirect proper URL to SEO-friendly for sell.php
RewriteCond %{THE_REQUEST} sell\.php\?game=([a-z0-9-]+)\s [NC]
RewriteRule ^ sell-%1? [R=301,L]
# Internally link SEO-friendly with proper URL for sell.php
RewriteRule ^sell-([a-z0-9-]+)$ sell.php?game=$1 [L]
# Redirect proper URL to SEO-friendly for testimonials.php (with pages)
RewriteCond %{THE_REQUEST} testimonials\.php\?page=([0-9-]+)\s [NC]
RewriteRule ^ testimonials/%1? [R=301,L]
# Internally link SEO-friendly with proper URL for testimonials.php (with pages)
RewriteRule ^testimonials/([0-9-]+)$ testimonials.php?page=$1 [L]
# Redirect proper URL to SEO-friendly for recover.php (with code)
RewriteCond %{THE_REQUEST} recover\.php\?code=([a-z0-9-]+)\s [NC]
RewriteRule ^ recover/%1? [R=301,L]
# Internally link SEO-friendly with proper URL for recover.php (with pages)
RewriteRule ^recover/([a-z0-9-]+)$ recover.php?code=$1 [L]
# Redirect proper URL to SEO-friendly for index.php (with order ID)
RewriteCond %{THE_REQUEST} index\.php\?order=([a-zA-Z0-9-]+)\s [NC]
RewriteRule ^ o/%1? [R=301,L]
# Internally link SEO-friendly with proper URL for index.php (with order ID)
RewriteRule ^o/([a-zA-Z0-9-]+)$ index.php?order=$1 [L]
# Redirect proper URL to SEO-friendly for any standard page
RewriteCond %{THE_REQUEST} \s/+(account|testimonials|contact|privacy|terms|cart|recover|faq|login|register|banned)\.php [NC]
RewriteRule ^ /%1? [R=301,L]
# Internally link SEO-friendly with proper URL for any standard page
RewriteRule ^(account|testimonials|contact|privacy|terms|cart|recover|faq|login|register|banned)$ $1.php [NC,L]
It is likely that you had a 301 redirect from http to https in your configuration. Simply removing this redirect and retrying will not help since 301 is the code for a permanent redirect which means that the browser will remember this redirect, i.e. it will visit https:// instead of http:// since the cached redirect says so and will not even try to visit http://.
To fix this you need to have both the redirect removed and the browser cache cleared - and then can you retry. And yes, this affects every client which has visited your site. Since you cannot clear the browser cache from every client you better install a new certificate. Once you have one you can redirect all users from https:// to http:// and after a while most browsers should have gotten the message that http:// is the way to go so that you can remove the redirect and the certificate. Or even better, just stay with https:// - there are CA like Let's Encrypt which provide you with free certificates.

CMS Made Simple .htaccess Redirect for non-WWW

I need some help with a CMS Made Simple website that has pretty URLs enabled.
At the moment I have http://www.example.com/ going to http://example.com/
However If I try and access http://www.example.com/pagename it goes to http://example.com/index.php?page=pagename when it should be going to http://example.com/pagename
Additionally I want to redirect http://example.com/index.php to http://example.com/
I've included the contents of my .htaccess file below:
# Attempt to override some php settings, these settings may be helpful on some hosts if your
# default configuration does not meet CMS's minimum requirements, and your host
# has given your account appropriate permissions
#php_value upload_max_filesize "10M"
#php_value session_save_path "tmp/cache"
#php_flag magic_quotes_gpc Off
#php_flag register_globals Off
#php_flag session.use_trans_sid Off
# (this is important, so uncomment if your host permit)
#Options -Indexes
#ServerSignature Off
#Options +FollowSymLinks
# To prevent E_STRICT problems with PHP 5.3+ you can uncomment the following lines
# Note: These settings should only be enabled for production sites!
#php_flag display_startup_errors 0
#php_flag display_errors 0
#php_flag html_errors 0
#php_value docref_root 0
#php_value docref_ext 0
<IfModule mod_rewrite.c>
RewriteEngine on
#
#Sub-dir e.g: /cmsms
RewriteBase /
# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
# but ignore POST requests.
#RewriteCond %{REQUEST_URI} !/$
#RewriteCond %{REQUEST_URI} !\.
#RewriteCond %{REQUEST_METHOD} !POST$
#RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]
# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
#RewriteEngine On
#RewriteCond %{HTTP_HOST} !^www\.
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301]
</IfModule>
<IfModule mod_header.c>
# Disable ETags
Header unset ETag
FileEtag None
</IfModule>
<IfModule mod_deflate.c>
# Compress css, plaintext, xml, gif, and images in transport.
AddOutputFilterByType DEFLATE text/css text/plain text/xml image/gif image/jpeg image/png
</IfModule>
<IfModule mod_expires.c>
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
# Set expires tags on various files... so that the browser wont attempt to reload them.
ExpiresActive On
ExpiresDefault "access plus 1 year"
<IfModule mod_header.c>
# Setting cache control to public allowes proxy servers to cache the items too.
Header set Cache-Control "public"
</IfModule>
</FilesMatch>
</IfModule>
You need to move the www to non-www redirect above the internal index.php rewrite. Additionally, I would recommend adding the L flag to the internal rewrite. This flag is required for the 'www' redirect as well. Lastly, the internal rewrite needs to use (.*) instead of (.+) as the former includes requests to the root, whilst the latter does not.
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
Currently, the request is internally re-written to index.php?page=$1, which becomes the new request URI, which is then appended to the next redirect if 'www' is not included. Swapping those two blocks around will solve the issue.
To redirect /index.php to /, place the following redirect above the 'www' redirect shown above.
RewriteRule ^index.php$ / [R=302,L]
To make that redirect permanent and cached by browsers and search engines, change 302 to 301.

.htaccess 301 redirect if page has a trailing slash

lets say my domain page is
www.website.com/about
and then a user puts
www.website.com/about/ (a trailing slash) at the end
it returns the same page, great. However search engines may see this as duplicate content as it could be seen as 2 pages.
if the user puts a trailing slash at the end of a page i want them to do a 301 redirect to the correct page.
my .htaccess file looks like this
# Begin cache control #
ExpiresActive on
ExpiresDefault "now plus 1440 minutes"
ExpiresByType text/html "now plus 1440 minutes"
<FilesMatch "\.(css|png|bmp|ico|htm|gff|html|js|jpg|jpeg|gif|gcf)$">
FileETag MTime Size
ExpiresDefault "now plus 1440 minutes"
</FilesMatch>
# End cache control #
Options +FollowSymLinks -MultiViews
AddOutputFilterByType DEFLATE text/html image/png image/jpeg image/jpg text/css text/javascript
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L,NC]
Does any one know how I can achieve this as everything im trying will not work.
You can insert this rule just before your last rule to remove trailing slash:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=302,NE,L]

Combine the .htaccess files for silverstripe and squeezr

I am trying to combine two .htaccess files into one:
https://github.com/silverstripe/silverstripe-installer/blob/3.1/.htaccess
AND
https://github.com/jkphl/squeezr/blob/master/.htaccess
I also want to exclude the CMS from any squeezr rules (i.e. links to http://www.mysite.co.nz/admin/.... should not run via squeezr.
Is it sufficient to basically combine the two .htaccess files and write the final redirection rule for silverstripe as follows:
RewriteRule (?!^squeezr|admin)^.*$ framework/main.php?url=$1 [L,QSA,NC]
Please let me know if you need more information in this question and I will add.
You should be able to just combine the 2. There doesn't seem any conflict
https://gist.github.com/24b48c12d82c73e60a37
EDIT:
In your question you also mention that you want to exclude the cms from sqeezer, the idea makes sense, however, the RewriteRule you have posted does not at all, in fact I would be surprised if that did not throw an error.
The only thing the above rule would do (if it would work), would be to exclude squeezr files from being rewritten to framework, but that would never happen anyway, because the RewriteConds before would already deny that (SilverStripe does not rewrite existing files).
so what you need to do is actually add a RewriteCond to the 2 squeezr rules, to tell them not to use cms and framework.
I believe adding the 2 following conditions (right before the squeezr rules) would do that:
RewriteCond %{REQUEST_URI} !cms # exclude the silverstripe cms
RewriteCond %{REQUEST_URI} !framework # exclude the silverstripe framework which is by
(you can also view it as diff for the gist here: https://gist.github.com/Zauberfisch/d681474df67ced83ec1f/revisions)
the full config file:
ErrorDocument 404 /assets/error-404.html
ErrorDocument 500 /assets/error-500.html
<Files *.ss>
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Files>
<Files web.config>
Order deny,allow
Deny from all
</Files>
<Files ~ "\.ya?ml$">
Order allow,deny
Deny from all
</Files>
<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Start the rewrite engine
RewriteEngine On
RewriteBase /
# REDIRECT ANY DIRECT IMAGE REQUEST TO A CACHED VERSION
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{ENV:REDIRECT_BREAKPOINT} !\d+px
RewriteCond %{QUERY_STRING} !^([^&]*&)*squeezr=(0|false|no)
RewriteCond %{HTTP_COOKIE} squeezr.images=(\d+px) [NC]
RewriteCond %{REQUEST_URI} !cms # exclude the silverstripe cms
RewriteCond %{REQUEST_URI} !framework # exclude the silverstripe framework which is by the cms
RewriteRule ^(.+)(\.(?:jpe?g|gif|png))$ squeezr/cache/$1-%1$2 [NC,E=BREAKPOINT:%1,L]
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Please make sure that you set this path ^^^ to the squeezr root directory that is also specified
# for the SQUEEZR_ROOT constant in the common engine configuration (SQUEEZR_ROOT/conf/common.php). If you
# apply the default setup for squeezr (i.e. put everything into a directory named "squeezr" under your
# website's document root), then you shouldn't have to change anything.
#############################################################################################################
# REDIRECT ANY DIRECT CSS REQUEST TO A CACHED VERSION
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{ENV:REDIRECT_BREAKPOINT} !\d+px
RewriteCond %{QUERY_STRING} !^([^&]*&)*squeezr=(0|false|no)
RewriteCond %{HTTP_COOKIE} squeezr.css=(\d+x\d+#\d+(?:\.\d+)?) [NC]
RewriteCond %{REQUEST_URI} !cms # exclude the silverstripe cms
RewriteCond %{REQUEST_URI} !framework # exclude the silverstripe framework which is by the cms
RewriteRule ^(.+)\.css$ squeezr/cache/$1-%1.css [NC,E=BREAKPOINT:%1,L]
# ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# See above for hints on ^^^ this path.
#############################################################################################################
### SILVERSTRIPE START ###
RewriteRule ^vendor(/|$) - [F,L,NC]
RewriteRule silverstripe-cache(/|$) - [F,L,NC]
RewriteRule composer\.(json|lock) - [F,L,NC]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule .* framework/main.php?url=%1 [QSA]
RewriteCond %{REQUEST_URI} ^(.*)/framework/main.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . %1/install.php? [R,L]
### SILVERSTRIPE END ###
</IfModule>
#############################################################################################################
# Additional stuff for improving your website's delivery performance
#############################################################################################################
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain text/html text/xml text/css text/javascript text/json text/x-json text/x-json-stream application/x-javascript application/json application/x-json application/x-json-stream application/.*xml.* multipart/x-json-stream multipart/x-mixed-replace image/svg+xml
</IfModule>
<IfModule mod_expires.c>
ExpiresActive on
# Images
ExpiresByType image/gif "access plus 35 days"
ExpiresByType image/png "access plus 35 days"
ExpiresByType image/jpg "access plus 35 days"
ExpiresByType image/jpeg "access plus 35 days"
# Text based files
ExpiresByType text/css "access plus 35 days"
ExpiresByType text/xml "access plus 35 days"
ExpiresByType text/javascript "access plus 35 days"
ExpiresByType application/x-shockwave-flash "access plus 35 days"
# Default expiration
ExpiresDefault "access plus 1 days"
</IfModule>

htaccess permanent redirect from file that doesn't exists anymore

I got an old Peel shop that I moved on Prestashop. Before switch it live, I would like to keep the Google ranks I got on some products (approximatly 100 products).
So far I make my tests on Prestashop pre-prod server, with a domain name like "mywebsite.com/~subfolder/" (forced by hosting company). All products have been imported from Peel to Prestashop.
I thought that a simple
Options +FollowSymlinks
RewriteEngine on
RedirectPermanent /~subfolder/achat/product_detail.php?id=300 http://www.mywebsite.com/~subfolder/my/new/url-300.html
would worked, but I'm always redirected to the Prestashop not found page. As far as I see, it doesn't accept the fact that "achat/product_detail.php" does not exist.
I also tried
RewriteRule ^/~subfolder/achat/produit_details.php?id=110$ /~subfolder/layette-en-laine-et-soie/110-brassiere-laine-et-soie.html [L,R=301]
which is not working better.
What is the correct way to handle this? If possible in a relative way meaning that I would prefer not to have to modify the file when I will switch Prestashop live (and change http://mywebsite.com/~subfolder to http://www.mywebsite.com).
Any help would be greatly appreciated !
Thanks,
Nicolas
PS : if needed, below my htaccess file generated by prestashop (I removed the tests mentionned above) :
# .htaccess automaticaly generated by PrestaShop e-commerce open-source solution
# WARNING: PLEASE DO NOT MODIFY THIS FILE MANUALLY. IF NECESSARY, ADD YOUR SPECIFIC CONFIGURATION WITH THE HTACCESS GENERATOR IN BACK OFFICE
# http://www.prestashop.com - http://www.prestashop.com/forums
SetEnv PHP_VER 5_TEST
SetEnv REGISTER_GLOBALS 0
AuthUserFile /XXXX
AuthGroupFile /dev/null
AuthName "Veuillez vous identifier"
AuthType Basic
require valid-user
<IfModule mod_rewrite.c>
# URL rewriting module activation
RewriteEngine on
# URL rewriting rules
RewriteRule ^api/?(.*)$ /~subfolder/webservice/dispatcher.php?url=$1 [QSA,L]
RewriteRule ^([a-z0-9]+)\-([a-z0-9]+)(\-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*\.jpg$ /~subfolder/img/p/$1-$2$3.jpg [L]
RewriteRule ^([0-9]+)\-([0-9]+)/[_a-zA-Z0-9-]*\.jpg$ /~subfolder/img/p/$1-$2.jpg [L]
RewriteRule ^([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /~subfolder/img/p/$1/$1$2.jpg [L]
RewriteRule ^([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /~subfolder/img/p/$1/$2/$1$2$3.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /~subfolder/img/p/$1/$2/$3/$1$2$3$4.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /~subfolder/img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /~subfolder/img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /~subfolder/img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /~subfolder/img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /~subfolder/img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg [L]
RewriteRule ^c/([0-9]+)(\-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*\.jpg$ /~subfolder/img/c/$1$2.jpg [L]
RewriteRule ^c/([a-zA-Z-]+)/[a-zA-Z0-9-]+\.jpg$ /~subfolder/img/c/$1.jpg [L]
RewriteRule ^([0-9]+)(\-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*\.jpg$ /~subfolder/img/c/$1$2.jpg [L]
RewriteRule ^([0-9]+)\-[a-zA-Z0-9-]*\.html /~subfolder/product.php?id_product=$1 [QSA,L]
RewriteRule ^[a-zA-Z0-9-]*/([0-9]+)\-[a-zA-Z0-9-]*\.html /~subfolder/product.php?id_product=$1 [QSA,L]
RewriteRule ^([0-9]+)\-[a-zA-Z0-9-]*(/[a-zA-Z0-9-]*)+ /~subfolder/category.php?id_category=$1&noredirect=1 [QSA,L]
RewriteRule ^([0-9]+)\-[a-zA-Z0-9-]* /~subfolder/category.php?id_category=$1 [QSA,L]
RewriteRule ^([0-9]+)__([a-zA-Z0-9-]*) /~subfolder/supplier.php?id_supplier=$1 [QSA,L]
RewriteRule ^([0-9]+)_([a-zA-Z0-9-]*) /~subfolder/manufacturer.php?id_manufacturer=$1 [QSA,L]
RewriteRule ^content/([0-9]+)\-([a-zA-Z0-9-]*) /~subfolder/cms.php?id_cms=$1 [QSA,L]
RewriteRule ^content/category/([0-9]+)\-([a-zA-Z0-9-]*) /~subfolder/cms.php?id_cms_category=$1 [QSA,L]
RewriteRule ^page-non-trouvee$ /~subfolder/404.php [QSA,L]
RewriteRule ^adresse$ /~subfolder/address.php [QSA,L]
RewriteRule ^adresses$ /~subfolder/addresses.php [QSA,L]
RewriteRule ^authentification$ /~subfolder/authentication.php [QSA,L]
RewriteRule ^meilleures-ventes$ /~subfolder/best-sales.php [QSA,L]
RewriteRule ^panier$ /~subfolder/cart.php [QSA,L]
RewriteRule ^contactez-nous$ /~subfolder/contact-form.php [QSA,L]
RewriteRule ^bons-de-reduction$ /~subfolder/discount.php [QSA,L]
RewriteRule ^suivi-commande-invite$ /~subfolder/guest-tracking.php [QSA,L]
RewriteRule ^historique-des-commandes$ /~subfolder/history.php [QSA,L]
RewriteRule ^identite$ /~subfolder/identity.php [QSA,L]
RewriteRule ^fabricants$ /~subfolder/manufacturer.php [QSA,L]
RewriteRule ^mon-compte$ /~subfolder/my-account.php [QSA,L]
RewriteRule ^nouveaux-produits$ /~subfolder/new-products.php [QSA,L]
RewriteRule ^commande$ /~subfolder/order.php [QSA,L]
RewriteRule ^details-de-la-commande$ /~subfolder/order-follow.php [QSA,L]
RewriteRule ^commande-rapide$ /~subfolder/order-opc.php [QSA,L]
RewriteRule ^avoirs$ /~subfolder/order-slip.php [QSA,L]
RewriteRule ^mot-de-passe-oublie$ /~subfolder/password.php [QSA,L]
RewriteRule ^promotions$ /~subfolder/prices-drop.php [QSA,L]
RewriteRule ^recherche$ /~subfolder/search.php [QSA,L]
RewriteRule ^plan-du-site$ /~subfolder/sitemap.php [QSA,L]
RewriteRule ^magasins$ /~subfolder/stores.php [QSA,L]
RewriteRule ^fournisseurs$ /~subfolder/supplier.php [QSA,L]
</IfModule>
# Catch 404 errors
ErrorDocument 404 /~subfolder/404.php
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
ExpiresByType text/javascript "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType application/x-javascript "access plus 1 week"
ExpiresByType image/x-icon "access plus 1 year"
</IfModule>
FileETag INode MTime Size
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>
You cannot match against the query string using a RewriteRule and you cannot include query strings in the Redirect directives. You need to use a RewriteCond for the query string:
RewriteCond %{QUERY_STRING} ^id=110$
RewriteRule ^~subfolder/achat/produit_details.php$ /~subfolder/layette-en-laine-et-soie/110-brassiere-laine-et-soie.html [L,R=301]
Note that you also need to remove the leading / in the match because it gets stripped by the rewrite engine within the .htaccess file.
I finally answer my question : in case detailled above, the solution to have a valid redirection is below :
RewriteCond %{QUERY_STRING} ^id=110$
RewriteRule ^achat/produit_details.php$ /~subfolder/layette-en-laine-et-soie/110-brassiere-laine-et-soie.html [L,R=301]
To be done for each product id that need a redirection
Hope it will help !

Resources