.htaccess dynamic variables from php? - php

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.

Related

Custom URL to point to php file

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]

Mapping URL into custom files

I want to map URL in my localhost XAMPP into custom files.
For example:
localhost/index.php --> d:\xampp\htdocs\index.php (default)
localhost/normal/data.php --> d:\xampp\htdocs\normal\data.php (default)
localhost/view/userinfo.php --> d:\xampp\htdocs\view.php?p=userinfo (custom)
localhost/view/welcome.php --> d:\xampp\htdocs\view.php?p=welcome (custom)
So, basically, all URL that goes into inside view path will be mapped to view.php files with the filename.php (minus the .php) as its query parameter. There's actually no physical folder view, and no physical files userinfo.php and welcome.php inside the folder.
The reason that I need to do this is that so I can pass all the pages that viewing data into an "application frame" that will wrap the page with header, menu, and footer, and I don't need to give header, menu, and footer call in each page. I might have the actual files userinfo.php that I can $include_once, or I might not (I can just generate it from within the view.php), but hey, that's one of the power of this kind of framework, right? And, if someday I need to change this structure, I can change it from just within one file (view.php), not all.
Can I do this in PHP and XAMPP? How? I've noticed that some website seems to used this practice (URL which have no actual files or even path at all), but when I try to read tutorial for it, I got confused.
URL mapping in PHP?
The accepted answer listed 3 links to learn about URL rewriting. Mostly they're written for Apache in Linux, and mostly they pull all the possible scenario and configuration that I got confused which one I really need with all those long documents and technical jargon. I need just the practical step of my specific problem, and then, I will be able to start from there to explore myself if I have more advanced needs. Please help.
if you do want to go down the mod rewrite route adding the following to an .htaccess file in the site root should do it. You will need to make sure mod rewrite is on for XAMPP and I can't help you there I'm afraid. As you can see it rewrites the url, not the windows filename - so it would work on any OS.
The ([a-z]*) means it will take any filename.php with lowercase letters and redirect to /view.php?p=$1 where the $1 will be replaced by filename.
the [L,R] (L means last rule so stop processing if any more are reached, and the R means redirect (it will change the url in the browser). Use P instead to reverse Proxy (the user will still see the url they requested but the server will serve the correct file) - This will require mod_proxy as well.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/([a-z]*).php$ /view.php?p=$1 [L,R]
</IfModule>
XAMPP uses apache so the rewrites would work the same in Windows as they do in Linux. You could place a .htaccess in the site root directory with some rewrite rules.
However, using PHP
in d:\xampp\htdocs\view\userinfo.php you could include the line
<?php
header('Location: http://localhost/view.php?p=userinfo');
?>
But this must be before any thing is echoed to the screen (even whitespace).
You can use the Apache module mod_rewrite to edit requests before they hit PHP. You want to put something like the following in a .htaccess file in your htdocs directory.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/(.*)\.php.*$ view.php?p=$1 [L,QSA]
QSA means Query String Append. This means that if there are any GET parameters set on the original request they will be appended to the end of the new request too.
Note that this assumes that Apache is configured with AllowOverride enabled and the mod_rewrite module loaded.

unwanted URL switch and security concern. looking for a simpler or better solution

I couldn't do it any shorter :-/
I'm having a problem with my php project concerning redirects and $_GET.
I hope you will help me :)
Lets say I have these 3 folders in the root of my Webserver.
/root
/themes
/page.php
/project1
/index.php
/content.php
/project2
/index.php
/content.php
/themes contains a php file called /page.php
/page.php contains a dynamic PHP/HTML structure or theme, which I can use for different content files.
/project1 and /project2 contain mostly HTML content files which get included by /page.php.
/project1 and /project2 are the root folders for 2 different domains, so each contains an index.php.
Now the problem:
In order to get the websites for each domain to work I have to redirect the user to /themes/page.php, because thats where the functionality is and then include the correct content files.
To let /page.php know what content file out of /project1 or /project2 it has to load I use a redirect for index.php to fill $_GET ,like this :
header("Location: root-domain/themes/page.php?path=project1&file=content1");
That way page.php gets to know the path and filename of the requested file, by taking it from GET.
BUT:
That way the user gets to know each folder and each filename, even of some php files. I don't know if that's good?
And the URL changes from the requested URL into the URL of my webserver root.
Does one know a better solution?
Thanks! <3
Actually, I think there is a solution, you just need to use mod_rewrite, which is a module for Apache. With that, you could redirect all your URL requests to this one specific page of yours. Unfortunatelly, the "magic" behind mod_rewrite is little bit more complex, so I recommend you this documentation for further uses.
But for now, just check if you have a mod_rewrite turned on in server configuration, then simply create a file called .htaccess in directory project1 (or any other) and insert this little piece of code in it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([^/.]+)/?([^/.]+)?/?$ /themes/page.php?path=$1&file=$2 [QSA]
</IfModule>
Hit save, and that should be it. Now every page request goes through /themes/page.php.
And by the way, you definitely shouldn't be exposing script names in url.

Replace mod_autoindex with PHP indexer in all directories without adding a index.php to all of them

I like the ability to automatically index a folder, so that I can serve a large number of files, without adding links to a page all the time. However I dislike the httpAuth login box that comes with using .htaccess to secure a directory. Plus there are more features I wanted on my indexes. So I have written a PHP script to generate indexes the way I want, so that I can control everything with PHP, store users in SQL, add extra links to my file editor, and log in with a nice looking web form.
The problem is any new directory needs an index.php file that includes the script, or I just get apache indexes. Which means copying a one line index.php file to every directory. I could generate it using PHP, but if I am working with FTP to manage files that will not solve all the problems. Is there any way to configure apache to display my index script in any directory that does not have an index files of its own? Such that it acts just like mod_autoindex? But with my custom script.
Put the index.php in your root, then use redirect
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d #if directory exists
RewriteRule . /index.php [L]
Then inside index.php use $_SERVER['REQUEST_URI'] to figure out which folder was requested, then use PHP to display what you wanna display, e.g. using opendir, readdir, etc (don't forget to handle the '.' and '..' unix files)

Is PHP allowed to modify .htaccess file in current folder?

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.

Categories