Search code examples
regexapachemod-rewriteurl-rewritingurl-shortener

mod rewrite an URL like "http://miniqr.com/http://www.anyurl.com/" (no typo)


ok, basically

http://miniqr.com/http://www.anyurl.com/

(no typo)

should call

http://miniqr.com/api/create.php?content=http://www.anyurl.com/

to achieve this i have this in the root .htaccess

RewriteRule ^http:\/\/(.*)$ \/api\/create.php\?content=http:\/\/$1 [L]

RewriteRule ^https:\/\/(.*)$ \/api\/create.php\?content=https:\/\/$1 [L]

sad thing is, it once worked, then the server was updated, now it doesn't work

anybody knows why? (or know another way to do it) help would be awesome

my .htaccess pretty much looks like this:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^miniqr\.com$ [NC]
RewriteRule ^(.*)$ http://miniqr.com/$1  [L,R=301]

RewriteRule ^(^$|index\.php|robots\.txt|docs|reader\/) - [L]
RewriteRule ^http:\/\/(.*)$ \/api\/create\.php\?content=http:\/\/$1  [L]
RewriteRule ^https:\/\/(.*)$ \/api\/create\.php\?content=https:\/\/$1  [L]

Solution

  • The URL path is normalized before RewriteRule sees it, in particular, // is replaced by /. You should apply your rule to the original request instead like this:

    RewriteCond %{REQUEST_URI} ^/(https?:.*)
    RewriteRule ^ /api/create.php?content=%1 [L]
    

    %1 refers to the match done by RewriteCond.
    %{REQUEST_URI} is the actual query in the first line of the HTTP request.


    Alternatively, be less strict and accept a single / after the scheme as well as //, since the path might be similarly normalized by the user agent or by proxies.