I'm trying to run two different php frameworks on the same server. The framework to use is determined by the first path in the uri.
So for example:
http://www.example.com/v1/requestname
will go to the index.php located in:
/var/www/html/api/api
And everything else (http://www.example.com/v2/requestname) will go to the index.php located in:
/var/www/html/api2/public
Here is my apache 2.2 config:
Alias /v1 /var/www/html/api/api
<Directory /var/www/html/api/api>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</Directory>
DocumentRoot "/var/www/html/api2/public"
<Directory "/var/www/html/api2/public">
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</Directory>
I've been attempting to do this with apache, but every request is being routed to api2. Everything I'm looking seems to say this should work.
So here's the solution I came up with for anyone in the future:
Set anything beginning with /v1 to be send to the first version of the API.
Alias /v1 /var/www/html/api/api/index.php
Send everything else to the second version:
DocumentRoot "/var/www/html/api2/public"
<Directory "/var/www/html/api2/public">
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</Directory>
The trick was adding index.php onto the end of the Alias.
Related
I am building an API, this will be stored in different folders in different servers and will have different versions, for example, I would like to turn this url:
http://aserver.com/api/v3/something
into this:
http://aserver.com/api/v3/index.php?endpoint=something&version=3
And this url:
http://leserver4.com/afolder/api/v6/someone
into this:
http://leserver4.com/afolder/api/v6/index.php?endpoint=someone&version=6
As you can see, the only common delimiter is this string: "/api/v"
I want be able to handle this in any folder and send the version number, so I made this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/v(\d+)/([^.]+)$ api/v$1/index.php?endpoint=$2&version=$1 [QSA,L,NC] # by anubhava
But is not working, it shows error 404
I configured apache2.conf this way:
<Directory "/var/www/*/api">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
what am I doing wrong?
I edited the question to include the configuration file and the whole .htaccess file
Create a new .htaccess inside api/ directory with this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^v(\d+)/([^.]+)$ v$1/index.php?endpoint=$2&version=$1 [QSA,L,NC]
This is the architecture of my website :
/
app/
index.php
...
libs/
lib1/
file.php
lib2/
...
I need to access index.php by this url : domain.com/index.php
I tried this :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)app
RewriteRule ^(.*)$ app/$1 [L]
It works, but inside my index.php, I call for example :
include('../libs/lib1/file.php');
It's doesn't work because this path refer to root now...
And I can't access to domain.com/libs anymore, because it's looking for domain.com/app/libs.
How can I do ?
The include() shouldn't care what the path the browser sees. That should be based on the local filesystem on the server. But your rules are affecting direct access to the libs, so try adding a few more conditions:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^app/
RewriteRule ^(.+)$ app/$1 [L]
RewriteRule ^$ app/index.php [L]
This makes it so requests for existing files or content won't get routed through the app folder.
I've created a .htaccess file in my document root at /var/www/html/ to rewrite URL of Codeigniter to remove "index.php" from URL of all pages.
e.g. Change URL from
http://myhost/index.php/controller/function
to
http://myhost/controller/function`
Here is the code of my `/var/www/html/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I've got many suggestions from googling to enable mod-rewrite module, but I can see in my httpd.conf its already enabled.
LoadModule rewrite_module modules/mod_rewrite.so
This is perfectly working on my local system running on Debian 7 (Apache/2.4.4).
Any help would be appreciated. :)
You need to also specify the locations that can use it. For example, in /etc/httpd/conf/httpd.conf you should see something like:
<Directory "/var/www/html">
...lots of text...
</Directory>
Make sure is has:
<Directory "/var/www/html">
AllowOverride All
</Directory>
It's possible that your centos server isn't setup to handle PATH INFO. Try replacing the index.php/$1 in your rule's target to index.php?/$1.
Then you need to modify your CI config to enable query strings: http://ellislab.com/codeigniter/user-guide/general/urls.html
In you application/config.php file set $config['enable_query_strings'] to TRUE.
# CentOS Linux release 7.9.2009 (Core) -
# CodeIgniter 3.1.11 - ( .htaccess - This code work for me )
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I want to remove index.php from codeigniter url
my current configurations I tried so far with zero results are
folder structure:
xampp\htdocs\Codeigniter\application\controllers\olx\olx.php
xampp\htdocs\Codeigniter\application\views\olx\frmSignup
my HTACCESS file on the location
xampp\htdocs\Codeigniter.HTACCESS with the following contents nothing else in .HTACCESS file for me
RewriteEngine On
RewriteBase /olx
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
mod_rewrite is enabled checked using phpinfo();
following changes made to config.php file
$config['base_url'] = 'http://mypc/olx';
$config['index_page'] = '';
when try to access this url
http://mypc/codeigniter/olx/frmSignup i get 404 error
changes made to routes.php are
$route['default_controller'] = 'olx';
$route['olx/frmSignup'] = 'olx/frmSignup';
please help I want to access my app without index.php. i am working on localhost with Windows7
Change AllowOverride None to AllowOverride All in my virtual host file at /etc/apache2/sites-available/000-default.conf in ubuntu
Change AllowOverride None to AllowOverride All in my virtual host file at /etc/apache2/sites-available/default.conf in windows
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride All <---- replace None with All
</Directory>
<Directory /var/www >
Options Indexes FollowSymLinks MultiViews
AllowOverride All <--- replace None with All
Order allow,deny
allow from all
</Directory>
....
Then put this code in .htaccess file in root folder in codeigniter
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
It will definately work.
From http://ellislab.com/codeigniter%20/user-guide/general/urls.html - Try
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
after all said by friends, you have to do another thing (I think you are doing it on localhost):
First, double check the suggestions:
then go to windows tray, right click on WAMP icon, and then click on it , go to Apache menu, and then to modules and then check for rewrite_module to see if it is checked or not, and click on it if you find it unchecked (means disabled) to enable it. Now try again and report it...
You just have to create .htaccess file on the root application folder or edit it if you already have. The following .htaccess file works for me.
.htaccess
# BEGIN ci
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Codeigniter
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
so not olx as it's a controller and not a folder
in config
$config['base_url'] = '';
$config['index_page'] = '';
in route
$route['default_controller'] = "olx";
and move your
D:\xampp\htdocs\Codeigniter\application\controllers\olx\olx.php
to
D:\xampp\htdocs\Codeigniter\application\controllers\olx.php
and olx should look like
class olx extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
echo "test";
// or $this->load->view('olx/overview.php', $data);
}
}
and try localhost/codeigniter/ or localhost/codeigniter/olx
I am using ExpressionEngine and want to remove index.php from my URL's. I have this .htaccess file saved in the root folder. It works perfectly on localhost but when I upload it to the server it doesn't work. The correct URL appears in the address bar but the page stays on homepage. Any tips?
<IfModule mod_rewrite.c>
# Enable Rewrite Engine
# ------------------------------
RewriteEngine On
RewriteBase /
# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteCond %{THE_REQUEST} ^GET
RewriteRule ^index\.php(.+) $1 [R=301,L]
# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Make sure your AllowOverride Directive in Apache is configured to allow .htaccess files and your server has mod_rewrite installed and active.
On Mac OS X, you'll find this file at /etc/apache2/httpd.conf. Look for the <Directory> Directive and change it to be:
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
</Directory>
You'll need to restart Apache so it can read the new configuration:
sudo /usr/sbin/apachectl restart
If you'd prefer to use a GUI to restart Apache, go to Apple > System Preferences > Sharing and toggle the checkbox next to the Web Sharing service.
If you're using Windows or any flavor of Linux, the same approach applies, but the Apache configuration may be in a different place, especially if you're using WAMP or MAMP.
Also, for reference, the "officially supported method" by EllisLab for Removing index.php from ExpressionEngine URLs is the following:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>