I've created a folder api/ in my www/html folder.. I've put a .htaccess file in there that looks like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule api/(.*)$ api/api.php?request=$1 [QSA,NC,L]
</IfModule>
In the api/ folder I have the api.php file with this code for testing;
<?php
echo $_REQUEST['request'];
?>
If I go to myserver/api/test I get a 404 not found.. So in this case I would thing it should echo test?
I've also tried putting the file in the www/html file instead of inside the api folder since I specify the api folder in the .htaccess file..
I had to set AllowOverride All in my apache2.conf file
Reference this answer:
https://stackoverflow.com/a/15662459/7417114
Related
I have three folders--Model, Views and Controller--and I have stored the index.php file in the Views folder. I am not able to route the files. I tried the following code in the .htaccess file
RewiteEngine On
RewiteCond %{REQUEST_FILENAME} !-f
ReWriteRule ^(.*)$ index.php
Let's assume your project structure is something like below:
Model
Controller
Views -> index.php
You can use this .htaccess file in the root folder:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ view/ [L]
RewriteRule (.*) Views/$1 [L]
</IfModule>
This sends all the traffic to Views folder.
And another this .htaccess file inside the Views folder:
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
This sends all the traffic throw index.php file with a pretty URL.
Tips:
But I suggest changing your structure as below that seems to be better:
public -> index.php #will include bootstrap.php
assets
.htacess
app --->> Model
Controller
View
Helper
bootstrap.php #contains autoload and includes
...
.htaccess
So all traffic will pass throw public/index.php and the application folder and services are separate from public and assets.
In my website i have two files index.php and .htaccess. Both are located in directory public_html/image/public. The .htaccess file plays a role that i do not have to mention index.php in URL. Here's the code of .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
So www.example.com/image/public/ points to index.php.
Now if i move index.php to public_html without moving .htaccess, how can i make the same functionality again as it was before(means not have to mention index.php in URL). I guess i have to make some changes in .htaccess file but i cannot seem to make it work out.
Change RewriteRule line with this
RewriteRule . /index.php [L]
But if you just want to disable the listing of all images when someone hit the URL of an image path without imagename in your case it could be something like this www.example.com/image/public/
You can do with this, just change all content of your .htaccess file with below code
Options -Indexes
I am trying to build a custom mvc framework in php and its folder structure is something like this in my localhost.
Controllers(Dir)
Models(Dir)
Views(Dir)
Lib(Dir)
Config(Dir)
Lang(Dir)
Public
.htaccess
In this .htaccess file I am redirecting all the request in the public folder and the code in this .htaccess file is as follows
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
And the following is my public folder contents
css(Dir)
js(Dir)
images(Dir)
mod(Dir)
index.php
.htaccess
In this htaccess I am checking if the requested uri is not the folder or file in this folder then pass that request to index.php, which in turn does the url processing for controllers and actions etc. And its code is as follows
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %[REQUEST_FILENAME] !-f
RewriteCond %[REQUEST_FILENAME] !-d
RewriteRule ^(.*)$ index.php [PT,L]
</IfModule>
Everything is working fine except when I try to use any css file,js, image or any php file in mod directory(I am calling those file via ajax) it still pass that request to index.php which again try to locate them as a controller etc.
So please can anyone tell me what I am doing wrong and how can I solve this problem. I still don't fully understand Rewrite Rules and Conditions so its possible that I have done something wrong in my rules.
Thank you,
Anchal Bhargava
My htaccess is this :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([0-9a-zA-Z-]+)?$ show.php?id=$1 [L]
</IfModule>
So whenever browser get 'any thing' after root folder then it will go show.php and parameter will be 'any thing' .
Note: it will not effect if it have extension or a folder.
But there is index file in my root folder. So browser should take index file first. So that, i added this in .htaccess file :
DirectoryIndex index.php
But not working. So is there anything to do, to show at least index file whenever root folder visited ?
You can have your .htaccess like this:
DirectoryIndex index.php
RewriteEngine On
# request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
# request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z-]+)/?$ /show.php?id=$1 [L,QSA]
my site's directory is:
localhost/
includes/
includes/initialize.php
includes/user.php
includes/session.php
includes/database.php
public/
public/bootstrap/
public/css/
public/js/
public/dashboard/index.php
public/index.php
.htaccess(first htaccess file)
{
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/$1 [QSA,L]
</IfModule>
}
I'd like to have my url more neat, for example
when I type this: http://localhost I know it goes to http://localhost/public/
but the URL still is http://localhost
but the problem is when I type http://localhost/dashboard
unfortunately the URL will be http://localhost/public/dashboard/ on my browser.
I do not want to see the word public.
I think I should have another htaccess file under the public directory, but I do not know the suitable code for it.
Try this code in your DOCUMENT_ROOT/.htaccess file:
DirectoryIndex index.html index.php
RewriteEngine On
RewriteRule !^public/ /public%{REQUEST_URI} [NC,L]