Reaching files outside od the documentroot scope - mod-rewrite

I'm writing a script to build a dynamic website,
the website is created by loading external components.
The DocumentRoot is location at /sites/website/public
the components directory is located at /sites/website/components
i wannna reach the data directory of each component depends on the requested url.
for example:
the url:
http://ibuildmywebsite/component-data/randomimage/demo/swan04090044_small.jpg
should fetch the file /sites/website/components/randomimage/data/demo/swan04090044_small.jpg
how can i achieve that ?
i would prefer a way that can be placed inside .htaccess (if there is one) instead of modifying the virtual host definitions.
Thanks!

Combine RewriteRule with Alias maybe ?
Alias /randomimage /sites/website/components/randomimage
RewriteRule ^component-(.*)/randomimage/(.*)$ /randomimage/$1/$2 [R,L]
(Won't work in .htaccess though)
You could probably also use Symlinks with:
Options +FollowSymLinks
And link dynamically components-*/randommimage/* to /sites/website/components/randomimage/*/*/

Related

apache2 silent redirection not working as expected

I have a web server using apache2, and I want to silently redirect some pages.
So if an user try to access "http://website.com/test.php", the page at "http://website.com/fool.php" will be used instead. And i need to do that for several pages, not just test.php.
I though I could use the mod_rewrite module for this purpose, but I cant get it working.
I tried adding in apache2.conf, so i could have at least just a redirection for now (not silent):
RewriteEngine on
RewriteRule "^/test.php$" "/fool.php"
or
<Directory "/var/www">
RewriteEngine on
RewriteBase "/var/www"
RewriteRule "^/test.php$" "/fool.php"
</Directory>
This result in the test.php not being redirected at all (it still can be accessed), but also, it breaks my custom 404 and 403 pages with the error "ForbiddenYou don't have permission to access this resource.Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.".
Apache2 is not complaining about the syntax when i restart it.
Setting it in my sites-enabled config leads to the same result.
Note that the goal is not to prevent the direct access to test.php, and the request must result in a 200 success code.
There may be something i do not understand, because searching on the net, it looks like it's the way to achieve this.
You may try an alternative method: use the .htaccess file. (place the file in the root directory of your site)
For example:
RewriteEngine on
RewriteRule ^/test.php$ /fool.php [L]

How do I set default page for my website if the index file is located inside a couple of more folders in htdocs?

When I load my website I want the default page to be loaded but it is located inside two more folders in htdocs. I tried changing .htaccess but it did not work as I expected. This is my directory structure:
htdocs\hotel-management-repo\hotel-app\index.php
What rule did you apply in the .htaccess? Have you tried something like:
RewriteRule ^index.php$ hotel-management-repo\hotel-app\index.php [QSA,L]

How to setup laravel in this url format http://ip-address/~username

I had setup Laravel projects before but this is my first time setting it up on this url format: http://ip-address/~username.
When I first visited the url, it just shows the directory listing so I tried accessing it by appending /public in the url and it works. However all the other links have /public on it and leads to a 404 error.
This is the current directory structure:
inside public_html
- app
- bootstrap
- config
- database
- public
- other folders
- htaccess
- other files
I tried putting an htaccess file in the project root folder.
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
However it still doesn't work and I don't know what to do anymore. If you have been in this kind of situation before, please help me. Thank you in advance.
You'll need to set your WebServer's document root to the public directory, that way it looks in the public and you wont have to append your url with public all the time.
Either that or you'll have to move your index.php out of the public directory and put it inside the root directory your web server is currently set as, but I would recommend you do the first option.

issue with shifting Kohana 2.4 from root location to make it accessible via a directory?

I have developed a kohana project 2.4 version, with i has been developed by configuring virtual host and we are accessing it using some domain aa.com, now i have to move the project to folder named kohana and make it access via aa.com/kohana. If i access it this way then my url changes, So the css & js file are not included since the src url i have provided like /public/css/ so it's not working, same goes for calling a controller. like il be calling a controller using this format /user/login/. now the url when i click to shows like aa.com/user/login which is not found, in turn it should automatically comes like aa.com/kohana/user/login. Is there any way by changing the htaccess file or hacking routes.php file. ?
I should able to automatically append /kohana/ before the url.
Please suggest me a method how it can be achieved.
Yes there is!
I've only started after kohana 3.0 but you can just change the rewritebase.
In your .htacces it probably says "rewritebase /"
Change this to your new directory. "rewritebase /kohana/"
If 2.4 has a bootstrap (i'm not sure) somewhere it should say "base_url". You change this the same as the .htaccess. /kohana/.
edit: here's an example:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /kohana/
# Protect application and system files from being viewed
RewriteRule ^(application|modules|system) - [F,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]

url rewrite debian

I just got fresh debian VPS into my hands.
I installed url rewrite and simple things like this does seem to work
RewriteRule ^login$ /login.php
However this doesn't work:
RewriteRule ^cats$ /index.php?cat=cats
Anyone have idea why?
(it works on local xampp server + it worked on shared host I had)
Thanks
First of all make sure that you have mod_rewrite, and it's on. To do so, create an empty Php file, then add <? phpinfo() ?> and diplay it in your browser.
If it's "on" then you can go on.
If it's in a .htaccess file then try with / without the slashes.
RewriteRule ^cats$ /index.php?cat=cats
Or
RewriteRule ^/cats$ /index.php?cat=cats
And if that's not enough:
Two hints:
If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

Resources