combining mod_rewrite and vhost_dbd_module - mod-rewrite

I'm trying to give users a static files folder served from inside their chroot. The following code works under other conditions (basically without the inclusion of the DBDocRoot directive) but just doesn't do what is expected. Is mod_vhost_dbd simply not compatible with mod_rewrite? It partially works in that the request hits the correct python script which then does some other stuff and launches a wsgi app.
With this code nothing is logged in rewrite.log. If you comment out the DBDocRoot directive line stuff is logged but of course the rewrite rule doesn't work because ${ENV:username} is the empty string. The query work perfectly well when run from a mysql console.
LoadModule vhost_dbd_module /usr/lib/apache2/modules/mod_vhost_dbd.so
DBDExptime 5
<VirtualHost *:80>
ServerAlias *
RewriteEngine On
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 9
DBDriver mysql
DBDParams host=amysqlhost,user=abdbuser,pass=private,dbname=adbname
DBDocRoot "SELECT concat('/mnt/user_storage/webscripts/', username, '/user_script.py'), username from user, userdomain where user.id = userdomain.user_id and userdomain.domain_name = %s" HOSTNAME
RewriteCond /mnt/user_storage/homedirs/%{ENV:username}/var/www/static%{REQUEST_URI} -f
RewriteRule ^/(.*) /mnt/user_storage/homedirs/%{ENV:username}/var/www/static/$1 [L]
</VirtualHost>
Has anyone else successfully used this module to set an environment variable? The notes on the mod_vhost_dbd wiki say that it can be done.

Related

Use both subdomains and subdirectories in Magento multistore?

I currently have a store in Magento with several store views for different languages as example.com/sv/, example.com/fi/ etc. Now I want to add a store to a subdomain as test.example.com but I can't make it work.
I added a new store and a new store view for the subdomain and then changed the Base URL for this. I have created the subdomain and it points to the location of the magento installation. I have also added different lines in the .htaccess file, tried some variations of the ones below.
SetEnvIf Host .*newstore.* MAGE_RUN_CODE=newstore
SetEnvIf Host .*newstore.* MAGE_RUN_TYPE=website
RewriteCond %{HTTP_HOST} .*newstore\.com [NC]
RewriteRule .* - [E=MAGE_RUN_CODE:newstore]
RewriteCond %{HTTP_HOST} .*newstore\.com [NC]
RewriteRule .* - [E=MAGE_RUN_TYPE:website]
But nothing works. When I try to access the subdomain I get redirected to the main store. But I can access the new store as a subdirectory, it's supposed to be like this test.example.com but I have to write example.com/test.
Is what I want to do even possible?
Update 15-04-09
I found a redirect in the .htaccess file that caused the subdomain to be redirected to the main store. I removed it and now I can access the subdomain but the page is just white and it says "Index of /" etc. so I don't get the magento store view.
I use this code in my .htaccess that does what you want:
SetEnvIfNoCase Host "^it\.mystore\.com$" MAGE_RUN_TYPE=website MAGE_RUN_CODE=ws_it
SetEnvIfNoCase Host "^pt\.mystore\.com$" MAGE_RUN_TYPE=website MAGE_RUN_CODE=ws_pt
SetEnvIfNoCase Host "^fr\.mystore\.be$" MAGE_RUN_TYPE=store MAGE_RUN_CODE=fr_be
SetEnvIfNoCase Host "^nl\.mystore\.be$" MAGE_RUN_TYPE=store MAGE_RUN_CODE=nl_be
Also make sure you have properly defined the Base URL (in your example: test.example.com) in System > Configuration > General Web > Secure and Unsecure for your new website or store

Laravel pretty URL

I'm going crazy here! I'm trying to learn Laravel and pretty URLs just don't work.
I have enabled mod_rewrite from my apache config file, I have set AllowOverride to All in my user config file and I have the following in my .htaccess file in public folder of Laravel installation:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]
I enter http://localhost/~user/lara/public/index.php/users it works, but with http://localhost/~user/lara/public/users I get 404 Not Found error
The requested URL /Users/user/Sites/lara/public/index.php/users was not found on this server.
You can see that the redirection works fine, as public/users have turned into public/index.php/users but it says not found, even though when I manually enter public/index.php/users it show me the output.
I have read all the related questions on SO, none of the worked for me!
This is most likely caused by the fact that you are changing the document root during a request. From the looks of your URL (with the ~user segment) you are using mod_userdir or something similar, and what these types of plugins do it allow you to map a given URL prefix to a document root that is not the normal one for the server.
As such, you sometimes run into issues like this where the right .htaccess file is found, but its rewritten URL is against the original document root rather than the modified one and so your index.php file cannot be found (maybe, to be honest I don't really know, this is all conjecture). This is also why going directly to index.php/users works - the problem isn't the setup per se, but the mixing of rewrite rules and the change of the document root.
As such, the fix is to use a RewriteBase line, ad the following to the .htccess file:
RewriteBase /~user/lara/public/

.htaccess redirect to subdirectory for Ruby application deployed by passenger

My Ruby on Rails project is on justhost server. When I am creating there ruby project, it is creating this in folder rails_apps by default.
For my project, I have created symlinks.
ln -s ~/rails_apps/webworth/public ~/public_html/webworth
And
ln -s ~/rails_apps/webworth ~/public_html/webworth_app [Purpose of this symlink just for browsing files by ftp client]
I created .htaccess file in ~/rails_apps/webworth/public directory. Below is the .htaccess code
Options -MultiViews
PassengerResolveSymlinksInDocumentRoot on
RailsEnv development
RailsBaseURI /webworth
SetEnv GEM_HOME /home5/worthgur/ruby/gems
and one .htaccess file in ~/public_html. Below is the code:
# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
RewriteEngine on
Options +SymLinksIfOwnerMatch
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?webworth.com [NC]
RewriteCond %{REQUEST_URI} !webworth/ [NC]
RewriteRule ^(.*)$ "webworth\/$1" [L]
Everything is working proper with these structure of .htaccess files except one problem. Extra text is appending to all urls in my project i.e "webworth" for all links generated by routes.rb file. And I want to remove this extra text "webworth" from all URL's.
Now urls are generating for example in this manner.
http://webworth.com/webworth/tags/smartphones
while it should be http://webworth.com/tags/smartphones.
This extra text i.e "webworth" was added previously for the folder that has to be accessed since it was not in the document root for the site and because of this extra text, extra text is appending to all URL's generated by routes.rb file thru ~/rails_apps/webworth/public/.htaccess file [Reason: RailsBaseURI /webworth]
I guess this can be fixed by changing RailsBaseURI /webworth to RailsBaseURI / in ~/rails_apps/webworth/public/.htaccess file.
I did that and I am sure I could not write proper commands in ~/public_html/.htaccess file so that I can redirect the traffic from the public_html to the symlink (webworth) for my site. I used many options but couldn't figure out how it will work. Please help to write ~/public_html/.htaccess file properly to redirect the traffic from public_html folder to my symlink(webworth).
Justhost suggested this link: How to host the Primary Domain from a subfolder. But still I could not figure out the issue. Thanks
I had the same problem with justhost and I solved it without adding any redirect.
I set my rails app public folder to be the main site public folder.
I renamed the original "public_html" to "public_html_backup", just in case.
Added a symlink ln -s ~/myRailsApp/public ~/public_html
in my htaccess I changed RailsBaseURI /myRailsApp to RailsBaseURI /, resulting in:
<IfModule mod_passenger.c>
Options -MultiViews
PassengerResolveSymlinksInDocumentRoot on
RailsEnv production
RackBaseURI /
SetEnv GEM_HOME /yourUserPath.../ruby/gems
</IfModule>
I hope this can help you.
Cheers

Using .htaccess to serve static files via a subdomain

Right, excuse my stupidity, I've looked through a load of examples on t'interweb but I don't think I've found what I'm looking for.
I have a website, photography.example.com is the main site but I also want to have another subdomain to serve static files, for example static.photography.example.com.
If I request a file (e.g. http://static.photography.example.com/js/jquery.js) I want that file to be retrieved from the non-static domain, allowing me to keep my file structure completely untouched but using multiple domains to allow more concurrent http requests.
I don't want to throw any http responses that would make the browser thing the file has been moved, I just want to mirror the files from the normal domain to the static domain. After this I would proceed to set far future expired to improve caching etc.
How do I achieve this using .htaccess?
EDIT 1
So after a bit of messing around I have come up with this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ http://photography.example.com/$1 [L]
But this actually redirects to the domain I'm trying to read, I want it to serve the file up under the static domain name, any help with modifying this script would be greatly appreciated.
EDIT 2
So I've amended my DNS and waited a few days for it to propagate but the CNAME technique doesn't work either. Here's the entry:
In your question, you're talking about:
Adding Expire headers for caching
Splitting resources across domains
For Item #1, you can edit your httpd.conf/.htaccess file on your main domain (it doesn't hurt doing it on your whole website, no?)
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType application/x-javascript A2592000
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000
</IfModule>
Item #2 doesn't need any Apache configuration - just configure your static.photography.example.com DNS entry with CNAME photography.example.com. That should do the trick.
Or you can edit your httpd.conf and add a ServerAlias
<VirtualHost xx.xxx.xxx.xx:80>
DocumentRoot /
ServerName photography.example.com
ServerAlias static.photography.example.com
</VirtualHost>
So for now, you don't need a separate virtual host with dedicated Apache configuration.
Here are a few other reasons why you'd want a separate domain, and a separate virtual host with dedicated configuration:
Your main domain has cookies and you want to use a cookie-free domain
You actually want to use a CDN (or your Amazon S3 account to reduce bandwith on your primary hosting)
If you want one of those, or if your caching needs are too complex (you don't want to cache all JS/CSS/images, but rather a subset of it), then your only solution is: get your hands on your httpd.conf and write separate configurations for each domain
My understanding to the question is to REDIRECT
http://static.photography.example.com/js/jquery.js
to get the file from following address (without changing the URL on the browser):
http://photography.example.com/js/jquery.js
But keep URL to all existing files like the following to NOT REDIRECTED:
http://static.photography.example.com/images.jpg
If it is true then this .htaccess should work:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)?.photography.example.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) http://photography.example.com/$1 [P]
Moreover you can also use MAPPING if you have a complex rules like:
RewriteRule ^/file1\.js$ http://photography.example.com/file.js?q=444 [P]
RewriteRule ^/file2\.js$ http://photography.example.com/file.js?q=345 [P]
RewriteRule ^/file3\.js$ http://photography.example.com/file.js?q=999 [P]
create a text document (e.g. map.txt) in the folder with .htaccess and put in the following:
file1 444
file2 345
file3 999
The .htaccess will have the following look:
# Set a variable ("map") to access map.txt from config
RewriteMap map txt:map.txt
# Use tolower function to convert string to lowercase
RewriteMap lower int:tolower
# Get requested file name
RewriteCond %{REQUEST_URI} ^/([^/.]+)\.js$ [NC]
# Seek file name in map-file
RewriteCond ${map:${lower:%1}|NOT_FOUND} !NOT_FOUND
# Perform rewriting if the record was found in map-file
RewriteRule .? http://photography.example.com/file.js?q=${map:${lower:%1}} [P]
I think you would need the CNAME record to pass the request for static.photography.example.com to your server and the have .htaccess parse requests for static.photography.example.com in a special manner.
Adding the following rewrite rule to .htaccess should do the trick
RewriteCond %{HTTP_HOST} !^(www\.)?photography.example.com$ [NC]

Ko3 - URL Rewriting problem - Removing index.php

I'm developing a website using Kohana 3 (1rst time I use a framework). Locally, everything works perfectly. At the moment, I have a default template controller, a multi-language support and my 'index.php' is correctly removed. So before going further, I tested if it worked on my server and I got an endless loop.
I followed the tutorial from the unofficial wiki for the multi-language implementation: http://www.kerkness.ca/wiki/doku.php?id=example_of_a_multi-language_website
A redirection to the default language occurs if the language is not specified in the uri so I figured the problem might have come from there even though it worked locally, so I removed it to see what happens without the redirection. Now, I can see my home page, but whatever the uri is in the web browser, the home page will always be called. I inserted the following line in my home view to check what the uri was:
request::instance()->uri() and effectively, the uri is always: /en/home/
I put the index.php back (in the bootstrap) and everything worked fine again, even with the redirection to the default language.
My first guess was that the uri isn't rewritten correctly, so I tried to change the .htaccess but no success...
Here's my .htaccess:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /dev/
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# 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]
(btw I also tried the other RewriteRule in the unofficial wiki, doesn't work either)
Additional info:
Host: WebHostingPad
Apache: v2.2.11
PHP: 5.2.9
Rewrite_Module is activated
Thank you, I would really appreciate your help because I've been trying to fix this for days now and it's really starting to annoy me ;)
The only thing you have to change in order to get rid of index.php in URL is to set the 'index_file' param in Kohana::init ( bootstrap.php ) to FALSE ( everything else can cause an error ).
So the Kohana::init looks like this;
Kohana::init(array(
'base_url' => '/',
'index_file' => FALSE,
));
If it worked with the original .htaccess, there's no need to change it at all.
The problem came from $_SERVER['PATH_INFO'] which returned no value...
This issue can be solved by adding the following line to the php.ini:
cgi.fix_pathinfo=0

Resources