Alright, so pretty much I have this on Htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index2.php?url=$1
And I'm trying to execute in a php file the following
mime_content_type('Doggy.png')
and it returns an error saying path of file not found, which I'm pretty sure it does exist,
trying to find what the error could be I ended thinking it's htaccess.
Can anybody help me solve this?
Thank you in advanced.
No, it's not your .htaccess rules. Those only rewrite HTTP requests into file execution rules. Once your PHP executes, they have no influence anymore whatsoever.
The problem is simply that the file Doggy.png does not exist relative to the file where this command is executed. The file must be in the same directory as index2.php, assuming that's the file that contains the line mime_content_type('Doggy.png'). Otherwise you need to use relative paths like mime_content_type('../Doggy.png') or mime_content_type('img/Doggy.png').
As we've established already, the problem is not in rewrite rules.
What actually matters is not he relative location of the files, but rather location of your image file relative to a working directory.
For example, if your directory structure is:
/
/index.php
/inc/imagelib.php
/inc/Doggy.png
and you're doing manipulation on Doggy from the imagelib.php, but it is actually included from index.php and your working directory is /, then, despite being in the same folder, you'll need to address the file as inc/Doggy.png.
The working directory may change, depending on what your entry point in the program will be (in case of a web application - where was the file that originally got the request), or (in case of command line), where were you in shell when executing the command.
To avoid problems with relative paths, i suggest using absolute paths.
So (assuming the same scenario as in the previous example), in imagelib.php you would need to construct the path to image path like this:
`$absolute_path = dirname(__FILE__)."/Doggy.png"`
Related
How do I point a URL to a file so when I go to the URL it points to that file but doesn't change the URL. For example:
mydomain.com/orders/create should point to /myfiles/orders-create.php
then when I go to mydomain.com/orders/create it will display all the contents of orders-create.php.
Any ideas?
If you want to do it on the server-side you could edit the .htaccess file similar to this question's answer.
The main changes you're looking at are:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*)$ $1.php [NC]
This would let you create files without an extension and it would run force run the PHP interpreter.
Here's a good read for pretty URLs to find out more ways you could do this, and it'll explain more about what they actually are.
If you can change create-order.php, then you can make it so that it does not rely on relative paths. One way to do this is to create a bootstrap.php file that has all of the includes and then your "leaf" PHP files only have to find this bootstrap.php file.
Another option is to set the PHP path. If you change the include path to include the root of the directory, then when including files in create-order.php will look in the root and it will find them. You can set the include path in your PHP files or in the PHP configuration.
If you don't want or can't change create-order.php then one way to do this would be via the webserver. For example, in apache, you can use mod_rewrite to do just that, have a public URL actually invoke a specific local file. The rewrite configuration might look like this (example for just this one file):
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^orders/create to create-order.php$ create-order.php [L]
or a catch-all rule with this (untested):
RewriteRule ^orders/(.*)$ $1?%{QUERY_STRING} [L]
It's been awhile since I've used mod_rewrite so I am hoping somebody can shed some light on this quickly.
Basically, I have a flat dynamic PHP file, call it myForm.php. I need to serve this file whenever a request is made on the domain at the path h.t.t.tp://mydomain.com/mylong/uri/contact/path. If a user goes to that location I need my apache server to serve the myForm.php file, and this of course should be oblivious to the user. They would still see /mylong/uri/contact/path as their location address, but the file being served is from the webroot folder or some other location and called myForm.php.
This is easily possible using mod_rewrite correct?
Something like:
RewriteEngine on
RewriteRule ^/mylong/uri/contact/path$ /myForm.php [PT]
Does that work? Taken from:
http://httpd.apache.org/docs/trunk/rewrite/remapping.html
Thanks in advance to anyone who can confirm and provide a great answer!
Remove leading slash from RewriteRule:
RewriteEngine on
RewriteRule ^mylong/uri/contact/path/?$ /myForm.php [L,NC]
.htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.
I have a config.ini file with some values. One of them is the path to the root of my script. So in my js file i get the content from the config.ini file, but i have one big mistake. To load the data from the config file i already need one value from the config file, namely the path to the config file.
Any idea how to handle that?
Regards
Sylnois
Edit:
This is my htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^([^/]+)/$ index.php?token=$1 [L]
This rewrites my link from http://domain.com/fun/bla/index.php?token?123 to http://domain.com/fun/bla/123/ ..
So if someone access the second link, my js script would not be run anymore, cause i work with relative paths. I have now a value in my config which points to the root directory of my applicatoin: "./fun/bla/". Everything works so fine. But my requirement are, that no paths should be implemented in my code.
Yes. Store the path to your config file in code. The rest can be loaded from the config file.
You can't possibly have everything in a config file. I once worked with someone who tried to store database configuration in the database. And then realized their mistake when they tried to make the application, you know, work.
What I've always done is to statically define the name of the config file in my code, so in your JS:
config_file = '/path/to/myconfig.ini'
This is a chicken and egg problem. The config file cannot contain the path to the config file, its path needs to be known to all parts of the program that need to know the settings. Perhaps have the path as a global variable in your program somewhere?
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 have a PHP web app located on shared hosting.
My goal is to modify .htaccess file from PHP code when the PHP page is running.
I need that .htaccess to insert a couple of mod_rewrite lines into it.
The problem is that on Windows+Apache I can dynamically modify .htaccess file
but the same code on Linux reports a problem when I try to access this file in any
way (copy or fopen):
"failed to open stream: Permission denied"
I have given .htaccess file 777 permissions - still no result.
WHat prevents me from doing this? How can I develop a workaround?
P.S.
My initial goal was to be able to add a new RewriteRule into .htaccess that maps a
newly added category_id with new category_name.
If it wasn't shared hosting, I would use something like RewriteMap (in main Apache config) and would be able to access the map file.
This is the first real limitation I've been unable to tackle with PHP+Apache, but I hope it's circuventable too.
This seems like an overly-complex solution to just having a general "load category" page that takes the category name from the URL and loads the corresponding ID.
For example, if the URL is:
http://yoursite.com/category/programming
I would remap that to something like:
http://yoursite.com/category.php?name=programming
I want to suggest something else that also works. Instead of writing a rule for every 'special' url, why not use one for all?
I found it a whole lot easier to use what wordpress uses: every url is redirected to the index.
All you have to do is, set up the index file, read in the directory that was loaded (perhaps using $_SERVER['URI_REQUEST']), and deal with it.
add to .htaccess this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Thanks to that chunck you have a system somewhat unlimited at your disposal. If you ever feel like renaming you categrory url, or add another special case, it's already ready!
You only need a small set of rewrite rules. To do what Chad suggests:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/category/.*$ category.php [QSA]
Thus, anytime someone navigates to /category/category_id, the request will be directed to category.php, which will be handed the /category/ URI in $_SERVER['REQUEST_URI'], from which you can easily get the category ID, and you don't need to bother with editing the .htaccess file every time you add a category.
While I agree with the above comments, it can definitely be done. PHP apps like WordPress do exactly this based on changes made on the settings page. It should be as simple as writing the file out however the parent directory NEEDS to have permission for the web server user to write to it.
If it isn't working for you the trick will be making the parent directory either 777 or 775 and having the group set to whatever group Apache runs under (usually "apache" or "www" or something similar)
Adam (commented on your question) is quite correct though, some other security layer on your server might be preventing you from doing this, and this is probably a good indication that you might be approaching the problem the wrong way.
I agree with Chad Birch. In case you can't be dissuaded, though, in your situation I would first be looking for parent directories with locked-down permissions.
FYI, one of the reasons that rewriting the .htaccess is a bad idea is that any requests that come in while the .htaccess is being rewritten will not have any of your redirects applied.