I have a small php script which generates images (GD) for forum signatures. To call them there is an url like : http://www.example.net/img/imgtype_NN.png
Where :
- imgtype is the type of the signature I want
- NN is the ID of the image
When I call this url I want to test if the image already exists in the directory (i.e. if it already has been generated) and in this case I want to show it.
Otherwise I want my script to generate it and then show it.
Here is my actual .htaccess :
RewriteCond %{REQUEST_URI} img/(.*)_(.*).png$
RewriteCond %{DOCUMENT_ROOT}/signatures/img/%1_%2.png -f
RewriteRule img/(.*)_(.*).png$ /signatures/img/$1_$2.png [L]
RewriteRule img/(.*)_(.*).png /signatures/signatures.php?signature=$1&id_pop=$2 [L]
Images are stored in example.com/signatures/img/ and the php script is located in example.com/signatures/signatures.php
After hours of tries I don't have exactly what I need and I give up now, I need some help.
Thanks guys !
Well, your code is not the best so maybe your problem is due to some syntax error.
Anyway, you can try this version (put this code in your root htaccess)
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/signatures/img/$1_$2\.png -f
RewriteRule ^img/(.+)_(.+)\.png$ /signatures/img/$1_$2.png [L]
RewriteRule ^img/(.+)_(.+)\.png$ /signatures/signatures.php?signature=$1&id_pop=$2 [L]
Make sure you've enabled mod_rewrite and allowed htaccess in Apache configuration
Related
i am buildung a very simple PHP API, which answeros on HTTP requests. It should be able to create a user and delete one. So there are two PHP files/classes at the moment. One for deleting, one for creating/inserting a user.
My Question:
How do I need to call them (how does the URL look like) ? As i have seen at some examples, most people only call them via the path:
delete: http://example.org/delete or http://example.org/delete.php
create: http://exmple.org/create or http://example.org/create.php
So, do I have to create these paths ? Do i need to rename all my files to index.php and put them to the right path ?
I need to know, what an estabhlished praxis is (i work with php storm IDE).
Thank you.
Different approach for this are available...
url rewriting
in your .htaccess (if you don't have this file, then create it in your root folder) and copy this in :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L]
Then call http://example.org/create, it will be re-written as http://example.org/index.php?page=create
If you want to keep two different pages (create.php and delete.php) so prefer this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ $1.php [L]
So, with this second variant http://example.org/create, will call http://example.org/create.php instead of index.php?page=create
sub-folders
I like this solution because it makes your API source code structured. So, create some subfolders with index.php in each one like this :
/_include
-- config.php
-- other scripts to include...
/create
-- index.php
/delete
-- index.php
So users will call http://example.org/create and http://example.org/delete ! You can make a _include folder with scripts called by both index.php.
You need to point your URL to the location of your PHP file or the route. For example, if you point it to http://example.com/delete you need to make sure that a route for that exists.
Pointing your URL to http://example.com/delete.php would work fine as long as it exists.
i know this has probably been answered before but i really don't know how to word this.
basically, i'm trying to store images, but when retrieving them i'd like the url to look something like http://www.example.com/xdfg485324
I know mvc .net does this with routes and controls and stuff, but is there a way to do that with php?
i was thinking that maybe since that xdfg485324 directory was not found, i would have a php script run and check to see if that image exists in the database then return the actual image data only (i'm not storing the actual image data in the database though, although i could)
As far as i know there's not really actually a good reason to do this, i just thought it looked more professional.
EDIT:
Thanks to Rasclatt getting me on the right track and providing some htaccess code, i was able to do exactly what i wanted, and the solution was something like this:
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^image/(.*)$ /test/image/?image=$1 [NC,QSA,L]
my actual url looks something like this "http://localhost/test/image/235443"
i have an index.php in the image directory which gets called after this url rewrite.
images/index.php
<?php
if(isset($_GET['image']))
{
$filename = "../uploadedimages/".$_GET['image'].".jpg";
if(file_exists($filename))
{
header('Content-Type: image/jpeg');
$image = imagecreatefromjpeg($filename);
imagejpeg($image);
imagedestroy($image);
}
}
?>
all uploaded files are saved as jpg into the /test/uploadedimages directory, so after this script is ran, it returns the image requested from the uploadedimages directory, but the url still stays the same, even if you get the url to the image.
As stated in my comment, if you are using an apache server, using a .htaccess file is probably the best way with a rewrite rule because if it is not a correct address, I think by default your server would just do an error 404. If you have a rewrite, you can least force every hit to go to one file for routing using php. Here is a basic example of this rewriting. You put this file in any folder you want it to have an affect. It will affect all child folders as well:
.htaccess
RewriteEngine On
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [NC,QSA,L]
Here are a couple of relevant StackOverflow references:
.htaccess rewrite to redirect root URL to subdirectory
How to enable mod_rewrite for Apache 2.2
How to remove index.php from URLs?
I'm looking to handle the URL's except homepage with a common PHP file. This is just like a PHP $_GET request except the difference that there would be no parameter. It'll be just like a file.
Ex- http://localhost/ - This should be managed by index.php file as usual.
http://localhost/ANYTHINGHERE - This should be thrown to a custom PHP file which would then decide what to do.
Actually, I'm working on a project where I need to hide the URL information from the users. So, the file that would manage the ANYTHINGHERE URL would actually access a directory localhost/i/.
Thanks and waiting for best response!
To achieve this you need two parts:
First: .htaccess which redirects all accesses to your domain passed to a php script (index.php here):
RewriteEngine On
RewriteRule (.*) index.php/$1
Second: In index.php you get the user-entered URI as $_SERVER['PATH_INFO'] (starting with /)
This, however, makes all requests to go through the index.php script (depending on the location of index.php you could also get an endless recursion, so read on ;) ). Normally one doesn't want that (e.g., images should be served directly by the web server). Thus, one normally uses (i.e., existing directories, files and links are served by the web server directly):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^(.*)$ - [NC,L]
RewriteRule (.*) index.php/$1
If this should take place in a subdirectory you need to add RewriteBase /subdirectory directly after RewriteEngine On.
If you don't want to use $_SERVER['PATH_INFO']you can also use RewriteRule (.*) index.php?url=$1 [QSA], then you get the user entered URI as $_GET['url'].
This requires mod_rewrite to be loaded on the server.
See http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html.
I know theres a lot of posts about redirects but this is a little different (I think).
Basically I want my outlinks to be example.com/out/1234 and I want them to go to a php that looks up the URL 1234 if referenced to in MySQL and the php header redirect to that URL.
The problem Im having is passing 1234 to a page. I know how if it was out.php?q=1234 but I want it to be /out/1234
Does there need to be an index file within an /out directory that also has a htaccess to rewrite it?
If so, any ideas what the regex need to be to do this? I have seen a few sites doing this and I cant work it out.
htaccess file in your document root, you can try adding:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?out/(.*)$ /out.php?q=$1 [L]
Replace the /out.php with whereever your php script for handling the URL is
I have a site which I have converted to use cms made simple. It works perfectly and I have the friendly urls working fine too. My issue is with a little script I wrote myself and how best to integrate it. The script is a gallery script, it reads a directory and outputs a formatted gallery in html. I was planning on making it a user defined tag in cms made simple but I hit a small snag.
The gallery script needs to be able to read in two values from the url groupId and showpage.
If I am using freindly urls then the cms and use the tag I hit a snag as the cms tries to find an actual page at "www.mysite.com/gallery/mygroup/2" and then throws a 404.
basically I need
http://www.mysite.com/gallery/photogroup/2
rewritten to
http://www.mysite.com/gallery.php?groupId=photogroup&showpage=2
UPDATE
Follwoing Yuri's advice I added his rule to the htaccess. But I have hit another snag.
So for instance if we go to
http://www.mysite.com/gallery/photogroup/2
then Yuri's rule should take effect. But that path is also a correct physical directory on my site coincidentally. Is there a way to have the rewrite rule take effect instead of bringing me to a white screen browsing the files in the directory or to the forbidden screen if I have indexes turned off which I do.
Below is my htaccess
php_flag magic_quotes_gpc Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1
RewriteRule ^gallery/(\w+)/(\d+)$ gallery.php?groupId=$1&showpage=$2 [QSA,L]
So, did you try to write in .htaccess something like this?
RewriteEngine On
RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond ${REQUEST_FILENAME} !-d
RewriteRule ^gallery/(\w+)/(\d+)$ gallery.php?groupId=$1&showpage=$2 [QSA,L]
sounds like a "module" to me. Maybe this Make your module use clean URLs