.htaccess URL Rewrite to affect Subdirectories - php

I'm trying to achieve something with .Htaccess RewriteEngine.
I want to be able to controll subfolders via one single .Htaccess file, currently to get this working I have to have the same .Htaccess file in every folder & subfolder but it would be great to just have one in the ROOT folder to avoid copy-pasting.
Currenty my structure looks like this:
ROOT
Images
2012
January
February
...
2013
...
Other
Misc
Folders contain images that are accessed by index.php?i=IMG_NAME and by using the following
RewriteEngine on
RewriteRule ^([^/\.]+)$ index.php?i=$1 [L]
I can use for example: Images/2012/January/IMG_NAME and it works fine to load the image.
But as mentioned I have to have the above .Htaccess file in every folder & subfolder to get it working, otherwise I have to use Images/2012/January/?i=IMG_NAME are there any workarounds for this?

I know this only skirttails the issue but if you don't have the actual url point to a real folder then you can run everything from the .htaccess file from public_html.
So if you actually stored the images in /data/2012/January/*
Then had the url that is used by /Image/2012/January/IMG_NAME

Related

Log all access of subdirectories

I have a directory structure like
/logging.php
/customer1/
/customer2/
/customer3/
...
Now I need to log all access of the subdirs customer1, customer2, customer3 and so on.
Can I somehow (like a htaccess rule) lead all access trough a php file logging.php which saves the access into a mysql database and than forwards to the wanted directory?
I don't want to include the logging.php in every single file of the subdirs, as the subdirs are dynamically generated.
A simple .htaccess rule should suffice.
RewriteRule ^(customer\d+(/.*)?)$ /logging.php?path=$1 [L]
Within logging.php, just look at $_GET['path']
This is assuming that the directory and files in question are in the web root of your site (Usually something like public_html/ or wwwroot/)

.htaccess sub directory in a variable

I would like to know if it's possible to point my domain to a directory which is two folders in from the root.
The point is, I inform the page that will be showed in the content area trought the URL variable, like this:
www.dominio.com.br/index.php?c=products/list (in this way, it works well)
I would like to access this path by using: www.dominio.com.br/products/list
This is my structure...
public_html
.htaccess
index.php
**products**
**list.php** <-- My desired folder.
customers
list.php <-- My desired folder.
new.php
I need a .htaccess code for read this URL with sub-directory folder in the variable. For now, I could make work when using the file in the same folder as index.php.
I hope this all makes sense.
Thanks.
If
www.dominio.com.br/index.php?c=products/list
Works well for you, you could use a rewrite rule to map a 'virtual' folder path to your existing page:
RewriteEngine On
RewriteRule ^products/list$ index.php?c=products/list

.htaccess Completely rewrite URL to show file in a folder, rather than page in root

So, I have the current file structure:
ROOT
-> /public
-> /user_views
user_handle.php
user_profile.php
user_feed.php
user_settings.php
.htaccess
As you see, the folder user_views contains a few of the possible views that the client could want to look at. What I am wanting, is for clients that insert the URL http://example.com/user/ to be directed to the page user_handle.php. This handle would act as a root file for all /user/ pages, and it would accordingly split into those pages through numerous $_GET requests.
So far, I have the following .htaccess, but it's not working...
RewriteRule ^user/ user_views/user_handle.php [L]
What could I do to get this to work, so that the url http://example.com/user redirects to the user_handle file in the user_views folder?
Thanks!
I'm not sure I fully understand your question, but it seems you would like to make user_handle.php located under public/user_views act as a "router" for the rest of you PHP files and have all requests to /user/ (e.g. /user/?page=1) be processed by user_handle.php.
If that's the case, your rule seems legit. The only thing I noticed (I might be wrong) is that your .htaccess is located outside the public folder. In that's the case, you need to include 'public/' as part of your rule.
I recreated the folder/file structure you described and it has worked for me using the following .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^user/ public/user_views/user_handle.php [L]
</IfModule>
Slight chance this is the problem, but you also might want to double check that mod_rewrite, which is the rule-based rewriting engine is enabled on your server/local environment. It should show up under 'Loaded modules' when you call phpinfo() in any PHP file.
Hope this helps.

.htaccess dynamic variables from 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.

Change base HTTP requested directory -- CPanel

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";

Categories