Codeigniter 4: Cannot access the page with apache mode_rewrite - php

I installed Codeigniter 4.0.0.rc3 on my local computer using Wampserver and created a 'people' controller. I can access the page using this address:
http://127.0.0.1/election/index.php/people
But not without 'index.php':
http://127.0.0.1/election/people
And this is the error:
The requested URL /Projects/Web/election/public/index.php/people was not found on this server.
.htaccess is in the public directory and is working because the error shows index.php in the path.
This is my htaccess:
# Disable directory browsing
Options All -Indexes
# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------
# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# If you installed CodeIgniter in a subfolder, you will need to
# change the following line to match the subfolder you need.
# http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
# RewriteBase /
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,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 the front controller, index.php
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# Ensure Authorization header is passed along
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 index.php
</IfModule>
# Disable server signature start
ServerSignature Off
# Disable server signature end
UPDATE: This htacces is working perfectly on a Linux Host but not working with Wampserver on Windows 10

In linux apache2 the following works, should be applicable to wampp's apache as well:
Make sure mode_rewrite is enabled
Edit your public .htaccess file as follows:
RewriteRule ^(.*)$ index.php/$1 [L]
to
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
(would appreciate if someone explains the QSA statement)
In app/Config/App.php edit:
public $indexPage = 'index.php';
to
public $indexPage = '';

I had to play around a lot before I figured it out, but I had installed CodeIgniter in a subfolder. (I access the website via http://127.0.0.1/myapp/).
I had configured the $baseURL parameter in the config file, but I kept getting that apache error. Just like you, if I added the /index.php in the URL, all my routes were working fine.
In /public/.htaccess, The QSA option didn't help, but this section just above did:
# If you installed CodeIgniter in a subfolder, you will need to
# change the following line to match the subfolder you need.
# http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
RewriteBase /myapp/
Adding this allow mod_rewrite to work fine. Obviously you need to change /myappp/ to what your virtual subfolder is, in order for it to match the subfolder portion of the $baseURL parameter in Config/App.php
Hope this help !

Related

Redirect to parent domain, if I put / at the end of URL in Codeigniter 4

Route redirect to parent domain when I put / at the end of the URL
For example:
My base URL is: http://localhost/pep_digital/.
My URL is http://localhost/pep_digital/dashboard
When I put trailing slash / at the end like http://localhost/pep_digital/dashboard/
It redirects me to http://localhost
After visiting this link http://localhost/pep_digital/dashboard/test/
It redirects me to http://localhost/dashboard/test
And https://localhost/pep_digital/dashboard/test/dashboard/test/
It redirects me to https://localhost/dashboard/test/dashboard/test
It removing the 1st segment from the URL.
And the following routes I tried for all the issue is the same.
$routes->group('dashboard', function ($routes) {
// Other routes
});
or
$routes->match(['get'],'dashboard', 'Dashboard::index');
or
$routes->get('dashboard', 'Dashboard::index');
My .htaccess
# Protect .env
<Files .env>
Order Allow,Deny
Deny from all
</Files>
# Disable directory browsing
Options All -Indexes
# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------
# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# If you installed CodeIgniter in a subfolder, you will need to
# change the following line to match the subfolder you need.
# http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
# RewriteBase /
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,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 the front controller, index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]
# Ensure Authorization header is passed along
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 index.php
</IfModule>
# Disable server signature start
ServerSignature Off
# Disable server signature end
Update
Well, I have updated my .htaccess to the latest updated branch, https://github.com/codeigniter4/CodeIgniter4/blob/develop/public/.htaccess
And then clear my cookies and cache and tried again and my issue got fixed.
Clearing cache is because the redirect was set to R=301.

Symfony 4 htaccess

Ok, i fixed all the problem, configured the server on localhost, but i want to hide the /public directory and want to display the content in the root folder of my document for example /forum/ and for logging /forum/login instead of going to forum/public/login. Here is the htaccess file but it dont work, because it gives No routes for GET /forum/.
RewriteEngine On
RewriteBase /forum/
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
What im doing wrong ?
Actually sometimes we may need to add .htaccess file
We can use this code as .htaccess file. I have checked this on Ubuntu VPS and also on Cpanel.
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /index.php/
</IfModule>
</IfModule>
My root .htaccess looks like this, I think you can put it onto your symfony root folder.
# Removes index.php from ExpressionEngine URLs
RewriteEngine on
# RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*) /public/$1 [L]
Then my .htaccess inside the public/ folder:
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the index.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
# Redirect to URI without front controller to prevent duplicate content
# (with and without `/index.php`). Only do this redirect on the initial
# rewrite by Apache and not on subsequent cycles. Otherwise we would get an
# endless redirect loop (request -> rewrite to front controller ->
# redirect -> request -> ...).
# So in case you get a "too many redirects" error or you always get redirected
# to the start page because your Apache does not expose the REDIRECT_STATUS
# environment variable, you have 2 choices:
# - disable this feature by commenting the following 2 lines or
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
# following RewriteCond (best solution)
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
# Rewrite all other queries to the front controller.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]

What is correct apache rewrite rule ( in .htaccess ) for coexist CodeIgniter and PhpBB3.1.X?

First i have my application run with CodeIgniter in directory /
Secondly I want install PhpBB forum in my subdir /forum/
But actually I have error 404 in /forum/install/app.php/install, i think it's due apache rewrite rules.
.htaccess :
In dir / ( for CodeIgniter ) :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^/(index\.php|assets|forum|images|cache)
RewriteRule . /index.php [L]
</IfModule>
And in subdir /forum/ ( for phpBB )
<IfModule mod_rewrite.c>
#RewriteEngine on
#
# Uncomment the statement below if URL rewriting doesn't
# work properly. If you installed phpBB in a subdirectory
# of your site, properly set the argument for the statement.
# e.g.: if your domain is test.com and you installed phpBB
# in http://www.test.com/phpBB/index.php you have to set
# the statement RewriteBase /phpBB/
#
RewriteBase /forum/
#
# Uncomment the statement below if you want to make use of
# HTTP authentication and it does not already work.
# This could be required if you are for example using PHP via Apache CGI.
#
#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
#
# The following 3 lines will rewrite URLs passed through the front controller
# to not require app.php in the actual URL. In other words, a controller is
# by default accessed at /app.php/my/controller, but can also be accessed at
# /my/controller
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ app.php [QSA,L]
#
# If symbolic links are not already being followed,
# uncomment the line below.
# http://anothersysadmin.wordpress.com/2008/06/10/mod_rewrite-forbidden-403-with-apache-228/
#
#Options +FollowSymLinks
</IfModule>
So «What is correct apache rewrite rule ( in .htaccess ) for coexist CodeIgniter and PhpBB3.1.X ?»
Thank you in advance for your answer.

Getting 404 error when attempting to use Apache rewrite on dokuwiki

I am building a wiki using dokuwiki, and am using the nice URL feature which uses url rewriting through Apache on the webserver, to get rid of the ugly php urls. My problem is that I seem to be only getting 404 Not Found errors when searching for the pages using the new URLs.
This is the .htaccess file provided, and the RewriteBase is pointed to the correct location.
#Options -Indexes -MultiViews +FollowSymLinks
## make sure nobody gets the htaccess, README, COPYING or VERSION files
<Files ~ "^([\._]ht|README$|VERSION$|COPYING$)">
Order allow,deny
Deny from all
</Files>
## Uncomment these rules if you want to have nice URLs using
## $conf['userewrite'] = 1 - not needed for rewrite mode 2
RewriteEngine on
RewriteRule ^_media/(.*) lib/exe/fetch.php?media=$1 [QSA,L]
RewriteRule ^_detail/(.*) lib/exe/detail.php?media=$1 [QSA,L]
RewriteRule ^_export/([^/]+)/(.*) doku.php?do=export_$1&id=$2 [QSA,L]
RewriteRule ^$ doku.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) doku.php?id=$1 [QSA,L]
RewriteRule ^index.php$ doku.php
## Not all installations will require the following line. If you do,
## change "/dokuwiki" to the path to your dokuwiki directory relative
## to your document root.
RewriteBase /dokuwiki
## If you enable DokuWikis XML-RPC interface, you should consider to
## restrict access to it over HTTPS only! Uncomment the following two
## rules if your server setup allows HTTPS.
#RewriteCond %{HTTPS} !=on
#RewriteRule ^lib/exe/xmlrpc.php$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
Make sure your Apache actually loads .htaccess files. You will probably need this in your Apache config:
AllowOverride All

How can I rewrite URL's of a certain directory?

Ok, vague question title, I know. Wasn't sure how to summarize this in one line. I have two applications in my CodeIgniter installation: frontend and admin. There are two front controllers for each that set to the correct application folder. Currently, I use index.php/home for the home page of the frontend and admin.php/home for the home page of the admin panel.
I finally got around to removing index.php from the URL of the frontend. For the admin cp, I would like to use example.com/admin instead of example.com/admin.php. So I basically need to rewrite any uri that has admin as the first segment to admin.php. I figured that adding RewriteRule ^admin/(.*)$ admin.php/$1 to my htaccess would do the trick, but apparently it doesn't... Not completely anyway. I get 404 Page Not Found for every single page in my admin cp. What am I doing wrong?
# Deny OR Allow Folder Indexes.
# Since we disable access to PHP files you
# can leave this on without worries.
# OR better yet, create a .htaccess file in
# the dir you want to allow browsing and
# set it to +Indexes
Options -Indexes
Options +FollowSymLinks
# Set the default file for indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
# mod_rewrite rules
RewriteEngine on
# The RewriteBase of the system (if you are using this sytem in a sub-folder).
# RewriteBase /CodeIgniter_1.6.3/
# This will make the site only accessible without the "www."
# (which will keep the subdomain-sensive config file happy)
# If you want the site to be accessed WITH the "www."
# comment-out the following two lines.
# RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
# RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
# If a controler can't be found - then issue a 404 error from PHP
# Error messages (via the "error" plugin)
# ErrorDocument 403 /index.php/403/
# ErrorDocument 404 /index.php/404/
# ErrorDocument 500 /index.php/500/
# Deny any people (or bots) from the following sites: (to stop spam comments)
# RewriteCond %{HTTP_REFERER} nienschanz\.ru [NC,OR]
# RewriteCond %{HTTP_REFERER} porn\.com
# RewriteRule .* - [F]
# Note: if you are having trouble from a certain URL just
# add it above to forbide all visitors from that site.
# You can also uncomment this if you know the IP:
# Deny from 192.168.1.1
# If the file is NOT the index.php file
RewriteCond %{REQUEST_FILENAME} !index.php
# Hide all PHP files so none can be accessed by HTTP
RewriteRule (.*)\.php$ index.php/$1
# If the file/dir is NOT real go to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
RewriteRule ^admin/(.*)$ admin.php/$1
</IfModule>
# If Mod_ewrite is NOT installed go to index.php
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
You need to add your rule before you index.php routing rule. It's because ^(.*)$ is matching admin/whatever, so your admin rule will never get executed. You should also add -Multiviews to your options so it looks like this:
Options +FollowSymLinks -Multiviews
Then right under RewriteEngine On, add your rule:
RewriteRule ^admin/(.*)$ admin.php/$1 [L]

Categories