In webapp2 (python) you can map urls to classes in one file.
app = webapp2.WSGIApplication([('/', pgMain), ('/test/', pgTest)], debug=True)
How would I setup such a thing in php & apache?
Thanks
EDIT:
What I am after is everything after a foldername to be redirected to a file in that folder. eg.
/version/1.0/test/folder/ => /version/1.0/index.php & var="/test/folder"
/version/1.0/another/ => /version/1.0/index.php & var="/another"
Thanks.
Simplest way for doing this is to create .htaccess file in your app folder and put this code there:
RewriteEngine On
RewriteBase /
RewriteRule "(^|/)\." - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [NC,NS,L]
Please note that you need to modify "RewriteBase" directive and put path of your application there "/" means your document root.
After creating .htacess file all requests will be routed to your index.php file and you can access URI via PHP's super global variable $_SERVER['REQUEST_URI']
Related
i have a question about writing a rule in the htaccess file which is located in the root directory of drupal. We use the Drupal 7 version.
We want to write a rule for the url that when i go to the page "/newpage/1/name" it should target the "/oldpage?id=1&user=name". So the targetpage is a page which is created in drupal itself (a added content from a contenttype).
But i always get the same "Page not found"-Error. I can access the "/oldpage" by the way
I tried some rules about simply rewriteUrl without any parameters (to make a start). But it also didn't work for me. Here my try:
RewriteRule ^oldpage /newpage [L,R=301]
This line is below this part:
# Pass all requests not referring directly to files in the filesystem to
# index.php. Clean URLs are handled in drupal_environment_initialize().
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
So are there any special rules for writing rules in the htaccess file in drupal? (maybe especially in version 7) or what is wrong on my code?
Reading this site Guide ,I would try the following line
RewriteRule oldpage?id=(.*)&user=(.*)/$ /newpage/$1/$2
I was handed down a project from another developer (who's not available any more and I cannot ask him for a solution) where he'd kept multiple codeignitor projects in a folder and called each of them through their respective index files.
The file structure - /var/www/html/{folder}/mobile_index.php
The mobile_index.php references a folder 'mobile' which resides in the same folder as mobile_index.php.
So the URL that I call is - "xx.xx.xx.xx/{folder}/mobile_index.html". This page opens fine but when I click on any link (e.g. user) within the page, this redirects to "xx.xx.xx.xx/user"
Now this page is blank and my error log says there's no page user in /var/www/html/user. But when i manually convert the link to "xx.xx.xx.xx/{folder}/mobile_index.php/user" this works and opens up the page. I think the folder is missing the .htaccess file but I have no clue what to include in that file, can anyone help me get the links working again?
This is what I have so far -
RewriteEngine on
RewriteCond $1 !^(mobile_index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /{folder}/mobile_index.php/$1 [L]
Options +FollowSymLinks
RewriteEngine On
#RewriteBase / #on server, uncomment this line by removing hash'#'
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* mobile_index.php?/$0 [PT,L,QSA]
This is working localy on my WAMP server, but when I tried to use it on my host it always gets an error:
HTTP 404 (GET /)
• teste/index.php:17 Base->run()
You can see the error here: http://rafaelmsantos.com/teste/
I don't have a clue whats going on, I've tried different .htaccess but it display always the same error.
.htaccess
# Enable rewrite engine and route requests to framework
RewriteEngine On
# Some servers require you to specify the `RewriteBase` directive
# In such cases, it should be the path (relative to the document root)
# containing this .htaccess file
#
# RewriteBase /
RewriteRule ^(lib|tmp)\/|\.(ini)$ - [R=404]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
config.ini
[globals]
AUTOLOAD=public/pages/
DB.dns="mysql:host=localhost; dbname=lod; port=3306;"
DB.user="root"
DB.password=""
DEBUG=3
UI=assets/
index.php
<?php
$lod = require('lib/base.php');
$lod->config('config.ini');
// HELPERS DEVELOPED BY ME
require_once 'helpers/base_helper.php';
//*-----------------------------------------------------------------*/
//* PÁGINAS */
//*-----------------------------------------------------------------*/
$lod->route('GET /', 'PagesController->index');
$lod->route('GET /project/#page', 'PagesController->index');
$lod->run();
And my folder structure:
try to set the RewriteBase in your .htaccess (as the comment above said):
RewriteBase /teste/
Turns out I had to autoload in another way,
insted of AUTOLOAD=public/pages/ on config.ini, I had to use $lod->set('AUTOLOAD','public/pages/'); after the require_once and before the routes.
Plenty of information around the net on how to hide the index.php from your Yii 2.0 application URL, however, what I'm trying to do here is to also remove the '/basic/web/' from the URL. /basic/web is the directory from which the application is running and the configuration that I have so far is the following. That goes into the config file:
'urlManager' =>[
'enablePrettyUrl' => true,
'showScriptName' => false,
],
And this is my htaccess file that I have within the /web folder:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
So far, so good, I can access something directly calling mysite.com/basic/web/controller/action. What do I need to do though to remove the /basic/web so that the URL becomes just simple mysite.com/controller/action?
Any tips welcome, thanks!
EDIT: I'm looking for a way without touching the apache configuration file as I don't have access to it.
RewriteEngine on
# Change yourdomain.com to be your primary domain.
RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$
RewriteCond %{REQUEST_URI} !^/basic/web/
# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /basic/web/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$
RewriteRule ^(/)?$ basic/web/index.php [L]
Change .htaccess permissions to 440 when you're done. Nothing to fear using this method, contrary to what is stated by Mihai P.
You should define your apache configuration in another way. Your site should point to {folder}/basic/web and not to {folder}.
Because you changed the requirements:
For a cpanel setup you should:
1) remove the silly basic folder, what is the point of it anyway? Just because Yii installs that way does not mean you have to keep it. So move everything 1 level up.
2) Rename web to public_html make sure you rename it in some files too (config/bootstrap comes to mind).
Yes you can do it with .htaccess but you should not have the files exposed to the internet, just your web folder should be exposed so I am not giving you that solution because it is not a good one.
The Working Solution is Here!
Step 1. in yii2 basic application directory create an index.php file and fill it as below
<?php
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';
$config = require __DIR__ . '/config/web.php';
(new yii\web\Application($config))->run();
Step 2. Create a .htaccess file in the same base directory as follows
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php
I have a web directory structure like so:
root
/content
/plugins
/myplugin
/Slim (folder containing Slim Framework)
index.php
/other_folder_1
/other_folder_2
.htaccess
index.html
I'm interested in what to specify in my .htaccess file in order to refer to a directory that isn't actually present on the server, but actually point to the Slim app in the /myplugin directory.
Here are a few example URLs, which I'd like users (or myself) to be able to use in the browser's location bar, or link with in documents:
1. http://example.com/nonexistent_dir
2. http://example.com/nonexistent_dir/info
3. http://example.com/nonexistent_dir/info/details
I'm trying to rewrite these URLs to the following:
1. http://example.com/content/plugins/myplugin/index.php
2. http://example.com/content/plugins/myplugin/index.php/info
3. http://example.com/content/plugins/myplugin/index.php/info/details
...which would all actually be handled by the index.php Slim Framework app in the /myplugin directory. It's important the apparent URLs remain as they appear in the first example, without being changed in the location bar.
Here's what is currently in the .htaccess file in the root directory:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/schedule [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /content/plugins/myplugin/index.php [QSA,NC,L]
</IfModule>
This redirects all 3 of the test examples to http://example.com/nonexistent_dir, using the / route. So my thought is that I should be capturing everything after the nonexistent_dir, whether it be something or nothing, and appending it to the end of the RewriteRule somehow. But I don't understand how.
I realize that using parentheses around an expression will enable me to use the contents as a variable, referred to it with $1 (or $2, $3... for multiples), but I don't know how to apply it to this solution.
Any help will be most appreciated.
Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^nonexistent_dir(/.*)?$ /content/plugins/myplugin/index.php$1 [L]
Slim actually discards the base directory, and sets $env['PATH_INFO'], taking the content of this variable to match against the specified routes.
For example, lets take a /sub/index.php (Slim index file) and this rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^somedir(/.*)?$ /sub/index.php$1 [L]
...and this route specification:
$app->route('/info', function() use ($app) { ... });
So, with a GET request to /somedir/info, Slim strips /somedir from REQUEST_URI and sets $env['PATH_INFO'] with value /info (this is actually done in the constructor of \Slim\Environment class).
Later, the Router class will match /info and execute the closure function.
If you want to pass parameters via url, the route would be, for example:
$app->get('/info/:param', function($param) use ($app){ ... })