htaccess rewriteRule in variable subfolder in different servers - .htaccess

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]

Related

Two different projects (different index.php's) on same server

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.

Remove subfolder with htaccess

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.

Codeigniter URL rewriting .htaccess is not working on CentOS

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]

.htaccess variables REWRITING but not REDIRECTING

I've been looking for my answer for 2 days but since I haven't found it, I finally decided to put my question to you.
I'm using a Brinkster shared host and can access the .htaccess but no Apache configuration. Every folder on the server represents the content of one site (domain). In the root I have a .htaccess where every domain is controled with
RewriteCond %{HTTP:Host} ^(?:www\.)?domainname\.be$
RewriteRule (.*) /folder/$1 [NC,L,NS]
In the folder I have another .htacces file.
I want to rewrite and redirect for prettier URLs
http://www.domainname.com/pag.asp?name=var_name&id=var_id
should look like
http://www.domainname.com/name/
The rewrite bit works with the .htaccess code bellow.
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks -MultiViews
Options FollowSymLinks
AllowOverride All
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^name=([a-zA-Z-]+)&id=([0-9]+)$ [NC]
RewriteRule ^/?page\.asp$ /%1? [R=301,L]
</IfModule>
But for some reason www.domainname.com/name/ always gives a 404.
What am I doing wrong?
Any help wouldd be highly appreciated.
Thank you

Cannot remove index.php from CodeIgniter URL

My directory structure is /var/www/CI/ with all the folders viz., application, system under the folder CI. Have created a .htaccess file under CI.
Following is the code in .htacess.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Still localhost/CI/blog gives 404 error. Can anyone guide as to where this rule is wrong?
Please try the following
At first, make sure you set the config file like the following..
$config['index_page'] = '';
Also make sure that mod_rewrite is enabled in the httpd.conf file and after that, overwrite your .htaccess file which is located your project root folder not in the application folder with the following code..
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
Make sure in your application/config/config.php file this is set as follows:
$config['index_page'] = '';
Also do this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^CI/(index\.php|public|images|robots\.txt|css)
RewriteRule ^CI/(.*)$ CI/index.php/$1 [L]
</IfModule>
Typically you don't put CI in its own directory and put application and system in the root folder.
Check your Apache configuration so that it is allowing an override.
If the AllowOverride is set to None, the rewrite won't work for you.
Adding the following to your httpd.conf (NOT your .htaccess) should fix your problem
<Directory "/">
AllowOverride All
</Directory>
Let me know if it's still not working.
This works. Although, please make sure your mod_rewrite is enabled,
Find the httpd.conf file under the C:\wamp\bin\apache\apache2.4.9\conf folder inside the Apache’s installation folder.
Find the following line #LoadModule rewrite_module modules/mod_rewrite.so in the httpd.conf file.You can do this easily by searching the keyword “mod_rewrite”.
Remove the # at the starting of the line, # represents that line is commented.
Then restart the apache server.
You can see now mod_rewrite in the Loaded Module section while doing phpinfo().
Use the following steps,
1. Create .htaccess file in the document root [myroot_folder/.htaccess].
2. open the config file in application/config/config.php and do the following
$config['index_page'] = '';
remove index.php from your base url
if it is $config['base_url']= 'http://localhost/myroot_folder/index.php';
change it to $config['base_url']= 'http://localhost/myroot_folder/';
Now open the .htaccess file and add the following code block
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
1) Make .htaccess file and put this code.
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
2) Change:
$config['index_page'] = '';
remove index.php present in config.php file
3) Rewrite on from your server
If you have not worked all but that you did so, try this:
Change AllowOverride None to AllowOverride All in the virtual host file in /etc/apache2/sites-available/default
Details here

Resources