Modern favicons & htaccess rewrite rule - apache

Instead of having all the new favicon formats placed into the root directory of my website, I am placing them inside a subfolder.
To conform to the standards, as some browsers / device versions do not use the path as directed inside the html meta tags, but instead try to get the file from the website root anyway, I am creating a rewrite rule to redirect all these files to the actual location - but ONLY these files.
What I have come up with so far is the following :
RewriteEngine On
RewriteRule ^/((apple\-touch\-icon|android\-chrome|favicon|mstile)-([0-9]+)x([0-9]+).png|manifest\.json|browserconfig\.xml|favicon\.ico|(apple\-touch\-icon\-precomposed|apple\-touch\-icon).png|safari-pinned-tab.svg)$ /favicon/$1 [L]
This should match all of the following files :
When testing the rule at this site, the rule is not matched (see pic):
I would like to keep this rule on the same line, and due to the size standards changing, I wish to keep this dynamic (aka, instead of specifying each individual file, use a mask as I attempted to do). I suspect something with my regex is off.
Please assist or provide a solution with the corrected regex pattern for what I am intending to achieve.

Modern favicons + Rewrite
Following is a fairly robust pattern for mapping the modern favicon's using rewrite.
Regex pattern, for reference
^(browserconfig.xml|manifest.json|safari-pinned-tab.svg|(android-chrome|favicon|mstile)-[0-9]+x[0-9]+.png|apple-touch-icon(-precompressed.png|-[0-9]+x[0-9]+.png|.png)|manifest.json)$
Usage: apply it to a rewrite rule (htaccess)
This example assumes the rewrite destination where the favicon's are placed is a folder named favicon (or whatever folder you wish).
RewriteRule ^(browserconfig.xml|manifest.json|safari-pinned-tab.svg|(android-chrome|favicon|mstile)-[0-9]+x[0-9]+.png|apple-touch-icon(-precompressed.png|-[0-9]+x[0-9]+.png|.png)|manifest.json)$ /favicon/$1 [L]

I don't think this will work.
For one thing, its "precomposed" not "precompressed"
It looks like you're evaluating 'manifest.json' twice
Also, I don't know of any icons that show single-digit sizes; they
would be really tiny, e.g., apple-touch-icon-precomposed-1x1. They
are all 2 or 3 digits, e.g.,
apple-touch-icon-114x114-precomposed.png or favicon-16x16.ico
Cheers!

Related

rewrite rule to forward old search to new search

I've search for a few hours now but I can't figure out a solution.
I just put up a new website that has a different search url than the old site. I'm trying to capture the search queries pointed at the old site and send them to the new sites search.
such as:
advanced_search_result.php?search_in_description=1&keywords=alternator
redirecting to the new sites search like:
index.php?route=product/search&search=alternator
I've tried variations of the following without any luck.
RewriteRule ^advanced_search_result\.php?.*keywords=(.*)$ index.php?route=product/search&search=$1 [R=301,L]
any help would be appreciated.
Your issue is that you are trying to map a pattern including the query string which is not possible with a RewriteRule. That is clearly documented. You need to use a RewriteCond for that:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(?:[^&]*&)*keywords=([^\&]*)
RewriteRule ^/?advanced_search_result\.php$ /index.php?route=product/search&search=%1 [R=301,L]
Reason is that in a RewriteRule the pattern is only matched against the path component of the request URL. The query string is not part of that. Matching against the query string is only possible in a RewriteCond using the %{QUERY_STRING} variable, since such a condition can test an arbitrary string against some pattern, not only the path component of the URL. Tokens captured inside such a condition can then be cited by a %1 in a following RewriteRule, as opposed to the $1 which refers to a capture for that rule itself.
The details are explained in the official documentation of the rewriting module which is something you should always consult when working on rewriting or redirection rules. It is very well written and comes with good examples.
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Using .htaccess to make subdirectory act like subdomain

It's a little difficult for me to ask the question correctly in title, but mainly here's what I'm trying to figure out.
If I use .htaccess on my website and say I'm trying to create a specific path "redirect" so that anything inside that path uses it's non-redirected path for it's relative path. Sorry for the structure of that sentence.
So for example...
I have my website as-
http://www.example.com/
I want to keep things organized so I create a sub-folder/subdirectory to keep all these folders inside of.
http://www.example.com/projects/
So the folder projects will be the place holder for all future projects and I want to create a folder inside projects for each project, so that I can define a URL for each one like such...
http://www.example.com/SuperFish/
http://www.example.com/AquaFear/
Now the folder for SuperFish and AquaFear would be under http://www.example.com/projects/ but also in their own folder say...
http://www.example.com/projects/01/
http://www.example.com/projects/02/
Now it'd be fine and dandy to keep the links like such, but for memory purposes and to share with other people the links to those projects, I'd like to just create a nice simpler URL with a custom "name". like http://www.example.com/SuperFish/ instead of http://www.example.com/projects/01/.
Now I was able to do this partially with my .htaccess file in the root folder with this code...
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^SuperFish/?$ projects/01/ [NC,L]
RewriteRule ^AquaFear/?$ projects/02/ [NC,L]
The issue with this though is that now I can't use relative paths with my SuperFish or AquaFear made up URL links. Anything such as .css, .js, .etc... Isn't within scope because it's trying to look for those files through the made up URL rather than the real physical one.
So the question is, how would I make it so that I can use my made up URL and also have the webpage load things to the real physical path or somehow fix the relative paths by doing this within the .htaccess file?
The reason I titled this the way it is, is due to subdomains being able to find their files with relative paths even though they're a subdirectory just the same.
Using final $ blocks files inside this two folders from being redirect correctly. You could tried instead :
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^SuperFish/?(.*)$ projects/01/$1 [NC,L]
RewriteRule ^AquaFear/?(.*)$ projects/02/$1 [NC,L]

rewrite for almost everything under /

I'd like to add groups to my site, and for that I would like to get rid of a directory, thus the homepage of each group would be like: http://example.org/my-awesome-group instead of http://example.org/group/my-not-so-awesome-group
using the rule RewriteRule ^group/([a-z0-9-]+)/ is quite easy to rewrite each requests accordingly, but without it I'm having some headaches, I'm not sure how to tackle this. do you have any experience about it?
for example there's no group /javascript /css those have to be treated as actual directories on the file system.
thanks!
You can make rewriting rules conditional on whether or not the request refers to a file/directory that actually exists on the filesystem. In addition to the mod_rewrite documentation, take a look at this question here on SO that has an example configuration.

Apache URL Redirect Alternatives

One of my clients (before I came along) decided to use htaccess redirects as their form of URL shortening/search engine friendly URLs. They have literally thousands of them.
The new version of the site now has friendly urls but they aren't equivalent to their redirects so they still need them.
My question to you all is: Is there another way than to populate this file with thousands of lines of "Redirects /folder1 /folder2"?
Thanks
If you cannot make simple rules to catch all of them as in the #chris henry solution you can use the RewriteMap utility of mod_rewrite. You'll be able to write these thousand rules in a text file, then make this text file an hash file, and mode_rewrite will try to match url in this file (if it's an hash file it's quite fast). After that mode_rewwrite can generate a redirect 301 with the [L,R=301] tag.
Yep, look at using the Apache config (httpd.conf or httpd-vhosts.conf) to set up site wide folder aliasing. Eg:
Alias /folder1 c:/www/folder2
Look at http://httpd.apache.org/docs/2.0/mod/core.html#directory for more info.
Depending on how different the URLs being redirected are, one solution might be to come up with an rewrite rule that covers all of them, and maintain the short / long URLs in your application, or even a database.

Case Insensitive URLs with mod_rewrite

I'd like for any url that doesn't hit an existing file, to do a lookup on the other possible cases and see if those files exist, and if so, 302 to them.
If that's not possible, then I'm ok with these compromises:
Only check the lowercase version
Only check the first path portion
For example http://example.com/CoOl/PaTH/CaMELcaSE should redirect to http://example.com/cool/path/camelCase (assuming the latter exists).
but of course a full solution is much more useful to me and others
CheckSpelling on
Matches files and directories. See the documentation for details.
I don't have Apache handy to test, but some combination of these rules should do what you want:
RewriteEngine on
RewriteMap lower int:tolower
RewriteCond ${lower:%{REQUEST_URI}} -U
RewriteRule [A-Z] ${lower:%{REQUEST_URI}} [R=302,L]
A lowercase map to convert /SoMeThinG to /something
A condition to see if the lowercase of the REQUEST_URI exists (-U is internal apache query)
The rule to actually do the rewrite
I don't know if the RewriteMap can be applied in a condition, or if it only applies to a rule. These are based on experts exchange accepted answer and a small orange forum discussion.
Your "ideal" solution is probably not possible unless you can enumerate every valid page on your site. If you only have a few valid pages, a combination of RewriteMap and a text map will do exactly what you need. If there are hundreds / thousands of pages you may need to write a script and use the prg directive.
If you can't identify every valid page, you would need to try every variant in case. Consider your URL as a binary string, with 0 for lowercase letter and 1 for uppercase. Just from your simple example you'd have to test 2^17 variations, 128k pages.
Look up the Apache module mod_negotiation. It does exactly what you want:
http://httpd.apache.org/docs/2.0/mod/mod_negotiation.html#multiviews
You can also pipe all requests to a single PHP file and let the PHP file do the checking for you.