codeigniter htaccess with multiple applications - .htaccess

I am using codeigniter. I have front end application and backend application like
/system/
/application/
/front/
/admin/
index.php
admin.php
.htaccess
I want my url like http://example.com/news/article1 (for site)
http://example.com/news/admin (for admin)
In .htaccess I have written
RewriteEngine On
# If the user types just "admin".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin$ admin\.php [L,QSA]
# If the user enter in any admin section, like "admin/section".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin\/(.*)$ admin\.php/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
the front end is working fine but when I enter mydomain.com/admin it is throwing 404 not found error. Please help me.
Thanks and regards

I'm not that good with .htaccess, but from the CI website, you can use this for your .htaccess rule
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
and then use routes to rewrite your urls. Much easier than trying to do it in .htaccess files

Related

htaccess rewrite going to 404?

for the last hours I cant seem to figure out why my .htaccess file is redirecting to a blank url. Here is my htaccess file below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ /test/user-profile/%1? [R=301,L]
I am trying to rewrite www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4
It does rewrite the url, however then the page has "The requested URL was not found on this server." the htaccess file is in public_html folder, so I am trying to select a specific user inside www.exampleurl.com/test/userslist.php which would go to www.exampleurl.com/test/user-profile.php. It all works perfectly before using htaccess. www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4 but it just wont find the url or file? im a super noob folder path below -> public_html/test/userslist.php & user-profile.php
Just cant seem to figure out what to do. Yes I do have mod-rewrite on.
With your shown samples, please try following htaccess rules file.
This assumes that you are hitting link www.example/test/user-profile?id=4 in browser which is redirecting to www.example/test/user-profile/4 and is being served by index.php with parameters user-profile=user-profile in your url and id=digits in url. You can also change them as per your need.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /test/
RewriteCond %{THE_REQUEST} \s/(test/user-profile)\?id=(\d+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([*/]*)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]
2nd solution: In case someone is hitting url example.com/test/user-profile/7 then try following htaccess rules.
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([^/]*)/(\d+)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]

issue in removing index.php in codeigniter 3 while using redirect method

in codeigniter 3.1.3, while i'm using redirect('Admin','refresh') method from Login controller, in url it shows localhost/rsufadmin/index.php/Admin.... i have a htaccess file which contains
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
if i manually type localhost/rsufadmin/Admin , Admin controller work just fine... but i have to go to other controllers from a controller. So, how can i remove this index.php part from url??
i have some screenshot below to make it understand easier... while using redirect method from controller,.. while manually entering url
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|images|asset|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

I need to use htaccess for sever solutions

I have three directories
phone/
email/
profile/
In the phone directory I would have for example phone/555-555-1212.html
I would like to use htaccess to call my php file and search mysql
that page is /phone/template.php?a=555&b=555&c=1212
Email same thing
email/jay#gmail.com.html
php page /email/template.php?email=jay#gmail.com
Profile page would be
/profile/123456.html
to call /template/profile.php?id=123456
Please help
Thanks!!
Right now in my profile folder I am practicing and have this but if I do not have anything else after the profile/ it just pulls a blank page
I am a complete rookie to htaccess and am trying to learn
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9_-]+).html$ index.php?ter_id=$1 [QSA,L]
put this in the root's .htaccess:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#----- Email Rewrite
RewriteCond %{REQUEST_URI} email/([^/]+) [NC]
RewriteRule .* email/email_template.php?email=%1
#----- Profile Rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} profile/([^/]+) [NC]
RewriteRule .* profile/profile_template.php?profile=%1
#----- Phone Rewrite
RewriteCond %{REQUEST_URI} phone/([^/]+)-([^/]+)-([^/]+) [NC]
RewriteRule .* phone/phone_template.php?a=%1&b=%2&c=%3
P.S: DO NOT forget to sanitize the $_GET['foo'] in your pages and remove any unneeded symbol

How to redirect complete domain to a particular folder

i want to rewrite url using .htaccess file,
my all data is saved in work folder on server
and i want to show that folder when user enter the complete webaddress
www.example.com to www.example.com/work using mod_rewrite
my code is
RewriteRule ^(.*)$ /work/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
this code create problem when i enter somthing like www.example.com/folder/ than it externally redirect me to www.example.com/work/folder/ and shows 404 page not found even when work contains that folder and that folder contain index.php
Try this :
RewriteCond %{REQUEST_URI} !^/?work/
RewriteRule ^(.*)$ /work/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ $1.php [NC,L]

Code Igniter SubDirectory Question

I am trying to let the "trac" directory and all of it's subdirectories be accessible through the url http://www.domain.com/trac/
I am working with the codeginiter framework and my directory structure looks like
.htaccess
index.php
system
trac
I can access the abov url fine, but the problem is the scripts and other files contained in trac subdirectories ie: trac/chrome/common/css/trac.css are not accessible and 404. Here is my .htaccess code. Please help.
RewriteEngine On
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond $1 !^trac/
RewriteRule ^trac/(.*) /trac/$1
You don't even need to mention /trac/ in your .htaccess. That's EXACTLY the point of
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
You're setting two "Rewrite Conditions." The first one says, "As long as the request isn't a file." The second one says "OR As long as the request isn't a directory."
Then
RewriteRule ^(.*)$ index.php?/$1 [L]
Sends everything else to index.php, where CI takes over. And just for the record, my full .htaccess that I use on every project:
Options +FollowSymLinks
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]
If you're confused about the RewriteCond %{REQUEST_URL} ^system.* line, it only exists so that any browser requests to the /system/ folder is always routed to index.php, and therefor ignored.
rewrite rules are executed in order... so try this
RewriteCond %{REQUEST_URI} !^/trac/
RewriteCond $1 !(index\.php/)
RewriteRule ^(.*)$ index.php?/$1 [L]
Explanation:
if the uri DOES NOT start with trac
if the uri IS NOT index.php
rewrite url as index.php?/{rest of the url}
If you remove the last two lines, it should work.
RewriteCond %{REQUEST_FILENAME} !-f checks to ensure that you're not requesting a file. If you are, this condition fails and the rule RewriteRule ^(.*)$ index.php?/$1 [L] is never executed.

Resources