I would like to be able to tell what the base directory of a site is automatically. Say I have a website in this location on a Unix machine:
/home/webserver/mywebsite
And it's publicly accessible here:
http://mywebsite.devserver.com/temp
I would like to be able to move the website from "temp" to the root of the site without having to manually change my php configuration files in my framework.
I guess what I need is the public facing directory of the index.php file, so say I have a .htaccess rewrite rule that means:
http://mywebsite.devserver.com/pages/page
.. will load the index.php file in the root and will return the page at /pages/page, I would need the "base directory" of the site to return "" (An empty string) because the site belongs directly after the domain name. But then say I put the site inside a directory like:
http://mywebsite.devserver.com/directory/pages/page
I would need the "base directory" to return "directory" so I can use it within the code elsewhere.
I am very sorry if this sounds vague and I will happily explain more if needed but any help will be greatly appreciated!
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=BASE:%2]
It compares REQUEST_URI variable (which is the complete URL) with the URI matched by RewriteRule (which is relative to current path) and gets differential in %{ENV:BASE} variable.
Related
I've noticed a strange situation that occurs when I try to create a mod-rewrite entry where the source path is also the same name as an existing php file in the same folder. See example
RewriteRule ^users/([^/]*)/([^/]*)$ redirect.php?page=user&name=$1&id=$2 [L]
The issue is that I'm calling the directory "/users/" as the source path in the rule and if I also have a file in the root folder with the same name "users.php" then the rule above ends up pointing to that file instead of the page I want to redirect rule to point to (which is redirect.php in this case).
Any suggestions?
That's because of Apache content negotiation.
To disable it, put this line in your htaccess (for example, before RewriteEngine on line)
Options -MultiViews
I want to extend the following .htaccess rewriteCond to also check if the requested file exists within a subfolder in web (and serve it, not redirect - but serve the file)
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
for example I have the following files in sub-directories within Web folder of symfony project
static/contact.php
static/wiki/countries.php
the request URLs look like this
localhost/symfony/web/contact.php (www.domain.com/contact.php)
localhost/symfony/web/wiki/countries.php (www.domain.com/wiki/countries.php)
i do not want to redirect, just want to serve the contents.
Let me elaborate a little further.
I am currently in the process of upgrading an old php project to symfony2 framework.
The old project has the following URL structure for static pages
www.domain.com/faq.php
www.domain.com/contact.php
www.domain.com/wiki/countries.php
Now in order for this to work as it is - I have to copy the static page and the wiki folder in the WEB folder of the new symfony2 project. Which is fine and the above given Rule / conditions would work and the above URLs would work.
But I do not want to copy the static pages into WEB as it will clutter the WEB folder, and i want to keep it clean - So I moved the static pages to a sub folder within WEB folder called static. Now the above URLs do not work, and throw a page not found error coz all the pages are within web/static folder. so with the current Rule / cond the URLs will have to be changed to
www.domain.com/static/faq.php
www.domain.com/static/contact.php
www.domain.com/static/wiki/countries.php
Is there a way where I can keep the previous URL structure and keep the static pages within a sub folder in web? one way which i can think of is that to have a cond and rule in .htaccesss file within WEB folder to also check for requesting files within static folder and if it is found, then serve them (not redirect)
thanks for you time and help, much appreciated.
I'm trying to find a way to dynamically change the root folder of a site. I currently set the root folder with the following:
RewriteCond %{DOCUMENT_ROOT}/foo/bar%{REQUEST_URI} -f
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/foo/bar%{REQUEST_URI} [QSA,L]
So if a user visits http://example.com it's pulling from /foo/bar. However I would like to be able to change /foo/bar to any other random directory of my choosing with PHP.
This is done is because the root folder of the site can be changed quite regularly for various reasons.
I could just write a PHP script that will replace /foo/bar in the .htaccess file but that's not a wise decision to give PHP write access to .htaccess. Ideally I would like to have a simple .txt file that can be included to pull in /foo/bar and then PHP can write that .txt file.
Is this possible, or is there any other way to get this done? Essentially the thing that matters is that PHP can safely change the root folder without a Apache restart.
You might want to look into RewriteMap, but actually, I'd make a symlink, and point it to the proper place.
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"`
I have a php websites hosted on a server. I use CPanel to manage it. public_html has lets say following directory structure
public_html
- dir1
- dir2
- dir3
- ....other files.....
- website2home
Now I am trying to make website2home as the base directory of my website, but files inside website2home use some files from public_html folder and some files from within itself.
THE PROBLEM
When I assign a domain name to website2home, It does not reads the files from public_html folder (in fact, public_html folder is not visible) and shows some php warnings and some 404 not found errors. But lets say the domain name for original website is www.aaa.com, then if I access website2home by using www.aaa.com/website2home all works fine.
So My Question...
Is there any way to set the base directory, so that my website2home fetches all files and correct files without replacing hundreds of filepaths in php code?
Please answer considering that I don't want to modify source files of website.
I believe you are looking for some kind of .htaccess configuration. For instance, you may set your website2home folder as base folder for your www.aaa.com domain and then redirect some file access to other folders using .htaccess, something like this..
RewriteEngine on
RewriteRule captcha.jpg ../captcha.jpg
RewriteRule \.pdf$ ../pdf_files/$1
Have a look to mod_rewrite documentation
Edit: If, for some reason, the ../ redirection doesn't work, you always can redirect all your files to a pseudo-index PHP page. For instance:
RewriteEngine on
RewriteRule captcha.jpg my_index.php?access=../captcha.jpg
RewriteRule \.pdf$ my_index.php?access=../pdf_files/$1
And then, in your index PHP page you can simply load your PHP files in other folder:
require "../pdf_files/$requested";