Please forgive me, but I'm VERY new to PHP, and even worse with url rewrites.
I'd like to write a PHP script that will dynamically output an image that will then be used as a signature on a forum that I belong to.
I have a base PHP file that I'm working with and will be editing that so I'm able to host it for other users of this forum.
The information will be stored in a database, and I'd like to call the PHP script with a PNG URL
Example:
http://somedomain.com/somecode.png
rewriting that to
http://somedomain.com/sig_img.php?img=somecode
Where somecode is the database table primary index.
I don't really need help with the PHP script (yet), but I have no clue where to begin with the .htaccess mod_rewrite code.
Thank you all for any assistance!
Assuming that mod_rewrite is enabled, something like this should work:
<Location />
RewriteEngine On
# ensure requested resource is not a file.
RewriteCond %{SCRIPT_FILENAME} !-f
# ensure requested resource is not a directory.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*\.png)$ sig_img.php?img=$1 [NC,L]
</Location>
This will redirect any request that is a not a file or directory to the script sig_img.php with the requested filename as the parameter. You may want to restrict this to only .PNG file requests, in which case please read about the options.
Make sure you treat the request as untrusted and parameterise the input; don't just concatenate $_GET['img'] into your query string.
Related
I'm cleaning up my URLs and running into an issue with getting the section/sports name (e.g. football) to use to determine which section of the navigation to highlight/display.
Example URL:
http://example.com/index.php?sports=football
.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?sports=$1 [NC,L]
So far, so good. Then I attempt to grab the section name, but it's not working:
$url = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$parsed_url = parse_url($url);
parse_str($parsed_url['query'], $output); // $output['sports'];
Is this because I need to grab the URL that is visible in the address bar?
http://example.com/football/college
If so, can someone explain the order that things are processed (e.g. .htaccess, php, etc.)?
You can just read the query parameters through the $_GET global array.
So in your case you can just access it as follows:
echo $_GET['sports'];
Regarding the order of accessing files. First of all Apache will receive a request to a resource in a public directory, the directory contains .htaccess, this file contains instructions for Apache to handle the request for files in that directory.
After that, Apache will find the file is PHP file so it hand it over to PHP to process, the output of the script is included in the Response served by Apache.
I have paths like follwing
http://locahost.com/wayinfra/site/wayinfracms?view=about_us
http://locahost.com/wayinfra/site/project?view=justa_hotels
I want to use the urls as
http://locahost.com/wayinfra/about_us
http://locahost.com/wayinfra/project/justa_hotels
Added requirements - When i am using in url manager 'project/<view:\w+>'=>'site/project/' the url locahost.com/wayinfra/project/justa display perfect but locahost.com/wayinfra/project/justa_hotels generate error unable to find the request.
how i can do this?
Read the following document:
Yii's URL Management
Source - http://www.yiiframework.com/doc/guide/1.1/en/topics.url
You first need to configure the Web server so that a URL without the entry script can still be handled by the entry script. For Apache HTTP server, this can be done by turning on the URL rewriting engine and specifying some rewriting rules. We can create the file /wwwroot/blog/.htaccess with the following content. Note that the same content can also be put in the Apache configuration file within the Directory element for /wwwroot/blog.
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
Then configure the 'showScriptName'=>false in the urlManager array.
Use code below for alphanumerics and underscore in the parameter as required.
`'project/<view:[a-zA-Z0-9_]+>'=>'site/project/'`
I just started using Amazon S3 to host static files (images, videos, etc.).
For accessing the uploaded files, temporary links are created.
A temporary link looks like this one:
http://zeebit.s3.amazonaws.com/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=1346888760&Signature=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D
What I want is to serve these file through my url, something like this:
http://s3.mydomain.com/zeebit/stackoverflow-logo.png/AKIAIXEHEYSBDWAAXVVA/B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D
I know I can redirect requests to http://s3.mydomain.com to the Amazon url via PHP (for example), but I don't want the address bar to change.
I can create a .htaccess to transform the url to the Amazon url, but as I know .htaccess can't redirect to external resources.
So, how can I solve this?
There are a couple of solutions:
.htaccess Solution #1 - Rewrite Rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^s3\. # Hostname starts with "s3."
RewriteCond %{REQUEST_URI} !-f # Not a file
RewriteCond %{REQUEST_URI} !-d # Not a directory
RewriteRule ^/(.+)/(.+)/(.+)/(.+)/(.+)$ http://$1.s3.amazonaws.com/$2?AWSAccessKeyId=$3&Expires=$5&Signature=$4 [R=302,L]
NOTE: Your initial desired URL was missing the "Expires" value, so the above would work for URLs formed like so:
http://s3.yourdomain.com/[[S3_Bucket_Name]]/[[S3_Filename]]/[[AWSAccessKeyId]]/[[Signature]]/[[Expires]]
So:
http://s3.mydomain.com/zeebit/stackoverflow-logo.png/AKIAIXEHEYSBDWAAXVVA/B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D/1346888760
would redirect to
http://zeebit.s3.amazonaws.com/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D&Signature=1346888760
.htaccess Solution #2 - Redirect
Whilst being a less flexible solution than the above, you could put the following into your .htaccess file
redirect 302 /s3/ http://zeebit.s3.mydomain.com/
Using this rule, requests for
http://yourdomain.com/s3/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D&Signature=1346888760
Would basically retain everything after /s3/ and simply replace everything preceeding it with the Amazon S3 location.
http://zeebit.s3.amazonaws.com/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D&Signature=1346888760
Ok, I am starting to wonder if this is even possible. I have been pouring over htaccess tutorials and examples and I just can't seem to get it working right.
I have a script that creates dynamic images running in a directory on a friends website. I have access to / but the actual directory I am using is in /mydir/. In order to display the images they are output to png via PHP. I have the following RewriteRule setup to take the filename requested and run a PHP script. Each filename represents a separate file with a serialized object used in the display.php script
RewriteRule ^(.*)\.jpg$ display.php?file=$1
That part is working fine, however if someone requests a file that doesn't exist it just throws PHP errors because the script gets called but there is no object for it to use.
What I am trying to do now, and utterly failing at, is to have it check if the file exists in ./Cache/ directory then run the script as usual and if not then the server can just throw a standard 404.
I have tried things like
RewriteCond %{REQUEST_URI} ^(.*)\.jpg$
RewriteCond %{DOCUMENT ROOT}/img/Cache/%1.jpg -f
and other combinations of REQUEST_URI, REQUEST_URL, SCRIPT_FILENAME, SCRIPT_NAME, etc. I can't seem to come up with the proper variable and regex expression to handle this so I am turning to you guys for help. The biggest problem is not knowing what is actually in each of those variables so I can see where I am going wrong. I would kill for some kind of var_dump(),
Use this rule:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/mydir/img/Cache/$1.jpg -f
RewriteRule ^(.*)\.jpg$ display.php?file=$1 [L]
This needs to be placed into .htaccess file in /mydir/img folder
If you move this folder somewhere else or rename parent folder, you will need update it as well in RewriteCond line.
If you wish you can try this approach, which is more flexible (no need to specify parent folder names) but I cannot guarantee that this will work on your setup (but it DOES work on my PC):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} ^(.*)/([^/]+)\.jpg$
RewriteCond %1/Cache/%2.jpg -f
RewriteRule ^(.*)\.jpg display.php?file=$1 [L]
Here the current physical folder is determined automatically from requested URL. But if you have symbolic links or some other settings, it may produce incorrect results. Plus .. it has 1 more Condition .. which makes it a bit slower (1 extra regex to check).
I would use the PHP file to check it:
use file_exists in your PHP and if it does not exist, use header("HTTP/1.0 404 Not Found"); or header('Location to a 404 image
I just inherited a website built in PHP. The main page of www.mysite.com has a href to www.mysite.com/index/35.html somewhere in the page. In the site's root directory and its children there is no document 35.html.
The number 35 is actually an id found in a DB which also holds the html contents of the page.
If I load URL: www.mysite.com/index.php?id=35 the same page loads.
How does PHP know how to automatically convert
/index/35.html
to
/index.php?id=35
EDIT
Based on the answers, I have found a .htaccess file containing rewrite instructions that would explain the functionality.
However, IIS doesn't seem to (or is not configured) know how to use this. (probably because this is an Apache feature?)
So this begs the following question: Is there a way to configure IIS to work with this?
it will be done usign URL Rewriting using .htaccess - should be in the webroot.
It may look something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
May have other bits, but what this basically tells apache is to send anything that DOES NOT physically exist to index.php
It doesn't. There is a mod_rewrite rule that rewrites from /index/foo to /index.php?id=foo, either in a .htaccess file somewhere or in the httpd configuration itself.
RewriteEngine On
RewriteRule ^index/([\d]+)\.html /index.php?id=$1 [NC,L]
This is off the top of my head. Any browsers trying to load an address starting with index/ has any number ending in .html will be internally redirected to index.php?id= whatever the number is.
Edit: Just saw that your working on IIS. This probably won't work for you. Sorry.
I think you will be using .htaccess to redirect all requests to index.php. From there You can pass the query string a routing class, which will parse the url and identify the unique ids.
In this case we can say like, your routing class will parse the request /index/35.html to indexController, indexAction, id=35. now you can pass this id to the model to get corresponding page contents
NB : Here I a am assuming you are using mvc pattern. Anyway it can be treated in your own way, with the concept remaining the same. Hope this make sence.