Mod Rewrite and passing a URL as parameter - php

I am having a small trouble with mod rewrite. A friend of mine is writing a script that allows you to upload images.
What we want to do is allow the user to append a domain name to a direct image link, and the script would retrieve the image from the supplied URL.
For example, if the image is at: http://www.test.com/image.jpg, adding domain.com/http://www.test.com/image.jpg would allow a script to retrieve that url (test.com) to get the image we want.
EDIT: HTTP is in front of the URL because I don't want the user to have to remove the HTTP manually. They see an image in their browser, they append "domain.com" before it, http and all and the script retrieves that image and stores it on our server.
The rule I am using is:
RewriteRule ^([\w|.|/]+(jpg|png|gif))$ /upload.php?url=http://$1 [B,L,NC,R=302]
this correctly matches URLs but the colon in http:// causes problems.
If the user inputs: domain.com/www.test.com/image.jpg, it works.
If the user inputs: domain.com/http://www.test.com/image.jpg, it doesn't work and I get a 403 forbidden page (XAMPP on Windows).
If the user inputs: domain.com/http//www.test.com/image.jpg, it works (no colon in http).
EDIT: By working, I mean if I test it locally, I get to see the URL I pass in a $_GET['url'] parameter correctly, instead of seeing an error 403.
Can you please tell me what is wrong with this rule and how to fix it? Or any alternative solutions to achieve the behavior we want?
Thank you.

Well, I think I've found the problem. It wasn't the regex, nor mod_rewrite itself.
So it's a bug in Apache on Windows that has been declared WONTFIX.
For reference, see this StackOverflow thread: and this bug report
I'm posting what I found and will consider this question answered. Thank you all!

You could use urlencode() in php

This approach is cumbersome, error prone and insecure (for example, an image URL isn't required to end with those well known file extensions)
If I understand your use case, it starts when the user is surfing the web and he's viewing an image, and he wants to share it via your service. Then he types by hand http://your.sharing.service in the browser's address bar, just before any text. Then you use mod_rewrite to trigger your script, but I think your regex (and your service too) will fail in a number of unpredictable ways.
I never used a service like this, and I think that the standard approach of using a button to submit the URL to some script (let's say http://my.service.com/upload?url=...) should be preferred.

The problem is in your regex...
Try:
^((http|https)+[\:\/\/\w\.]+(jpg|png|gif))$

Related

Pass text past / in URL into new URL, with the text past / (IIS)

I am trying to make it so if I try to access mysite.com/styles.css, IIS will URL-Rewrite me to example.com/load.php/?url=/styles.css
However, mysite.com/styles.css will almost never be the same. That's purely an example. It could be myblog.biz/somemindblownpost but still needs to redirect in the same way. I have no way to predict what will be thrown at it.
Thanks.
this post will help you
[http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module][1]
[1]: http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module you just need to use the IIS URL rewrite as it mention in the post

How (or what) do I add a folder to the end of a php file

I've been looking for about a week for the answer, but I can not, for the sake of me, even get a straight answer, mainly because I don't know the exact question, so I thought I'd post an example of the question.
How do I add a directory to the end of a PHP file? Or if it's not a directory, what is it called, and how do I do it?
example: example.com/index.php?directory/some/more/things/
Note the 'index.php?directory/' that's the part I refer to
Thanks in advance
- Pat
P.S. Links to documentation/tutorials would be greatly appreciated!
EDIT: This is one of the links i am talking about, it's on a forum that I control, it is using Xenforo:
http://forum.noxcrew.com/index.php?forums/news-and-information.21/
If I understand correctly, what you're looking for is a URL rewriting capabilities in the web server. These allow the user to pass one URL and the server internally process it as another URL. For example, the user may type into the browser:
http://www.yoursite.com/file.php/somedirectory
And internally the server will invoke your program as
http://www.yoursite.com/file.php?param=somedirectory
The configuration would be web server-dependent. For example, in apache you would use mod_rewrite, for example, like so:
RewriteEngine on
RewriteBase /
RewriteRule /file.php\/(.*)$ /file.php?param=$1 [QSA]
This is, of course, a very simple example. You can do a lot more than this. Have a look at Apache mod_rewrite documentation for specifics. Of course, if you're using a different web server (e.g. IIS), then you'll have to look at that server's documentation.
You are probably looking for GET-Parameter that you can handle in PHP.
More information can be found here:
http://php.net/manual/de/reserved.variables.get.php
I'm not 100% sure what your trying to do but if your trying to 'send' a direction to a php script then you can use GET variables.
index.php?path=directory/some/more/things/
Then you can use the follow line to get "directory/some/more/things/" in your script.
$_GET['path'];
This is done because the 'path' part of the url is a variable name and then after the '=' is the value.
Learn more about GET Variables on the PHP Manual http://php.net/manual/en/reserved.variables.get.php

PHP CMS web page short URL?

I am buildiig a simple CMS and would like to know how to create short URLs (not the APACHE bit but the PHP bit).
example.com/?page=100
example.com/home/test
How would I interpret the ?page=100 into /home/test (Through select the database, but i couldn't figure out how) I can see if just one level /home/test because you probably can have a zoneID, but when it comes to /home/test/test. I become lost
And how do I parse back the /home/test to the page id.
Plus is there anyone can show a bit idea for the database design as well?
These resources can be useful to you:
https://stackoverflow.com/a/120411/370290
http://www.symfony-project.org/book/1_0/09-Links-and-the-Routing-System
http://codeigniter.com/user_guide/general/urls.html
http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/
You need some kind of mod_rewrite for your server side.
That will help you to send route data to index.php (or somewhere else) file without filename in adress string. Than some php file will analyze the route and give correct html.
ok i think you need to definitely need to look at the way you are going to do your routing (through mod_rewrite)..for example
1.you can rewrite the page www.example.com/test to ..www.example.com/index.php?page=test and implement a way of getting page by the page name..and returning an id if a page name exists ..if multiple entries exist then maybe the last modified will be given precedence over the otheers ..you can get the following book CMS Design Using PHP and jQuery helped me alot

How to check if there is anything at URL?

Part of my site requires user to input URLs, but in case they type the URL incorrectly or just input a non-existent one on purpose I end up with a bad record on my database.
E.G in Chrome if there isn't anything at a URL you get the error
message "Oops! Google Chrome could not find fdsafadsfadsf.com". (this is the case I'm referring)
This could be solved by checking the URL to see if there is anything, I can only think of one which is loading the external URL in a PHP file and then parsing it's content. But I hope there is a method that doesn't put unneeded strain on my server.
What other ways exist to check if there is anything at a particular URL?
I would just make a HEAD request. This will work with most servers, and avoids downloading the entire page, so it is very efficient.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
All you have to do is parse the status code returned. If it is 200, then you're good.
Example implementation with cURL here: http://icfun.blogspot.com/2008/07/php-get-server-response-header-by.html
You can use php get_headers($url), which will return false in case there isn't an answer
if you're willing to include a tiny Flash embed you can do a crossdomain AJAX call from the client to see if anything useful is at the destination. This would alleviate any Server involvement at all.
http://jimbojw.com/wiki/index.php?title=Introduction_to_Cross-Domain_Ajax
I would use cURL to do this, that way you can specify a timeout on it.
See the comments on: http://php.net/manual/en/function.get-headers.php

How do I get this URL without considering the Apache settings?

HEllo I have this URL I need to get with PHP
http://www.domain.com/forum/#forum/General-discussions-0.htm
The problem is this is not a real URL, but this the mask created by the .htaccess.
I need to get the visible URL and not the real path of the file, because I need to compare it with some PHP variables I have.
In fact the real path will look like this:
http://domain.com/modules/boonex/forum/index.php
And in that way is totally useless for me.
How do I get the first URL as it is?
You can't get that from http://www.domain.com/forum/#forum/General-discussions-0.htm. Everything after the fragment (#) is not even send to the server, there is no way to retrieve it save for a delayed update with javascript. All you'll get it is http://www.domain.com/forum/ send to the server, and on the onload event of your document you can possibly load something in with javascript.
Look into the source code or it may not have real urls at all. The part is for ajax based navigation. It may mean that there are no real urls on that site and if there are then they should be extracted from <a href="someurl"> as they might masked using javascript.
With
file_get_contents();
for example. Neither user nor your server mind about .htaccess
It's server proccessing the request who have to direct you to correct address
however php does ignore everything after #, so in this case you have no chance to get it without real url
As #Wrikken said, there is no way to get url after # fragment

Categories