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]
Related
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.
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 !
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]
When users go to my sub domain, I want them to be redirected to a specific file I have in my subdomain. Or, to be exact, when someone goes here
http://app.example.com/
they should be redirected to here:
http://app.example.com/dashboard/
Everything I have tried doesn't seem to be working and the examples I have found online either don't work or don't do what I need them to do.
I'm also trying to get this to work from my root directory (from public_html). This is my file structue:
/ public_html
/ .htaccess
/ index.php
/ app.example.com
/ dashboard.php
You can see my htaccess file sitting on the top level. This is where I need to keep it.
This is my current file (including my attempt at the redirect):
# --------------------------------------------------------------------------------------------------------------------------------------
# Stops people from being able to see htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
# --------------------------------------------------------------------------------------------------------------------------------------
# Options
Options All -Indexes +MultiViews
# --------------------------------------------------------------------------------------------------------------------------------------
# Redirect app.example.com to /dashboard
RewriteEngine On
RewriteCond %{HTTP_HOST} ^app.example.com
RewriteRule ^$ /dashboard [L,R=301]
# --------------------------------------------------------------------------------------------------------------------------------------
# Makes it so url can have slash at the end instead of .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# --------------------------------------------------------------------------------------------------------------------------------------
# Adds wwww to url if it doesnt already have it
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
I've seen URL's like this around and I'm just wondering how it is they are used.
Until now I've been using www.mysite.com/users/?id=33
How can I use the other format?
Paul Peelan's answer is correct if a little verbose :-) Put this in your .htaccess file in the root of your site:
RewriteEngine On
RewriteRule ^users/(\d+)$ /users/?id=$1
This will match /users/33, /users/1, /users/12345 etc and redirect to /users/?id=12345.
This requires that your Apache configuration has the mod_rewrite engine enabled. See the mod_rewrite docs for further information.
You will have to use the "mod-rewrite" and e.g. an .htaccess file.
Apache will then send the urls according to your settings in the .htaccess file.
E.g.:
Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
# If your site can be accessed both with and without the 'www.' prefix, you
# can use one of the following settings to redirect users to your preferred
# URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
#
# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/... will be redirected to http://www.example.com/...)
# adapt and uncomment the following:
# RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
# RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
#
# To redirect all users to access the site WITHOUT the 'www.' prefix,
# (http://www.example.com/... will be redirected to http://example.com/...)
# uncomment and adapt the following:
# RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
# RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
# Modify the RewriteBase if you are using Drupal in a subdirectory or in a
# VirtualDocumentRoot and the rewrite rules are not working properly.
# For example if your site is at http://example.com/drupal uncomment and
# modify the following line:
# RewriteBase /drupal
#
# If your site is running in a VirtualDocumentRoot at http://example.com/,
# uncomment the following line:
# RewriteBase /
# Rewrite URLs of the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
Regards,
Paul
I know you have already selected an answer for this question, but this is quite useful to know. The PHP framework Codeigniter allows you to use URLs like this by default. I found it a lot easier to do URL rewriting this way. Further information about this, including code samples, can be found at http://codeigniter.com/user_guide/general/urls.html