Has regex changed? Matching URL problem...

Oh, the strange things mod_rewrite does!

Has regex changed? Matching URL problem...

Postby mescon » Tue Sep 29, 2009 4:47 am

I just recently began having problems with one of my redirect-counters.

I have not changed this, and the conditions have not changed. The problem seemed to begin when I upgraded to Ubuntu 8.04 LTS from 6.10.

The code below has produced the following:

http://services.yi.se/http://www.google.com

Would redirect to http://services.yi.se/redirect/put.pl?u ... google.com

put.pl would just log the redirect and forward.

The below code is what used to work.
I tried setting RewriteOptions Inherit, but that didn't do much.
Does anyone have an idea?

Code: Select all
<Directory /var/www/services.yi.se>
       AllowOverride all
        IndexOptions FancyIndexing FoldersFirst NameWidth=* SuppressHTMLPreamble ScanHTMLTitles DescriptionWidth=120
        Options +ExecCGI +FollowSymlinks
        AddType application/x-httpd-php .php .html
        AddType text/html .php
        AddHandler cgi-script .pl
        AddHandler application/x-httpd-php .php
        DirectoryIndex index.php index.html index.pl

        HeaderName /HEADER.php
        ReadmeName /README.php
        IndexIgnore HEADER.php README.php

        RewriteEngine on
        RewriteBase /

        # http://
        # https:// (with query)
        RewriteCond %{QUERY_STRING} ^$
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(http://.+)$ http://services.yi.se/redirect/put.pl?url=$1 [R,L]

        RewriteRule ^{(.+)};(http://.+)$ http://services.yi.se/redirect/put.pl?tag=$1&url=$2 [R,L]
        RewriteCond %{QUERY_STRING} ^$
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(http://.+)$ http://services.yi.se/redirect/put.pl?url=$1\?%{QUERY_STRING}        [R,L]

        RewriteRule ^{(.+)};(http://.+)$ http://services.yi.se/redirect/put.pl?tag=$1&url=$2\?%{QUERY_STRING} [R,L]

        # https://
        # https:// (with query)
        RewriteCond %{QUERY_STRING} ^$
        RewriteRule ^(https://.+)$ http://services.yi.se/redirect/put.pl?url=$1        [R,L]
        RewriteCond %{QUERY_STRING} ^$
        RewriteRule ^{(.+)};(https://.+)$ http://services.yi.se/redirect/put.pl?tag=$1&url=$2 [R,L]
        RewriteRule  ^(https://.+)$ http://services.yi.se/redirect/put.pl?url=$1\?%{QUERY_STRING}        [R,L]
        RewriteRule ^{(.+)};(https://.+)$ http://services.yi.se/redirect/put.pl?tag=$1&url=$2\?%{QUERY_STRING} [R,L]

        # ftp://
        RewriteRule ^(ftp://.+)$ http://services.yi.se/redirect/put.pl?url=$1        [R,L]
</Directory>
mescon
 
Posts: 2
Joined: Tue Sep 29, 2009 4:39 am

Postby richardk » Tue Sep 29, 2009 3:22 pm

It's probably because of the double slashes (they will probably have been replaced with one slash).

Try
Code: Select all
        # http://
        # https:// (with query)
        RewriteCond %{QUERY_STRING} ^$
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^http:/(.+)$ http://services.yi.se/redirect/put.pl?url=http://$1 [R,L]

        RewriteCond %{QUERY_STRING} ^$
        RewriteRule ^{(.+)};http:/(.+)$ http://services.yi.se/redirect/put.pl?tag=$1&url=http://$2 [R,L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^http:/(.+)$ http://services.yi.se/redirect/put.pl?url=http://$1\?%{QUERY_STRING}        [R,L]

        RewriteRule ^{(.+)};http:/(.+)$ http://services.yi.se/redirect/put.pl?tag=$1&url=http://$2\?%{QUERY_STRING} [R,L]

        # https://
        # https:// (with query)
        RewriteCond %{QUERY_STRING} ^$
        RewriteRule ^https:/(.+)$ http://services.yi.se/redirect/put.pl?url=https://$1        [R,L]
        RewriteCond %{QUERY_STRING} ^$
        RewriteRule ^{(.+)};https:/(.+)$ http://services.yi.se/redirect/put.pl?tag=$1&url=https://$2 [R,L]
        RewriteRule ^https:/(.+)$ http://services.yi.se/redirect/put.pl?url=https://$1\?%{QUERY_STRING}        [R,L]
        RewriteRule ^{(.+)};https:/(.+)$ http://services.yi.se/redirect/put.pl?tag=$1&url=https://$2\?%{QUERY_STRING} [R,L]


Code: Select all
        RewriteCond %{QUERY_STRING} ^$
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(http://.+)$ http://services.yi.se/redirect/put.pl?url=$1\?%{QUERY_STRING}        [R,L]

It only matches if there isn't a query string. It should be for the rule above.

Do you really need to check if it's a request to a file or directory? If you do, it should probably be
Code: Select all
%{DOCUMENT_ROOT}%{REQUEST_FILENAME}

instead of
Code: Select all
%{REQUEST_FILENAME}

(It's usually not the full path in the <VirtualHost> or main server configuration, i think).

If you don't, you could shorten it to
Code: Select all
        # /http:// or /https:// without a query string.
        RewriteCond %{QUERY_STRING} ^$
        RewriteRule ^http(s?):/(.+)$ http://services.yi.se/redirect/put.pl?url=http$1://$2 [R,L]
        # /{tag}/http:// or /{tag}/https:// without a query string.
        RewriteCond %{QUERY_STRING} ^$
        RewriteRule ^{(.+)};http(s?):/(.+)$ http://services.yi.se/redirect/put.pl?tag=$1&url=http$2://$3 [R,L]

        # /http:// or /https:// with a query string.
        RewriteRule ^http(s?):/(.+)$ http://services.yi.se/redirect/put.pl?url=http$1://$2\?%{QUERY_STIRNG} [R,L]
        # /{tag}/http:// or /{tag}/https:// with a query string.
        RewriteRule ^{(.+)};http(s?):/(.+)$ http://services.yi.se/redirect/put.pl?tag=$1&url=http$2://$3\?%{QUERY_STIRNG} [R,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby mescon » Tue Sep 29, 2009 10:34 pm

Hah! Awesome!
Richard, you have no IDEA how awesome you are. No idea!

I checked your replies to other posts and man, you're like the manifestation of good netiquette. Polite, to the point and always helpful.

I dont know if you hear this enough, but thanks alot for your help and your time. Your quick to learn the meaning of a problem and equally quick to come up with a solution. Impressive.

Aaaah.... If only I had the mad leet skillz.

Your solution worked flawlessly.
mescon
 
Posts: 2
Joined: Tue Sep 29, 2009 4:39 am


Return to Idiosyncrasies

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron