Problem with yourls.org redirects on windows 2003 with ISAPI rewrite [closed] - iis-6

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I've setup a yourls.org (URL shortening service) on a windows 2003 VPS server I have, using ISAPI rewrite. I already have ISAPI rewrite installed and working with Wordpress, so I know that is working. I have used the rules suggested from the page:
http://code.google.com/p/yourls/wiki/htaccess
In my ISAPI rewrite, but the redirects are not working. The page is looping, trying to redirect to itself.
I'm not familiar with Rewrite rules so any help would be appreciated. The rules I've applied are:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (/|\.php|/[^.]*)$ [NC]
RewriteRule ^(.*)$ /yourls-loader.php [L]
I added the third conditional line based on something I found in the wiki of the application.
If anyone could shed any light on why this isn't working, I'd appreciate it.
T

Ok, this took me all day, but I got it working. For anyone interested, there was absolutely nothing wrong with the ISAPI rewrite. The problem was in the code. In a file called yourls-loader.php, there's a line that checks the url, deconstructs it and reconstructs it. The problem is that it always forces the new url into https. If you've no security cert on your server it won't work!!!
//$scheme = ( isset($_SERVER["HTTPS"]) ? 'https' : 'http' );
//$request = str_replace( YOURLS_SITE.'/', '', $scheme . 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
$request = str_replace( YOURLS_SITE.'/', '', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
The first 2 lines are commented out, this is what was here. As I trust and know how my server is setup (cause I did it myself) I don't feel any need for this check system.
One other thing to be aware of on a Windows system is that you will have to add the suggested Server_URI parse at the start of this file too.
if (isset($_SERVER['HTTP_X_REWRITE_URL'])){
$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];
}
I hope this helps someone... It's taken me all day to resolve with no online support.

Related

Change Link href from .htaccess [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I want to change url used in href from .htaccess. As I am working in a existing project so I don't want to change the href from 46 files. Is there any way? For instance the code below is in the html file.
<link href="http://localhost/example.com/UK/styles/style.css">
I want to change it to http://localhost/example.com/styles/style.css from .htaccess
Here, my absolute domain is http://localhost/example.com
And my .htaccess is in the document root
.htaccess doesn't actually "change url used in href", but it allows you to internally rewrite a request from /example.com/UK/styles/styles.css to /example.com/styles/styles.css. The user still sees /UK in the URL, the URL is unchanged, but the file that receives the request is.
For example, at the top of the .htaccess file in the "document root". (ie. at http://localhost/.htaccess) you can do something like the following:
RewriteEngine On
RewriteRule ^(example\.com)/UK/(styles/style.css)$ $1/$2 [L]
For a more general "any URL" solution:
RewriteRule ^(example\.com)/UK/(.*) $1/$2 [L]
$1 and $2 are backreferences to the captured subpatterns in the preceding RewriteRule pattern.
Reference:
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

The Redirection of Multiple Parked Domains doesn't Work with Filename [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
My problem seems simple to me but I cannot find a simple solution. Here goes: I have one main domain, and multiple domains pointing to that main domain. To avoid duplicate content I'm trying to redirect all "secondary" or "parked" domains to my main domain so that it resolves to this:
www.parkeddomain1.com => www.maindomain.com
www.parkeddomain2.com => www.maindomain.com
www.parkeddomain3.com => www.maindomain.com
And so on...
Now I have found this htaccess code that is sort of a catch-all solution (which I would prefer):
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.maindomain.com$
RewriteRule ^(.*)$ http://www.maindomain.com/$1 [R=301]
So this code works when I'm dealing only with straightforward parked domains, not with parked domains with subfolders or subfiles. So:
www.parkeddomain1.com => www.maindomain.com
redirect works fine here but when I add a subfolder this happens:
www.parkeddomain1.com/subfolder/ => www.parkeddomain1.com/subfolder/
when what I'm looking for is:
www.parkeddomain1.com/subfolder/ => www.maindomain.com/subfolder/
All this in order to avoid the duplicate content problem with search engines.
Thanks to all for any answer that would guide me to a solution.
Cheers!
If the questioner starts rewrite ruling with [R=301] 301 redirect, a.k.a. “Permanently Redirect”, and then he try to view his URL www.parkeddomain1.com/subfolder/ but the result of the rule wasn't what he want, then even he try to change the redirecting rule, his web browser will always redirect that URL into the first URL where it redirecting with [R=301] flag. Because once the browser has been redirected permanently to the wrong address, even how many times you edit the rule, your browser will still be redirected to the old address, that's a browser thing, and you may even go on to fix the rule, and then change the rule all over again without ever knowing it. Changes the 301 redirects in your browser can take a long time to show up.
The solution is to restart the web browser, or use a different one. So, if you're testing, it's better to use a [R] flag instead of [R=301] flag. And when you are 100% sure that the rule does exactly as it's expected to, then switch it to [R=301] flag. Or else, this question belongs to Server Fault.
If your sole aim is to appease search engines, you can alternatively specify a canonical URL which tells search engines the definitive URL for a piece of content. So even if the same content is served by several different query-string variants, or several different domains, the canonical URL will tell the search engine that these are just alternatives to the one true URL.
See the Google Webmaster Central Blog about Canonical URLs for the details.
Try adding the "L" after 301
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.maindomain.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

Apache RewriteMap Used to Prevent Direct Access to Files [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I am trying to use the result of a RewriteMap function to conditionally allow access to a directory.
The intention is to read a timestamp from a cookie (my_cookie) and pass it into the RewriteMap (my_rewrite_map_func) which I have defined in httpd.conf (It's an executable which echos strings "TRUE" or "FALSE" to stdout if the timestamp in the cookie is within a certain range of the current time in Apache).
RewriteMap my_rewrite_map_func prg:/var/www/program
The contents of my .htaccess file are:
RewriteEngine On
RewriteCond %{HTTP_COOKIE} my_cookie=([^;]+) [NC]
RewriteCond ${my_rewrite_map_func:%{TIME}%1|FALSE},FALSE ^([^,]+),\1 [NC]
RewriteRule ^(.*)$ / [NC,L,QSA,F]
I can confirm that the program itself is working, the cookie is being read, and the Apache timestamp and the cookie timestamp are within the allowed range.
The regex on the second RewriteCond checks if the return value of ${my_rewrite_map_func:%{TIME}%1|FALSE} is FALSE, however, regardless of what I set this to, the RewriteRule never occurs.
Essentially, I cannot determine how to evaluate the value of ${my_rewrite_map_func:%{TIME}%1|FALSE}. Is there a way I can better extract or store the value of this?
Any help would be most appreciated.
UPDATE - SOLUTION:
I'm not sure why this question was flagged as off topic, it relates to protecting html files from a php script by means of an Apache .htaccess file using mod_rewrite to call a C++ program.
Regardless, the above code works perfectly when used in conjunction with two additional lines to handle the case of the cookie not existing:
RewriteCond %{HTTP_COOKIE} !^.*my_cookie.*$ [NC]
RewriteRule ^(.*)$ / [NC,L,QSA,F]
Hopefully this will help others who have experienced difficulty with the same issue; it seems there has been little success with this elsewhere on the web.
My specific case deals with preventing direct access to html and other files without modifying them directly, or using a download script. PHP code is used to generate a page with links to these files, from which Javascript performs an ajax call to retrieve the server's timestamp and sets a cookie. The timestamp in the cookie is compared to the time in Apache at page load, if it's within a certain range access is granted.
Here's how I do (you may think it's a lot of instructions, but it's sooo quick compared to PHP handling that you should not worry about 5 (or something) cond instead of 2 (or something)):
RewriteEngine On
RewriteCond %{HTTP_COOKIE} my_cookie=([^;]+) [NC]
# Don't touch anything but create MYCOOKIE environment
# and set it to empty if not found:
RewriteRule . - [E=MYCOOKIE:${my_rewrite_map_func:%{TIME}%1|}]
# If the environment was found = not empty:
RewriteCond %{E:MYCOOKIE} !^$ [NC]
# ... then process a rewrite rule:
RewriteRule [blabla...blabla]
I do some stuff like that 50 times and my web server is still amazingly fast. (the bottleneck is the database)
Hope this helps

Nginx vs Apache or using Apache with nginx [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have been running a website which servers javascript widgets for about 2 years. Now my question is whether I should use nginx purely or I should continue using apache with nginx.
I have about 200 sign ups a day and that means that sometimes the request rate for widget goes up by 2000 a day. So, now the problem is switching to nginx means that i would not be able to use the rewrite rules that i am using in apache.
Now that is one problem I know of, but are there any other issues that I can expect to see in an nginx environment that I dont in Apache?
Would you suggest me to switch purely to nginx or stay with apache and nginx as reverse proxy?
You could still use the rewrite rules from Apache, with slight modifications (I took this from Nginx Primer):
Apache:
RewriteCond %{HTTP_HOST} ^example.org$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Nginx:
if ($host != 'example.org' ) {
rewrite ^/(.*)$ http://www.example.org/$1 permanent;
}
Another issue is .htaccess files, but this would only be an issue if your sharing the server with others.
I would also research any Apache modules that your relying on and ensure that the Nginx equivalents include the same functionality. Definitely test your webapp with Nginx in a test environment first to identify any issues.
In the end, if your goal is better performance then the migration from Apache to Nginx should be worth it.

htaccess rewrite hide .php and also flatten query string on one particular url

I have checked various topics and nothing caught my eyes. This is what am trying to do ..
It's a small site and with only few pages all in my root /mobile folder. So I decided to modify h*p://example.com/mobile/academics.php to h*p://example.com/mobile/academics (without the trailing slash)
RewriteRule ^([^.]+)$ $1.php [NC,L] - Works fine.
But I have http://example.com/mobile/program.php?p=ams which I want to convert as http://example.com/mobile/program/ams . I tried this :
RewriteRule ^/program/([^/]+)$ program.php?p=$1 - Makes no effect. Browser keeps looking for /program/ams.php
How to have both rules coexist? I have query string only on program.php . Any help is appreciated. I am sorry if this has been answered before. I searched for quite sometime and couldn't find any.
Thanks,
Vik
use
RewriteRule ^/?program/([^/]+)$ program.php?p=$1