.htaccess is not working in my PHP MVC Framework - .htaccess

I am building my own PHP MVC framework from scratch on ubuntu. .htaccess file is not properly working in it. The folder structure and the .htaccess file content are as bellow.
Folder Structure
/MyMVCProject
/controllers
index.php
help.php
/libs
/modles
/views
.htaccess
index.php
.htaccess File content
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
When I type localh0st/MyMVCProject/help it gives me the following error.
Not Found
The requested URL /MyMVCProject/help was not found on this server.
Apache/2.2.22 (Ubuntu) Server at localh0st Port 80
And I'm sure I have enabled mod_rewrite.

Try to do it this way and from $_REQUEST array you can get url parameter and handle it in your index.php file.
Also for testing purpose add
echo <pre>;
print_r($_REQUEST);
echo </pre>;
at first lines of index.php
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^(.*)$ /$1 [R=301,L]
</ifModule>

Try following RewriteRule (Copied and bit modified from Zend framework)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,NC,L]

I encountered the same thing, in my case the error was produced when I had a help.php file in the same directory. Don't know exactly why this happens...

You need to add:
RewriteBase /MyMVCProject
to your .htaccess file.

Related

yii2 404 page not found when trying remove /web from url with .htaccess

Im'm trying to remove this /web from the url in Yii2 basic temeplate with .htaccess files. And i found a solution for it already but a problem has occured!
So the solution i have found earlier is:
1)Put in root/web folder .htaccess:
RewriteEngine On RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
2)Put in root folder .htaccess:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ web/css/$1 [L]
RewriteRule ^js/(.*)$ web/js/$1 [L]
RewriteRule ^images/(.*)$ web/images/$1 [L]
RewriteRule (.*) /web/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php
3)Add in config/web.php in request part 'baseUrl'=> ''
The problem occured after steps 2 and 3 are made. Browser tells:
Not Found
The requested URL /web/index.php was not found on this server.
Apache/2.4.23 (Win64) PHP/5.6.25 Server at localhost Port 80
All this happens when I'm doing it on my local server wich is WAMP. I tried to place this project on free hosting and evrything seem to work there. So how can I fix this problem on my local server? Why does it give me the 404 error?
Try in the root .htaccess replace
RewriteRule (.*) /web/$1
on
RewriteRule ^(.*)$ /web/$1 [L,PT]

.htaccess to access the file but to ignore non existing directory in URL path for that file

I have my htaccess and it already does couple of good things (it hides www and .php in my url) but I also need to add one additional feature to it.
I have the main domain called: wdd.ie
I have a root folder called: wdd.ie
Most important files are kept in the root.
I have also the following file in the root: about.php
I love to access about.php file by calling non existing directory: www.wdd.ie/myweb/
And my full URL path looks like this: www.wdd.ie/myweb/about
I would like to access the file (stored in the root) to allow non existing directory in url but to ignore it
Could anyone help!? Please, please help!
As this new desired htaccess feature complexity is now probably beyond my skills.
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,QSA]
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
If I've understood correctly you want the following url www.wdd.ie/myweb/about to offer up content from www.wdd.ie/about.php but keep the url the same?
If so, then the following should work
RewriteRule ^myweb/about$ /about.php [L]

HTTPS and REST API with .htaccess

Firstly I implemented a rewrite rule for my REST API on an apache2 server with the following .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?rquest=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
</IfModule>
It worked fine. If I entered e.g. http://mypage.de/REST/SomeRule/12 my index.php in the folder REST/ received the information SomeRule/12.
Now I enabled SSL on my server and I wanted to force the API to use https so I added the following code to my .htaccess file:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L]
Now I receive a page not found error. Can you see the error in the .htaccess file, or must it be somewhere else? I am not that familiar with these files. To create my API I used this tutorial.
With the help of anubhava I found the solution.
When I updated my server to run with https my folder sites-enabled included a new file sites-enabled/default-ssl. In this file I had to set
AllowOverride All
like I already did for the on using http. I addition the correct solution for the .htaccess file was the following:
<IfModule mod_rewrite.c>
RewriteEngine On
#use https
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/%{REQUEST_URI} [L]
#used for api
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?rquest=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
</IfModule>
I was not able to use
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L]
because the $1 then was somehow overwritten for the next Rule.

htaccess file misconfigured

I am using codeigniter php framework. I am trying to access a folder under public_html, but I can't get to it. It shows 404 custom page by codeigniter. Does the following script in .htaccess has something to do with it?
RewriteEngine on
RewriteCond $1 !^(index\.php|js|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Thanks
It probably is, you can try adding either some conditions:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|js|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Or include your folder as part of the exclusion (example, your folder is "foobar"):
RewriteEngine on
RewriteCond $1 !^(index\.php|js|images|robots\.txt|foobar)
RewriteRule ^(.*)$ /index.php/$1 [L]

Using htaccess to remove codeigniter index.php in a subdirectory

I'm trying to remove the index.php from my CI urls, but having followed the instructions it doesn't seem to be working.
I have a codeigniter installation running in a subdirectory. There is a default controller set in the routes config called main, and another controller called 'create'. The default runs fine, but going to /create returns a 324 error saying No Data Received. My htaccess looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
This should be enough:
<IfModule mod_rewrite.c>
RewriteEngine On
#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
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
This is what I use. My CodeIgniter also runs in a subdir.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|swf|uploads|js|css|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php/$1 [L]
.htaccess add:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Go into application/config/config.php and clear the index page config value:
// Old
$config['index_page'] = "index.php";
// New
$config['index_page'] = "";
Solved My problem hope others too... :)
Sometimes you need to enable it the rewrite module, run "apache2 enable module rewrite":
sudo a2enmod rewrite
You need to restart the webserver to apply the changes:
sudo service apache2 restart
Then folow Sofia's answer.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
for ubuntu

Resources