mod_rewrite for favicon control? - apache

I'm making several subdomains as what will basically be portals to the same site on Namecheap. Redirecting subdomains is actually really easy (especially since the plumbing is hidden from me),but I want the favicons to be different. This is crucial because the site is crawled by robots that probably don't care about Javascript or the like.
How would I get a request for http://newsubdomain.example.com/favicon.ico to go to http://oldsubdomain.example.com/differentfavicon.ico instead?
Since I'm a huge n00b in mod_rewrite and most of .htaccess in general, I don't know if it's significant that I'm ultimately storing the files in a structure similar to
http://example.com/oldsubdomain/differentfavicon.ico ...
I could probably use PHP if worse came to worst, but I'm trying to avoid adding yet another language to the list of things my little project requires.

How would I get a request for http://newsubdomain.example.com/favicon.ico to go to http://oldsubdomain.example.com/differentfavicon.ico
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} =newsubdomain.example.com
RewriteRule ^(favicon\.ico)$ http://oldsubdomain.example.com/different$1 [L,NC,R=301]

Related

Rewrite dynamic url htaccess apache

I would need to perform a redirect by extrapolating a part of the url and then creating the new one.
Specifically, I have to redirect:
https://(part to be extracted).montecoasp.it
up:
https://(extracted part).montecosrl.it
PLEASE NOTE: The part to be extracted may not even be there.
Can anyone tell me what to write in the htaccess file? Should you use RewriteUrl, RedirectMatch or what? Thank you.
I assume this is what you are looking for:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(\w+\.)?montecoasp\.it$
RewriteRule ^ https://%1montecoasp.it%{REQUEST_URI} [R=301,END]
You can implement such rule in a distributed configuration file, but you should prefer to use the static http server's host configuration.
Obviously the rewriting module needs to be loaded into the http server for this. And if you want to use a distributed configuration file (".htaccess"), then you need to enable those too...
In general it is a good idea to start out with a R=302 temporary redirection and only to change that to a R=301 permanent redirection once everything is sorted out. That prevents nasty caching issues...
You definitely should start reading the documentation of the tools you are using. You want to learn how things work, you do not just want to blindly copy things. As typical for OpenSource software the apache documentation is of excellent quality and comes with great examples:
https://httpd.apache.org/docs/current/howto/htaccess.html
https://httpd.apache.org/docs/current/mod/mod_rewrite.html

Htaccess rewrite rules subfolder

i'm quite new in the world of Url Rewriting. I have to do so into our own CMS to have a good looking url and to get a better structure that gives results with search engine (SEO).
This is the background file that do all the job that we now hide with nice URL's:
http://something.com/fr/index.php
This is an example of my structure pattern :
http://something.com/fr/accueil or ...
http://something.com/fr/produits/solives-de-rive-rimboard
The CMS simply create an HTACCESS file into the FR directory for quite basic rewriterule like the following :
Options +FollowSymlinks
RewriteEngine On
RewriteRule produits/solives-de-rive-rimboard index.php?p=144
RewriteRule produits/decoupe-sur-mesure index.php?p=145
RewriteRule produits/panneaux-osb index.php?p=146
RewriteRule produits/boites-de-bois-crates index.php?p=147
RewriteRule produits/palettes-de-bois index.php?p=148
RewriteRule accueil index.php?p=4
RewriteRule a-propos-de-cimdat index.php?p=139
RewriteRule produits index.php?p=140
RewriteRule videos index.php?p=141
RewriteRule nous-joindre index.php?p=142
All works fine for first level page like "accueil", "nous-joindre"...
but I haven't found the work around for page of second and third level like "produits/solives-de-rive-rimboard". The index.php just load fine except that all relative link to this page are a subfolder away (the css, the image, jquery...).
Is there any tag that I can use into the htaccess file to specify that no matter in which level "accueil", "produits/something", or a third level one like "produits/group/something" for the index.php have a basic path ?
I'll prefer looking a work around within the htaccess instead of giving index.php a "<base href="something">" that might give us other problems in our structure.
Thanks
This is an HTML issue, rather than a server or .htaccess problem. If your browser sees something like /category/product it is going to act as if it is in the /category/ folder regardless of where the internal server side redirects are sending the request.
The fix to this is simple, change all of the linking in your html to be relative to the site root. So if you have an image tag like
<img src="img/button.gif" />
change it to
<img src="/img/button.gif" />
This tells the browser the exact path from the root to request files from regardless of your rewrite rules. This needs to be done for all of your relative links, including css and javascripts. It may be a bit of work to do, but once it is done it should make future maintenance much easier since you won't have to worry about the apparent path to the page.
Yes, this could be done via .htaccess fairly simply, but there are side effects, including the fact that search engines could see it as duplicate content and it could increase your server bandwidth use as users can't cache static content efficiently. Your best bet is to follow the best practice now while the site is new and growing than try to fix it later after a workaround has caused problems.

Sub-domain tangle (knotsmith required)

I seem to have painted myself into a corner with my plans for a series of website sub-domains. I wonder if my plans can be rescued or have I truly shot myself in the foot and would need to totally re-write a ton of stuff? Here's the problem:
I planned a series of sub-domain sites all dealing with variations on a theme. For illustrative purposes let's pretend I have the site www.colour.com (this is the 'hub' whose files reside in public_html) and then I add subdomains red.colour.com (in public_html/red), green.colour.com (in public_html/green) and blue.colour.com (in public_html/blue). Ok all good up to this point.
The thing is that these sites all share a lot of resources in common - style sheets, Javascripts, images etc. These are resources I don't want to replicate because it's a waste of space, but more importantly I don't want to risk developing different versions of files and them not all keeping in step with each other. So I did what I thought was the sane thing and I store all these at the 'hub' (in public_html/css, public_html/js etc).
What I discovered when my site went nearly live was that as soon as I defined e.g. public_html/red as red.colour.com, it could no longer 'see' any of it's supporting files that were located one level higher (in ../) and so the appearence and functionality broke down into a complete screen mess.
Short of a major re-write, is there any way out of this mess that anyone can think of?
Thanks in advance!
Frank.
I cracked it!
It required 1 pint of blood, 2 gallons of sweat and 3 magnums of tears, but I finally cracked it.
The secret is to use mod.rewrite as I was starting to suspect. You can indeed not address areas above the root, but there are other ways of refering to such locations, specifically you can refer to files or directories that 'don't exist' using the ReWrite conditions here:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
So for example, when you want to refer to the style sheet directory that was at a higher level than the currect context, you would refer to ../css, but after you've made the current context into it's own sub-domain, ../css no longer exists. That means you can make a match with one or both of the above rewrite conditions. Now it's just a case of pointng the client to the (e.g.) css directory via the original domain, www.colour.com/css, in my example. Here, for completeness, and to help any other poor soul from killing themselves over this (I had to learn regular expressions, mod.rewrite and Lord knows what else in 6 hours to solve this one! so I hope someone else benefits as well as me!)
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ http://www.colours.com/$1 [R=301,L]
There may be some other directives worth adding such as to make it not rewrite hidden files, to prevent it looping etc but I haven't learn enough about mod.rewrite yet to make this squeeky clean. However it does work as it is and that is 99.99999% of my main goal.
Edit one week on.
Having lived with this solution a while I have to admit that there is a price to pay for this little bit of magic. I'm no expert as you will have gleaned, but it seems to me that this solution leads the browser to request each of the resources involved twice. This might not be a problem if your site is small and the efficiency hit unnoticeable, but if you want to be a stickler for optimum load times, then you might have to weigh up the pro's and con's of using this solution versus recoding your links into something more direct. Just so you know!
Frank, one slight tease: give us XYZ "then I can post a solution" isn't quite consistent with "every source including my web hosting company said it wasn't possible to do this".
Your approach is only sort doing what you want because a 301 redirect like this just passes the problem back to the client's browser telling it to refetch the resource from your other subdomain so it does two request for each resource -- which slows down the load time at the client.
Your first solution is that you could have done by just directly coding http://common.colour.com/... in your src and href attributes in your HTML. But this has the downside that you need to change your scripts. This approach is actually quite common as many high volume sites use a separate domain for such static resource and now often use a CDN for this. It can also have performance advantages as browsers will typically open two streams per domain for loading content but parallel up requests to separate streams.
Your second solution is to do this within your servers filesystem. What you need to do is to create symbolic links for DOCROOT/red/css to DOCROOT/css etc. If you are scripting in PHP you can do this by creating and executing a temporary configuration script in your DOCROOT and invoke it to add these links (which appear in your FTP browser as separate directory copies but in fact all point to shared resources. The key function that you need use is symlink() like this:
<?php
$root=dirname(__FILE__);
foreach( array( 'red', 'green', 'blue') as $c ){
foreach( array( 'css', 'js' ) as $d ) {
symlink( "$root/$c", "$root/$d/$c);
}
}
You'd need to tweak the colours and resource directories. Note that I've just typed this in and not debugged it -- costs extra ;-)
The third solution is to use an .htaccess file, but to do internal redirects rather than external ones. Here the Apache rewrite module maps the request directly to a different path/filename. I assume that your cpanel allows you to map XXX.colour.com to DOCROOT/xxx etc. so that a "GET /css/sheet.css" to a HTTP_HOST red.colour.com gets processed as DOCROOT/red/css/sheet.css. How you approach this depends on whether you use a single .htaccess at DOCROOT or separate red/.htaccess etc. If the former you can use a rule like:
RewriteEngine On
RewriteBase /
RewriteRule (red|green|blue)/(css|js|images)/(.*) /$2/$3 [L]
You do want the leading / on the rule target string here.
Try these out, and hope this helps :-)

We've created a new website, but the old URLs should continue to work because people have bookmarked them

The only problem is
The old urls are something like this www.example.com/?pt#!/2/1270/something-etc-etc/
and we want to redirect them, but we need to pass the something-etc-etc to the new url.
Something like this new.example.com/old/ plus(something-etc-etc)
I've been trying so many ways that I'm already lost
RewriteCond %{QUERY_STRING} ([:alnum:]-)+?[:alnum:]/$
RedirectMatch www.example.com/ http://new.example.com/old/
I was hoping that this regex will return only the ending part, but instead, it returns ?pt#!/2/1270/something-etc-etc/
Best way is to use the Apache mod_rewrite module as described in the URL Rewriting Guide. A bit of a heavy read if you haven't used mod_rewrite before, but well well worth learning. Lots of examples to make things concrete, too.

Creating rewrite rules for multiple urls in the same folder

I have been asked by our client to convert a site we created into SEO friendly url format. I've managed to crack a small way into this, but have hit a problem with having the same urls in the same folder.
I am trying to rewrite the following urls,
/review/index.php?cid=intercasino
/review/submit.php?cid=intercasino
/review/index.php?cid=intercasino&page=2#reviews
I would like to get them to,
/review/intercasino
/submit-review/intercasino
/review/intercasino/2#reviews
I've almost got it working using the following rule,
RewriteRule (submit-review)/(.*)$ review/submit.php?cid=$2 [L]
RewriteRule (^review)/(.*) review/index.php?cid=$2
The problem, you may already see, is that /submit-review rewrites to /review, which in turn gets rewritten to index.php, thus my review submission page is lost in place of my index page. I figured that putting [L] would prevent the second rule being called, but it seems that it rewrites both urls in two seperate passes. I've also tried [QSE], and [S=1]
I would rather not have to move my files into different folders to get the rewriting to work, as that just seems too much like bad practise. If anyone could give me some pointers on how to differentiate between these similar urls that would be great!
Thanks
(Ref: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html)
What I would do, is make /submit-review/ post directly to itself (or a php file) then once submitted redirect from within the PHP file.
It can be hard to force htaccess to maintain post values whilst redirecting etc
My friend found a solution to this one.
RewriteRule review/submit.php - [L]
Will catch the first rewrite and then prevent the next one, worked a treat!