PERL Dancer2, cgi dispatcher and uri_for helper - apache

I have a web interface that i need to maintain and improve, written in a plain old CGI.pm way. That app is not using templating system neither. It's served by apache, like that:
/var/www/vhost/myapp/cgi-bin/app.cgi
/htdocs/css/styles.css
/htdocs/js/script.js
To add new pages to that app, and make my life easyer, i want to host a Dancer or Dancer2 app next to it, i was thinking about something like that:
/var/www/vhost/myapp/cgi-bin/app.cgi
/cgi-bin/dispatch.cgi (the one from Dancer2)
/htdocs/css/styles.css
/js/script.js
/dancer_public/...
/dancer2/lib/...
/views/...
/environement/...
In the dispatcher, i'm changing the path to reach the Dancer2 bin/app.psgi.
I added the following .htaccess file to the htdocs dir:
# BEGIN dancer application htaccess
RewriteEngine On
RewriteBase /v2
RewriteRule ^/v2$ /cgi-bin/dispatch.cgi [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule v2/(.*) /cgi-bin/dispatch.cgi/$1 [L]
# END dancer application htaccess
By doing that,
accesssing to example.com/cgi-bin/app.cgi show me the old app
as expected
accesssing to example.com/v2 show me the dancer
app home page, default one, without css, which is expected as i moved
the public dir, and some other modification
Now that you have the setup, here is where i have an issue,
the redirection from v2 to the dancer app is working really well, however, in order to keep consistent, i would like to have the uri_for creating url from v2 , but it's using the SCRIPT_NAME as base url, to show the issue, when i'm creating that url: uri_for('/mypage'),
i would like to have that url : example.com/v2/mypage,
but instead i have url example.com/cgi-bin/dispatch.cgi/mypage
SCRIPT_NAME environment variable is containing cgi-bin/dispatch.cgi , i have one terrible hack, that is working, but clearly not the solution, i can have my expected behavior by forcing SCRIPT_NAME to /v2 in the cgi dispatcher, in a BEGIN block.
If someone as a clean solution for that, that would be of a great help!
Thanks

Have the same problem with D1.
$ENV{SCRIPT_NAME} = '';
inside dispatch.fcgi helps me.

Related

Rewriting directory into a query parameter in htaccess

I converted a website I'm building into a web view app in iOs.
I would like to track visitors that use the app instead of the website by adding a directory to my URL.
For instance, the "about" page URL would go from "https://example.com/about/" to "https://example.com/app-ios/about/"
My question is how to write an htaccess rule that tells my server to go to the path "/about/" and skip the "/app-ios/" directory?
Also, I'd like to add ?app=app-ios in my query parameters.
The most promising thing I found was this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/node/(.+)$ /search/node/?app=$1 [QSA,NC,L]
But I'd need to specify what comes before "foo".. In my case, "app-ios" is at the beginning of the request uri, always.
Plus I don't want a redirection. I just want my server to read /app-ios/something/other-thing/ as /something/other-thing/?app=ios.
First one question for tracking I would recommend to make this over a Query Parameter (as you wrote) like ?client=ios, but this might be an own opinion.
For rewriting the URL you could do the following:
to remove from the IOS from the URL (not tested):
RewriteEngine On
RewriteRule (.*)ios/(.*) $1/$2?client_id=ios&%{QUERY_STRING} [L]

Apache 2.4 .htaccess friendly URL rewrite

Having some issues understanding the .htaccess file and getting it to work properly. .htaccess is recognized....i entered plain test at the top and got the internal server error. And it appears mod-rewrite is working...because I downloaded a test php file and it works.
I am using Apache 2.4.23 with no php pages
However i cant seem to get this to work right.
I am trying to take this url
http://example.com/ProjectTest/index.shtml?dynContent=Content1
and rewrite it like this
http://example.com/ProjectTest/Content1
I am also trying to do this generically across the site since ?dynContent=whatever will be a constant.....and trying to keep the same path.....like the below example
Change this
http://example.com/ProjectTest/ProjectFolder/index.shtml?dynContent=Content1
to this
http://example.com/ProjectTest/ProjectFolder/Content1
I am not very good with the .htaccess files
UPDATE: Ok...I got this to work.....not really sure why it works....but when I type in the test-dev/ProjectTest/apples or test-dev/ProjectTest/oranges it calls up the appropriate content.
This is what I used
RewriteRule ^ProjectTest/([^/\.]+)/?$ ProjectTest/index.shtml?dynContent=$1 [L]
and then I rewrote it to be this and now it works for any directory
RewriteRule ^(.*)/([^/\.]+)/?$ $1/index.shtml?dynContent=$2 [L]
Try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^dynContent=(.+)$
RewriteRule ^/(.+)+/index.shtml $1/%1 [PT]
This requires mod_rewrite to be activated. The value of the bracket in dynContent=(.+) is stored in %1 and the bracket's value in ^/(.+)+/index.shtml is stored in $1. Both together creates your target path http://example.com/ProjectTest/ProjectFolder/Content1

Allowing relative paths only

I believe this is an Apache .htaccess issue, and yet, maybe not. Maybe I am looking at the problem from the wrong angle, and thus can't find the proper solution.
I am building a web app + hybrid mobile app. I would like to share the exact same code base, without having to tweak anything manually to deploy my app to Android or iOS, otherwise, the process of deploying will be hacky and painful. What I want is to take the web app repository, shove it into Cordova's box (you dirty man ;), and it would deploy it successfully.
Now, one issue is that Cordova requires relative paths to work properly. For example, this is how I include my require.js file :
<script data-main="library/js/dependencies.js" src="library/js/libs/require.js">
</script>
This works fine on the hybrid app. This works fine also on most of the web app's URLs, those with the following scheme :
domain.com/view_name
However, this is what happens when I load the app from a view that receives URI parameters :
domain.com/view_name/6iwO4NyJqy
The relative paths are not resolved properly anymore. I get 404 error due to unproper paths. For instance, this is how is resolved the require.js file above :
http://domain.com/view_name/library/js/libs/require.js
The view_name bit is the wrong part. It should not be there. Without it, the file would be found successfully.
This is my .htaccess file :
RewriteEngine on
RewriteBase /
# REROUTING EVERYTHING TO index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* /index.html [NC,L,QSA]
Is there a way to set my .htaccess file, so that I don't need to modify the relative paths within the app, and still can have them resolved properly ?
Any suggestion is most welcome.
It is not caused by your rewrite rule, it is due to your use of relative paths.
You can add this just below <head> section of your page's HTML:
<base href="/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

.htaccess rewrite root to folder, yet block direct urls to same folder

I've been researching and trying for a week to accomplish this, but I haven't been able to find my solution. I'm sure it's possible, but I'm just not an expert in the depths of voodoo magic.
The setup:
An installation of MantisBT located in
mysite.com/mantisbt/currentver/mantis-1.3.19
When I perform an upgrade, I want to archive all old versions and old
database dumps to /mantisbt/oldversions/ to keep things tidy.
I also have other utilities and pages in various subdirectories, for
instance "mysite.com/utils"
The goal:
I want users to type in mysite.com/ (root) and have the URL rewritten
(transparently) to /mantisbt/currentver/mantis-1.3.19/ so that they
don't see my directory structure and it looks like the mantisbt app is
located in the root directory.
I also want protection from anyone trying to directly access a
subdirectory beginning with "/mantis". For instance, if
someone directly types mysite.com/mantisbt/currentver/mantis-1.3.19/
into their browser, I want them redirected back to the root directory
so they access the site from root just like everyone else.
I also need to allow my other subdirectories like mysite.com/utils to
be accessible if I type in the full path to them.
The biggest problem I've encountered is that Apache loops through your .htaccess file again every time the URL changes. So I get stuck in these rewrite loops. I've tried looking at every possible tool that Apache offers, but I'm seriously outgunned here. I could provide examples of what I've tried, they're obviously not correct, but ask me and I can post them.
You'd be far better off altering the DocumentRoot in your httpd.conf, and mapping in the utils directory, via a Location directive. It will be far cleaner, and Apache won't have to do some trickery with every request.
Anyway something along the following lines should work.
# Stop direct access
RewriteCond %{HTTP_REFERER} !mysite.com [NC]
RewriteRule /?mantisbt/currentver/mantis-1.3.19 / [NC,L,R=301]
# Internally Rewrite everything apart from /utils to /mantisbt/mantis-1.3.19/
RewriteCond %{HTTP_REFERER} !mysite.com [NC]
RewriteCond %{REQUEST_URI} !/utils [NC]
RewriteRule .* /mantisbt/currentver/mantis-1.3.19%{REQUEST_URI} [L]

How manage several folders in my domain?

I want create subdomains like this:
domain.com/type/city
An examples:
domain.com/restaurants/new_york
domain.com/hotels/new_york
domain.com/restaurants/chicago
I have thousand of cities in a mysql database.
I thinked in some options:
Thousand of folders with an index.php for redirect (I think wrong way).
Create an sitemap with all links (domain.com?type=hotels&city=chicago) and manage they by code with the database.
Apache?
Please, which will be the best way for this? Thanks in advance!
You can solve this with a combination of PHP and Apache configuration. That is the most common solution and seen in popular PHP website software such as Drupal and Wordpress.
The idea is to let Apache send all traffic to one index.php file and pass the rest of the path as a parameter for PHP to handle with it.
You will need to be carefull with a few edgecases though; if file such as ./public/styles.css is requested, you don't want to serve that trough your PHP application but want apache to serve the file directly. Existing files will need to be handled by apache, all else by you application.
In your .htaccess:
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
The first line tells Apache to send normal files by itlself. Second line does the same for existing directories. Third line avoids that browsers (most notably version IE6) who request the example.com/favicon.ico don't hammer your PHP application.
Then it passes everything along to index.php and adds the rest of the path into the q param.
Inside index.php you can then read that, and take action with that:
<?php
$path = $_GET['q'];
$params = explode('/', $path);
print $path;
print_r($params);
?>
Thousands of folders would be the wrong way that is for sure.
If you start creating the sitemap with links of the type domain.com/?type=hotels&city=chicago you get a nice structure that you can manage programatically.
First get this started and working, then look up .htaccess and mod_rewrite which you can then use to map from domain.com/type/city to your links already functioning.
This seems both to be a good strategy for getting something working fast, and for ending up with the prettiest solution.