htaccess condition user-agent - .htaccess

What I'm trying to do is define a rule in my htaccess file that only applies to Internet Explorer:
Header set Connection close
I want this to only apply to Internet Explorer Users (ideally only IE11), I googled this but found no solution and I'm not that experienced when it comes to htaccess. How can I do this? Any hints?

you can just do something like this
RewriteCond %{HTTP_USER_AGENT} "MSIE 11" [NC]
<IfModule mod_headers.c>
Header set Connection "close"
</IfModule>
Make sure mod_headers is enabled though

Related

Can I redirect all old non-query string url's to new url

I want to redirect all old url's which have a format of 'domain.com/property-search/ref-R3265882' to the format of 'domain.com/services/properties/for-sale/property/?ref=R3265882' but can't seem to get it to work
I have tried:
RewriteRule ^property-search/ref-$ /services/properties/for-sale/property/?ref=$1 [R=302,L]
and also
RewriteRule ^property-search/(.*)$ /services/properties/for-sale/property/?ref=$1 [R=301,NC,L]
I am wanting to redirect all reference number with the old format to the new format.
Solution found (adding above in htaccess if using wordpress):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^property-search/ref\-([a-zA-Z0-9-]+)$ /services/properties/for-sale/property/?ref=$1 [R,L]
</IfModule>
This should do what you describe:
RewriteEngine on
RewriteRule ^/?property-search/ref-(R\d+)$ /services/properties/for-sale/property/?ref=$1 [R=301]
It is a good idea to start out with a 302 redirection first and only change that to a 301, once you are happy with the result. That prevents caching issues while you are still playing around.
That rule will work likewise in the http servers host configuration or in a dynamic configuration file (".htaccess") located in the http host's document root, if the interpretation of such files is enabled and the rewriting module is loaded... You should prefer to use the real http server configuration in all cases you can, though, dynamic configuration files have a lot of disadvantages.
I was able to sort it by doing this above wordpress current htaccess entries:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^property-search/ref\-([a-zA-Z0-9-]+)$ /services/properties/for-sale/property/?ref=$1 [R,L]
</IfModule>

Htaccess Font Face and Material Icons Css Not working after URL Rewrite

Hi I've got a wierd issue after a htaccess url rewrite... it works fine for the homepage but if i use the format subdomain.domain.com some fonts don't work, some do. The icons don't work it just shows a placeholder icon. It can't load the woff files etc. I think it may be the htaccess directoryindex disabled but i've put a special rule to allow the homepage to be displayed. How do I do the same for the \img\font\ folder which has the fonts. Also more importantly is disabling directoryindex best practice (i did this to avoid index.html being appended to the url's otherwise the rewrite subdomains doesn't work as it always encounters trailing index.html. Is there a way to set directoryindex to empty "" so that the second query works and I don't have to keep adding rules to allow specific folders?
DirectoryIndex disabled
#rewrite homepage to index.php to allow homepage as directoryindex is disabled
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/?$ index.php [L]
#Rewrite subdomains
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/index.php?sub=%1 [P,NC,QSA,L]
Thanks!
P.S The earth really is flat.
I'm getting CORS errors in the console but it's the files are on the same server and domain.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://example.com/img/icon/fonts/materialdesignicons-webfont.woff2?v=2.0.46. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). (unknown)
downloadable font: download failed (font-family: "Material Design Icons" style:normal weight:normal stretch:normal src index:1): bad URI or cross-site access not allowed source: http://example.com/img/icon/fonts/materialdesignicons-webfont.woff2?v=2.0.46
Adding this to htaccess fixed everything
<IfModule mod_headers.c>
#allow corrs access from subdomains
SetEnvIf Origin ^(https?://(?:.+\.)?example\.com(?::\d{1,5})?)$ CORS_ALLOW_ORIGIN=$1
Header append Access-Control-Allow-Origin %{CORS_ALLOW_ORIGIN}e env=CORS_ALLOW_ORIGIN
Header merge Vary "Origin"
</IfModule>

How to enable mod_rewrite via .htaccess?

I'm going to enable mod_rewrite (modsecurity ) in Cpanel and here is my htaccess, please advice me how to enable it.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|public|assets|uploads|themes|install|updates|asset|mob|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
<FilesMatch "\.(jpg|jpeg|png|gif|swf|css)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
Thanks in advance
As per your provided .HTACCESS code, mod_rewrite is already enabled..
RewriteEngine on
While Mod_security can not be enabled/disabled from cPanel end. If you have access to server OR WHM panel then you can do it and make sure you have compiled APACHE to enabl mod_security, otherwise contact your hosting provider to do it for you.
You can find APACHE compile for mod_security details from here...
mod_rewrite is not the same thing as mod_security. The first allows you to create custom rewrite rules in an .htaccess file like the ones you have mentioned. Mod_security is a security module for apache (protects your website against php sql injection attacks, xss etc).
mod_rewrite is enabled by default on a fresh cPanel/WHM install.
What is exactly not working on your actual config/setup?

Force fresh content, not from the cache

I used the following code in the htaccess to put my site under maintenance and show the visitors maintenance.html.
#Options +FollowSymlinks
#RewriteEngine on
#RewriteCond %{REQUEST_URI} !/maintenance.html$
#RewriteRule $ /maintenance.html [R=302,L]
My site is now back online but now I see that all those browsers that tried to go on my site during the maintenance, they are still getting the maintenance.html.
It works if I clear the browser cache and I sort of understand why it is happening. I think the browser caches the redirect and is still doing it.
How can I clear this issue?
You should be able to use mod_header to tell browsers not to cache your maintenance page.
Header merge Cache-Control no-cache
Header merge Cache-Control no-store

Prevent others from leaching/downloading fonts using htaccess?

Im not sure if this is a duplicate, if it is, please accept my apologies in advance. How can I prevent others from leaching/downloading my #font-face fonts (eot, svg, ttf, woff) using .htaccess and only allowing my domain to use them?
Thanks to the guys over at the #httpd channel on IRC. I've finally found the htaccess block of code to prevent people from hotlinking to my fonts.
SetEnvIfNoCase Referer "^https?://([^/]*)?example\.com/" local_ref=1
SetEnvIf Referer ^$ local_ref=1
<FilesMatch "\.(eot|svg|ttf|woff)$">
Order Allow,Deny
Allow from env=local_ref
</FilesMatch>
Source: Apache Wiki.
For some reason, the code which #joshhendo provided me didn't work for both font file types and images. I'm not sure how .htaccess works, perhaps others with more experience can chime in.
You could prevent hot linking (see http://altlab.com/htaccess_tutorial.html ). This could be changed for font faces), which would allow only pages from your domain to access it. This won't stop people downloading the fonts and uploading them to their own servers, but there's nothing you can do about that.
The following code should work (it's from the URL above, but I've modified it to include the fonts you mentioned. Obviously you will need to change mysite.com to your domain name.
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(eot|svg|ttf|woff)$ - [F]

Resources