How is this Isapi Rewrite rule actually working? - url-rewriting

#add in the www
RewriteCond Host: (?:http\://)?drink-superstore\.com
RewriteRule (.+) http\://www.drink-superstore.com$1 [I,RP]
We have it in one of our sites and I need it in another but I did not want to just use it without understanding it.
Help appreciated.

The RewriteCond is a condition, like an IF statement. It tests to see if the HOST is drink-superstore.com, with no leading www. If the condition evaluates to true, then the rule is applied for the request.
The rule says: Redirect to the same url ($1), using a different host, with the www prefix. The redirect comes in with the RP option, which stands for "Redirect Permanently". That is an HTTP 301 response code.

More information on ISAPI rewrite can be found here. I learned how to use URL rewrites from this handy page.Its part of the official Apache documentation.But also here is a tutorial for beginners on URL rewriting. While its geared towards apache, This should at least provide somewhere to start.

Related

MVC3 .net 4.0 extensionless urls and isapi_rewrite

Unfortunatly I am forced to use IIS6 for my MVC website. .net 4.0 adds functionality to mimic the stupid hack for getting extensionless urls to work in IIS6. My website is designed to take advantage url rewriting for SEO purposes. The keyword urls that the client wishes to use dictate an elaborate url rewriting scheme.
The problems with Microsofts impelmentation of the feature really comes down to url rewriting and the attempt to match a pattern. I have seen various rules that attempt to strip the dreaded eurl.axd from the url so that the patterns will match. I attempted to use these rules
RewriteRule ^(.)/eurl.axd/[a-f0-9]{32}(.)$ $1$2
or
RewriteRule (.)eurl.axd/. $1
which does work but it also introduces other problems when there are nested redirects. i.e. handling old urls to new ones, etc
what happens is the eurl.axd gets stripped and on the redirect the isapi_filter doesnt get the request which results in an IIS 404 errror.
Tinkering around with the urls, i came up with this possible solution.
RewriteRule ^generators/generator-parallel-capability/([^/])/([^/])$ /generators/htmlcontent/generator-parallel-capability/$1/$2 [NC,L]
it just grabs the eurl.axd portion and rewrites it to the executing url with it appended.
Is there a better way? I have several hundred urls that meet this pattern and it would be nice to have a single rule handle them all.
We used one generic rule on top of the config to cut the /eurl.axd234234
RewriteRule ^(.)/eurl.axd.$ $1 [NC]
this must work for everything but the root.
Using the rewrite rule RewriteRule ^(.)/eurl.axd.$ $1 [NC] results in some unplesant behavior when the url is rewritten more than one time. i.e. from an old url to the new vanity url then to the actual execution url.
Using maps, I was able to produce a pattern that works quite nicely and keeps the .htaccess file from being cluttered.
RewriteCond ${contentmap:$2} >"" [NC]
RewriteRule ([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /$1/${contentmap:$2}/$2/$3/$4 [NC,L]
RewriteCond ${contentmap:$2} >"" [NC]
RewriteRule ([^/]*)/([^/]*)$ /$1/${contentmap:$2}/$2 [NC,L]
The first detects the pattern /controller/some-content/eurl.axd/1234 and rewrites it to /controller/some-controller-action/eurl.axd/1234
the second does the same thing just without the eurl junk at the end. this is for my dev machine running iis 7
Im sure there are better ways and I am certainly open to better suggestions.

mod_rewrite returns 302 found

I have an apache 2.2 and I am using mod_rewrite. My objective is to forward traffic from
http://localhost:80/AA to http://localhost:8090/BB. So I have a simple rule
RewriteRule http://localhost:80/AA http://localhost:8090/BB
My problem is that the client receives "302 Found". I was hoping that the RewriteRule will
forward the traffic from AA to BB and finally the BB will send the response to my client.
Is there a problem with my configuration?
See the documentation for RewriteRule
What you wanna do is add the R flag to your rule, for redirection.
RewriteRule http://localhost:80/AA http://localhost:8090/BB [R=permanent,L]
L for last rule.
The left side, or pattern, of your RewriteRule should just be the filename you want to match, e.g. ^/AA$. If you also want to make sure the host and port match specific values, you should set up RewriteCond's to match %{HTTP_HOST} and %{SERVER_PORT}. However the latter may be unnecessary unless your web server is configured for multiple domains and ports.
This is untested, but something like this:
RewriteCond %{HTTP_HOST} ^localhost
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^/AA$ http://localhost:8090/BB
I would recommend that you try just this first:
RewriteRule ^/AA$ http://localhost:8090/BB
and add the RewriteCond's if you need them.
Edit based on comments: If you are trying to avoid the "302" response, you cannot do that for the situation you describe. When you use mod_rewrite to redirect from one host to another, you will get two responses sent to your browser. The first is 302, which tells the browser to go to the second URL. The second response should be 200.
With mod_rewrite, you can avoid the redirect in the middle if the rewrite is from one page to another on the same server and port. In that case, the rewrite is internal and the web server can respond with the BB page even though the request is for AA. If you use a different server or port, the web server for AA does not have access to the page BB, so it responds with a redirect to the server that does have access.
If you can reconfigure your site to use the same port for AA and BB, you can make it work. If not, I think you might be able to do what you want with mod_proxy. I have never used mod_proxy so I am not sure what you would need to do.
If you wish to forward traffic, I guess mod_rewrite can't do that. You should preferably use mod_proxy: http://httpd.apache.org/docs/2.0/mod/mod_proxy.html#proxypassreverse
Example from the documentation:
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /foo http://foo.example.com/bar
ProxyPassReverse /foo http://foo.example.com/bar

Rewriting a Subdomain with mod-rewrite

I have a project that uses the moodle library. I had to change the URL from moodle.example.com to learn.example.com, due to a client request.
I thought this would be an easy change, but alas moodle inserts all links and images in with the complete url instead of the relative url.
Is it possible using mod-rewrite to point all requests to moodle.example.com to learn.example.com and maintain the query string?
Example:
I want a request to: http://moodle.example.com/course/view.php?id=2&topic=1 to go to http://learn.example.com/course/view.php?id=2&topic=1.
Is this possible?
Thanks,
Josh
Try this rule:
RewriteCond %{HTTP_HOST} =moodle.example.com [NC]
RewriteRule ^ http://learn.example.com%{REQUEST_URI} [L,R=301]
In addition to the apache rewrite rules, it may also be worth looking at the moodle documentation on migration: http://docs.moodle.org/en/Moodle_migration
In particular look out for admin/replace.php. This tool can help you to rewrite links across all text in the moodle database at the same time.

Mod-rewrite rule for external pages?

Is it possible use mod_rewrite to resolve addresses hosted on another server?
Say I want to setup this URL:
http://www.myserver.com/myfolder/
To actually resolve to:
http://www.anotherserver.com/anotherfolder/
If so, could you provide a RewriteRule example?
You can use the P flag in a mod_rewrite rule to get that substitution URL requested by mod_proxy:
RewriteEngine on
RewriteRule ^myfolder/$ http://other.example.com/anotherfolder/ [P]
Now when a client is requesting /myfolder/ from your server, it will request http://other.example.com/anotherfolder/ and send the response from that server back to the client.
No, tunneling is not possible, you'd have to use a CGI script for this. However, you can redirect:
RewriteRule ^(.*) http://new.example.com/$1
with or without the [R] flag, and it will automatically redirect the user to the new domain.
Edit: Apparently it is possible to tunnel requests with mod_proxy and the [P] flag. See Gumbo’s answer.
Hey I tried rewrite and didn't work (just tested in localhost)
Also I find this easy way
Redirect /myfolder external_url

mod_rewrite rule not working

I have the following rules in my htaccess:
RewriteRule ^([^/.]+)/?$ list.php?categoryShortForm=$1&locationShortForm=world [QSA]
RewriteRule ^([^/.]+)/([^/.]+)/?$ list.php?categoryShortForm=$1&locationShortForm=$2 [QSA]
RewriteRule ^([^/.]+)/([^/.]+)/[^/.]*-p([0-9]+)/?$ view.php?categoryShortForm=$1&locationShortForm=$2&postingId=$3 [QSA]
In my localhost (windows, xampp), it all works fine.
In my real server (linux, apache) the first 2 rules work fine, but not there 3rd one.
For example:
/plastic-surgery/california-usa/ works fine, but
/plastic-surgery/los-angeles-california-usa/test-1-p1 gives me a 404
Any idea??
Check to make sure that you can browse directly to the target URLs. If mod_rewrite is rewriting onto something that doesn't exist, you'll get that 404. It might help to ratchet up mod_rewrite's log level to a high value, so you can see what it's rewriting to.
I am a little unclear on how your working URL is supposed to actually work. All three of your patterns start with "one or more non-slash, non-period characters" ([^/.]+), but URLs going into the pattern matching start with slashes: "/plastic-surgery/california-usa/".
Have you turned on mod_rewrite logging and checked out what mod_rewrite is actually doing?
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9

Resources