Codeigniter front controller location and proper .htaccess - php

How should my .htaccess code look like if I move my front controller? More specific, I had front controller in the following directory: root/public/index.php and my .htaccess looked like so:
RewriteEngine on
RewriteBase /
ReWriteCond %{REQUEST_URI} !public/
ReWriteRule ^(.*)$ public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]
Now when I move front controller to: root/en/index.php obviously my .htaccess becomes obosolete. Also I think I must change the path in my front controller? But lets fix the .htaccess first to work with the new structure.

Should be just a matter of rewriting:
ReWriteCond %{REQUEST_URI} !public/
ReWriteRule ^(.*)$ public/$1 [L]
To:
ReWriteCond %{REQUEST_URI} !en/
ReWriteRule ^(.*)$ en/$1 [L]

Related

htaccess - insert script name into all URL requests

I have an application which requires an htaccess file that contains the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(tileserver\.php)
RewriteRule ^(.*)$ tileserver.php?/$1 [L,QSA]
But I am getting 404 errors matching the following patterns:
http://107.170.120.88/tileserver/extra5/11/331/794.png
and:
http://107.170.120.88/tileserver/extra5/11/331/796.grid.json
It seems that any URL with the path:
tileserver/extra5/
needs to be instead:
tileserver/tileserver.php/extra5/
but I haven't had much luck writing an htaccess file to fix this.
If I take out the
RewriteCond $1 !^(tileserver\.php)
RewriteRule ^(.*)$ tileserver.php?/$1 [L,QSA]
the base script doesn't work, but then if I add something like:
RewriteRule ^tileserver/extra5/(.*)$ tileserver/tileserver.php/extra5/$1
it has no effect. How do I write this rule?
--- UPDATE ---
Here's the full htaccess:
DirectoryIndex tileserver.php
RewriteEngine on
RewriteBase /tileserver/
<FilesMatch "\.mbtiles$">
Order Allow,Deny
Deny from all
</FilesMatch>
RewriteRule ^(.+).jpeg$ $1.jpg [L]
RewriteRule .* - [E=HTTP_IF_MODIFIED_SINCE:%{HTTP:If-Modified-Since}]
RewriteRule .* - [E=HTTP_IF_NONE_MATCH:%{HTTP:If-None-Match}]
RewriteRule ^tileserver.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ tileserver.php?/$1 [L,QSA]
Note that it is sitting in a subdirectory, in which the parent (root) has this htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Which removes index.php from all URLs (for codeigniter). Maybe that's conflicting?
With the datastructure like this:
example.com/tileserver/tileserver.php
and the url for the matching pattern
107.170.120.88/tileserver/extra5/11/331/794.png
which should result in 107.170.120.88/tileserver/tileserver.php?/extra5/11/331/794.png
your .htaccess should look something like this:
#turn on the rewrite engine
RewriteEngine On
#use the as base for your rewrite conditions the following folder
RewriteBase /tileserver
#since we don't want to rewrite the requests pointing directly to this script, leave it
RewriteRule ^tileserver.php - [L]
#if its not a file or a folder rewrite everything to tileserver.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#everything that comes after /tileserver is passed as argument to the php script
RewriteRule ^(.*)$ tileserver.php?/$1 [L,QSA]
I suspect the problem might be the [L] flag.
Instead, try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(tileserver\.php)
RewriteRule ^(.*)$ tileserver.php?/$1 [QSA]
RewriteRule ^tileserver/extra5/(.*) tileserver/tileserver.php/extra5/$1 [L,QSA]

How to rename part of a url with .htaccess?

I currently have very little Apache experience, and am having difficulties with my .htaccess file. My question is this: how can I rename these files, listed below, properly? I believe my syntax is accurate, according to http://www.htaccesscheck.com, but when accessing these pages, either A: the page won't load due to a redirect loop, or B: the page won't load, but will redirect to the wrong page. Here is my current .htaccess file for this directory:
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
Any help is much appreciated.
Try code below:
RewriteEngine on
RewriteBase /photos/
RewriteRule ^([0-9]{1,2})-([0-9]{4})$ archives.php?month=$1&year=$2 [L]
RewriteRule ^([0-9]+)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
This rules will check, if:
request like yourdomain/11-1111, then return archives.php
request like yourdomain/111, then return catpost.php (you can type
any number)
else will return viewpost
You have some errors in your current .htaccess, because your second rule get result of first rule.
By the way, you can use http://htaccess.madewithlove.be/ to check step by step what is posted to your rewrite rules.
be sure to write a valid pattern
try this
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*?)-(.*?)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*?)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*?)$ viewpost.php?id=$1 [QSA,L]

.htaccess rewrite with codeigniter

I've got a codeigniter setup where all URLs redirect to index.php, like below.
I need /api/index.php to go to /api, but NOT through a redirect.
My code below matches api/index.php but it is still not working.
/index.php/api works fine, and goes to the correct URL, however /api/index.php just goes to a 404 error page... Any suggestions? Am I being silly?
RewriteEngine on
RewriteBase /
RewriteRule ^api/index.php$ /index.php/api [L]
RewriteCond $1 !^(index\.php|images|robots\.txt|assets)
RewriteRule ^(.*)$ /index.php/$1 [L]
Probably because it is looking for a file named /api/index.php
Add these Rewrite Conditions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
//Your rewrite rule here
Try this:
RewriteEngine on
RewriteCond $1 !^(index\.php|img|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

remove index.php with alternative routing in codeigniter

I'm working on a project in Codeigniter that doesn't use the standard routing.php file.
It calls the functions like this: test.vi/index.php/controller/function.
My goal is to remove the index.php from the url with a htaccess rewrite:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
And also this config.php change:
$config['index_page'] = '';
My question is do I need to route all the controllers/functions in my route.php or can it be done without using the route.php file?
first make sure you enabled mod_rewrite in httpd.conf:
to do so add/make sure it exsits this line
LoadModule rewrite_module modules/mod_rewrite.so
then create .htaccess in your codeigniter root folder (next to index.php file) not application folder.
.htaccess
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|img|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
codeigniter take care of all routings for you; so for
controlers/welcome.php method=hello u can access it by
http://localhost/welcome/hello
its not like laravel you need to specifically add route; codeigniter automatically Fitch controller for you. unless you have it stored in a different location or inside subfolder then you dont need to touch route.php except for setting your default route.
Here's my redirect in .htaccess for a live codeigniter site. The second part exempts certain directories.
#Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond $1 !^(index\.php|phpinfo\.php|test\.php|example\.php|images|i|css|js|beta|captcha|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
THIS works perfect for me... give it a try..
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
try this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test.v/index.php?/$1 [L]
I guess everyone forget about add test.vi folder name on .htaccess

CodeIgniter Routing

I'm trying to build a short URI service with CI just so I can learn CI faster
anyway .. I got stuck at the routing
i hid the index.php then added the following route $route['([A-z0-9]{4})'] = "/forward/redirect/$1";
but it just shows my default controller
I also tried with HTaccess
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ /forward/redirect/$1 [NC]
It gives an error for not having any passed data
any help is appreciated.
Cheers
Since there is no physical path /forward/redirect/, you should redirect to the "catch all" index.php file in the root and input the path as parameter:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ /index.php/forward/redirect/$1 [NC]
Or you can leave the rule as is and append another rule (this way you will have two rewriting cycles, the first one will rewrite to /forward/redirect/asdf and then the second one rewrites to index.php/forward/redirect/asdf:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ /forward/redirect/$1 [NC]
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
i finally got it working >_>
routes file contains this
$route['([A-z0-9]{4})'] = "/forward/redirect/$1";
htaccess contains this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ index.php/forward/redirect/?$1 [NC]
RewriteCond $1 !^(index\.php|images|robots\.txt|(.*).js|(.*).css|(.*).jpg|(.*).png)
//added (.*) so resources could be loaded properly :)
RewriteRule ^(.*)$ /index.php/$1 [L]

Categories