rewrite for almost everything under / - apache

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.

Related

Modern favicons & htaccess rewrite rule

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!

Mod Rewrite - Changing Base URLS

I got my mod-rewrite done correctly, however (obviously) the linked files on that page are wrong (cause they assume they're one directory inside). Can I rewrite the the base url via mod rewrite for when that rewritecond is met?
Thanks
You probably want to try mod_proxy instead of mod_rewrite, because it has a feature (ProxyPassReverse) which is designed for this particular use-case.

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.

Drupal Clean Urls break randomly for arbitrary paths

I've done everything right. My server has mod_rewrite enabled, my virtualhost path has AllowOverride set to All, and I have the .htaccess file in place with the rewrite rules same as everyone. But I have trouble accessing some pages using their clean url paths. So for 90% of the pages, clean urls work fine. But for that 10%, they don't.
I have checked whether those pages exist -- they do. Checked whether they are accessible using index.php?q=[path] -- and they are. They are only inaccessible through clean url paths.
Can anyone help me with this mystery?
Because you can access your pages through q=path/to/menu/item, then it's clear that it is mod_rewrite that is at fault and not Drupal.
To debug what is going on with your rewrite, either turn on the rewrite log and tail -f it while you request the troubled pages, or alternatively print_r($_GET) at the top of index.php or page.tpl.php to see what is actually being requested.
If you are comfortable posting your potentially sensitive .htaccess here, do so and we can have a look at it for you to see if there are any misconfigurations.
mod_rewrite has a few long-standing bugs that mangle URLs on the way through (do your problem urls have any escape characters?). I don't know if Drupal does this, but in other PHP apps I have had to add code to re-do the rewrite once the correct entrypoint has been reached.
Unfortunately, Drupal can't take its search path in PATH_INFO (as a lot of other apps do), otherwise you could use mod_alias which is much simpler and much more reliable.

Redirecting a Directory to a Script on Apache

So I'm playing with a script that makes it super easy to mirror images off of the web. The script works great (based off of the old imgred.com source, if you've seen that) problem is, it looks a little clunky when using it.
Currently, in order to use the script, you go to a url like:
http://mydomain.com/mirror/imgred.php?Image=http://otherdomain.com/image.jpg
What I'd like to do is to be able to go to:
http://mydomain.com/mirror/http://otherdomain.com/image.jpg
and have it redirect to the former URL, preferably transparent to the user.
I'm reasonably certain that this can be done via .htaccess with a MOD_REWRITE of some kind, but I'm getting frustrated trying to get that to work.
After messing with this myself, I found out that apache collapses any double slash in the URL before the query part into a single slash, and passes the result to mod_rewrite. Maybe that was giving you problems?
This might work for you (.htaccess in the mirror directory):
RewriteEngine On
RewriteBase /mirror
RewriteRule ^http(s?):/(.*) imgred.php?Image=http$1://$2 [L]
Don't know if your script accepts https addresses as well, so I included that just to be sure