RewriteHeader - Helicon ISAPI Redirect Old URL - isapi-rewrite

There are instructions in the Helicon documentation on using the RewriteHeader http://www.isapirewrite.com/docs/#RewriteHeader and even one example which does exactly what I am looking for:
And this rule will add Old-URL header to the request, providing a Request-URL as a header value:
RewriteCond URL (.*)
RewriteHeader Old-URL: ^$ $1
I've added these two lines as the first two lines of my httpd.ini, stopped and started my website, recycled my application pool (neither of which should be required)... but I can't see any Old-URL header anywhere. I have been going to various URLs, ones with or without redirection rules and haven't been able to find the header. I have been using Chrome's Network tab to review the pages coming back from the server (with status of 301 and 200 respectively)
All redirects in the file are working fine.
Update: There are multiple ways redirects can happen in our current project and I want to add HTTP headers so I can see which method the redirect was fired from.

Try to fix the rule like this:
RewriteCond URL (.*)
RewriteHeader Old-URL: .* $1
Or consider using ISAPI_Rewrite 3 which is more straightforward and has logging to easier debug the problem.

Related

Redirecting from one domain to another with mod_rewrite

I've set up a site for someone that has a URL like:
www.flowers-oakville.com.
But they'd also like www.flowersoakville.com to land on the page.
The question now is, what is the best way to redirect from www.flowesoakville.com to www.flowers-oakville.com. I don't want the user's browser to show the redirection by showing the new URL in the address bar so I think Apache's Redirect command is not the solution. Plus, the Apache Redirect would take an extra trip to the browser and back that I'd like to avoid. So I think the solution needs to be a mod_rewrite, which as I understand it, does everything within the server so the round trip is avoided and the user's address bar is unchanged.
But after reading through a few mod_rewrite tutorials, it seems that mod_rewrite is mainly for redirecting files, not an entire website. Is there a simple way to do this?
Thanks for any help.
I would do this at the domain level and ensure you are using relative links or an environment variable for the hostname in the website.
Add www.flowersoakville.com as a CNAME record pointing to www.flowers-oakville.com.
This will then serve the same website, but retain the domain name as the user entered it, unless you have explicitly linked the actual domain in a link somewhere in the site.
If mode_proxy
is enabled on your server, you can use mod_rewrite (the following code) in your .htaccess file to redirect a domain to another domain without changing url in the address bar.
Try adding this to www.flowersoakville.com/.htaccess file :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?flowersoakville\.com$ [NC]
RewriteRule ^ http://www.flowers-oakville.com%{REQUEST_URI} [L,NE,P]
Note : The example above will not work if proxy module is not enabled on your server.
I wanted to pass on my experience trying to use Dwev's answer. It turns out that the DNS Manager running in my virtual server (I'm using eApps as my hosting provider) does not support "domain based redirection." That is, the left side of a CNAME record has to be in that domain. So when trying to build a CNAME record in the flowers-oakville.com record that points to flowers-oakville.com, the left side has to be xxx.flowers-oakville.com where the xxx might be something like www. I can't get the left side to be flowersoakville.com, even though flowersoakville.com is one of the domains my server is listening for.

Hide real URL with ISAPI Rewrite?

Is it possible to hide real URL with HeliconTech's ISAPI Rewrite module? To be more specific, consider the following scenario:
User requests an URL "example.com/something".
"In the background", server redirects the request to another URL "anotherexample.com".
Contents from "anotherexample.com" are returned to the user (user still sees "example.com/something" in his/her browser address bar after response has been received).
So, the user must not be aware of the fact that contents came from different URL. If this isn't possible with ISAPI Rewrite, do there exist another ways to implement the functionality (in IIS 6 + SharePoint 2007 environment)?
This is achieved using proxy functionality in ISAPI_Rewrite 3. Here are the rules to put in .htaccess in the root of example.com site:
RewriteBase /
RewriteRule ^(.*)$ http://www.anotherexample.com/$1 [P]

Apache Redirect on downstream 404

We are building a mobile site whose url structure matches our current desktop site. We are hosting them on the same domain, and using apache to filter traffic between them using WURFL to help decide which agents go where.
Our filtering rules currently look like this:
RewriteCond %{HTTP_COOKIE} "bucket=mobile"
RewriteRule ^(.*)$ http://internal-mobile-pool.ourdomain.com/$1 [P]
RewriteRule ^(.*)$ http://internal-desktop-pool.ourdomain.com/$1 [P]
Our WURFL based solution sets the 'bucket' cookie for us.
The problem we are facing is that not all the urls on our desktop site have been re-implemented on our mobile site. What I would ideally like, is for the above rule to apply, but if it tries to go to the mobile servers, and gets a 404, it serves up the content from the desktop servers instead.
ie If a mobile requests www.ourdomain.com/some_desktop_only_resource.html, and the internal-mobile-pool.ourdomain.com/some_desktop_only_resource.html returns a 404 - for it to return the content on desktop-pool.ourdomain.com/some_desktop_only_resource.html
To describe this in pseudo-code
if(isMobile)
response = getMobileResponse(url)
if(response.code != 404)
serveResponse(response)
else
serveResponse(getDesktopResponse(url))
I know this is possible if I list all the supported urls in this file - I would like to avoid this, as I would like this Platform Recognition layer to be independent of the application it serves. I also know I could solve this in the mobile application itself by redirecting from there, but if at all possible, I would like this Platform Recognition layer to be self-contained.
Is this possible using mod_rewrite?
Don't think there's a way to do this using mod_rewrite. Because you're using the [P] flag to proxy, the RewriteRule hands the proxy request to mod_proxy and doesn't even know if it went through or not. If everything was in the same document root (or at least subdirectories of the document root), you'd be able to use the -f conditional check to see if a file exists.
Something you could try doing, though I have no idea if this will work, is using mod_proxy's ProxyErrorOverride along with ErrorDocument. If your ProxyErrorOverride is turned on, theoretically if a proxied request returns an error, mod_proxy will use the local error handler instead of the proxied one. You could then use a ErrorDocument 404 to handle the proxied 404 with a php script or something, then do something about re-routing it back through the local rewrite engine (with maybe a redirect with a special query string param?) so that it could be proxied to the desktop domain.

URL Rewrite to remove my controller's name from the url displayed

I have this site
http://cjbuilders.info/welcome/home
and all the links start with
http://cjbuilders.info/welcome
How can I use mod_rewrite to just remove
/welcome/
from the url? This should be an easy one, but i struggle with mod_rewrite.
Do you know about CodeIgniter's URI Routing? Add this into your routes.php config file and it should work just fine:
$route['home'] = 'welcome/home';
This should work, IIRC:
RewriteRule ^/welcome/(.*)$ /$1 [R]
However, guessing a bit about what's going on here, if the reason for this prefix is something like a Java app server deploying an app at a context called "welcome", then the better solution is not to rewrite the URLs but to fix the backend app server to have a null context, i.e. serve at / rather than at /welcome/.
This is because the app server will probably want to generate links to other views of its app, and will reinsert the "welcome": this becomes a pain, and means that all links on your pages will get HTTP redirects when visited (e.g. by search engines). There is no way that the proxying apache server can parse the HTML and tell when that "welcome" should be removed, so best to fix the server that's writing the links in the first place.

More Mod_rewrite problems

Ok, have a client that has existing links coming in from searchmarketing. I am in process of migrating the program from Cold Fusion to PHP.
RewriteEngine on
RewriteRule ^cat_ap~([^~]+)~(.*)\.htm$ /urban/cat_ap.php?$1=$2 [R]
Currently I have a URL structure:
http://www.test.com/urban/cat_ap~nid~5964.htm
which the above rewrite rule changes to
http://www.test.com/urban/cat_ap.php?nid=5964
Now I want to be able to get the variables out of the query string but maintain the url in the browser to the original http://www.test.com/urban/cat_ap~nid~5964.htm but still have it go to the PHP page.
So that when someone goes to http://www.test.com/urban/cat_ap~nid~5964.htm it actually goes to http://www.test.com/urban/cat_ap.php?nid=5964 but still shows http://www.test.com/urban/cat_ap~nid~5964.htm.
Any ideas on how to do this?
Thanks
Mike
You are already doing this on this line (but change the R flag to L):
RewriteRule ^/urban/cat_ap~([^~]+)~(.*).htm$ /urban/cat_ap.php?$1=$2 [L]
The URL that the user hits will still show as the .htm version while the server processes it as the .php
The R flag explicitly induces an external redirect. So just remove the R flag.
I think you'll need to set up reverse proxying to achieve the desired behaviour, and use the P flag with your rewrite rules. I've used a site with this sort of configuration before, so can say that it works, but I'm afraid I've never configured it myself :-(
A good first step at least would be to install mod_proxy and get it loaded and running. The mod_rewrite cookbook page on the P flag has a small amount of detail on proxying RewriteRules, and links through to the ProxyPassReverse directive documentation at apache.org.

Resources