Pretty URL's with CodeIgniter - codeigniter

I'm fairly new to CI and have been trying out how to produce clean URL's. I have accomplished this task before without using a framework by editing my .htaccess file as follows.
RewriteCond %{REQUEST_URI} !^/(css|js|img)/
RewriteRule ^profile/([^/]*)$ profile.php?id=$1 [L]
With CI, I have tried the following:
#Get rid of the index.php that's in the URL by default
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# Profile page
RewriteCond %{REQUEST_URI} !^/(css|js|img)/
RewriteRule ^profile/([^/]*)$ profile?id=$1 [L]
I know that by default, the value after the name of the controller in the URL (in this case, the Profile controller), will invoke the function with the same name inside the controller class. But, if there is no value in the URL specified after the controller, by default, the index function will be invoked. I plan on leaving the function name blank so that the index function will be invoked by default. But, the rewrite rule isn't working.
Any ideas?

With .htaccess you can do like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# Profile page
RewriteCond %{REQUEST_URI} !^/(css|js|img)/
RewriteRule ^profile/([^/]*)$ profile/index/$1 [L]
In rewrite you have to mention the function name whether it is the index function or any other
Same as you can utilize the CI routing routes.php
$route['profile/(:any)'] = "profile/index/$1";
Now in index function of profile you can get the parameter
function index($id) {
echo $id;
echo $this->uri->segment(3);
//Both will result the same
}

Related

urlrewrite images to codeigniter controller method gives page not found error

I've a codeigniter site up and running with no issues. Now I need to rewrite some images to codeigniter controller/method.
This is my .htaccess file which works fine
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|media|static|public)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Now I need something like this to rewrite images to codeigniter. Rewrite not redirect
RewriteRule ^(media/somefolder/someimage.jpg)$ /some_controller/somemethod?param=value [L]
I added the following line (this is not codeigniter just a regular php file) just for testing to make sure RewriteRule works and it's working fine
RewriteRule ^(media/somefolder/someimage.jpg)$ public/myphpfile.php?ipath=$1 [L]
But if I try any of the following I get codeigniter's page not found error page.
RewriteRule ^(media/somefolder/someimage.jpg)$ /some_controller/somemethod/?ipath=$1 [L]
or
RewriteRule ^(media/somefolder/someimage.jpg)$ index.php?c=some_controller&m=somemethod&ipath=$1 [L]
What am i missing here?
To add exception to only a specific image
Add the media rule like this :
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(media/somefolder/someimage.jpg)$ /index.php?/some_controller/somemethod/?ipath=$1 [L]
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|media|static|public)
RewriteRule ^(.*)$ /index.php?/$1 [L]
To add exception to all images inside the media directory
Since you've filtered out to ignore the media directory on this rule :
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|media|static|public)
In order to rewrite the media path to a controller instead of ignoring it, remove the media rule from it :
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|static|public)
Add the media rule like this :
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
# removed the media form below rule
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|static|public)
RewriteRule ^(media/somefolder/someimage.jpg)$ /index.php?/some_controller/somemethod/?ipath=$1 [L]
RewriteRule ^(.*)$ /index.php?/$1 [L]

Laravel serving up pages from invalid route

I have the following route: Route::resource('users', 'ProfileController'); When I go to site.com/users/123, it loads the page properly. I can also go to site.com/index.php/users/123 and get the same page (not sure if laravel is by default intended to do that).
If I change the url to site.com/does_not_exist/users/123, laravel returns a 404, as you would expect. However, if I go to site.com/does_not_exist/index.php/users/123, laravel loads the page. I can put any random path, non-existent path between site.com and index.php and it will work.
Why doesn't laravel return a 404 for this? How do I fix it so it does?
Because the index.php is the index.php in public folder that handles the incoming request and its a automatically generated class loader
This might work (similar question on how to fix this)
1-Renaming the server.php to index.php (no modifications)
2-Copy the .htaccess from public (like rimon.ekjon said below)
3-Changing .htaccess it a bit as follows for statics:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
If there are any other static files needed just add the extension to the previous declared list
Source

Not able to access other controllers

I have worked on a website which was has two domain names.It got linked to one of them on one server.On trying the same on another server for a different domain the website gets linked and opens but it is just able to access the default controller.Anything I try other then the default controller it shows page not found.
there is issue with your .htacces file
just try
your.domain.com/index.php/contorller_name/function
if it works then replace your .htaccc file at main folder with this.
RewriteEngine On
RewriteBase /your_project_path
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|images|js|uploads|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

URL Change in Codeigniter framework

I have a URL
http://localhost/himalaya/webmanager/recentUpdate/index/edit-recent-update-1
but I want it to make the URL like this:
http://localhost/himalaya/webmanager/recentUpdate/edit-recent-update-1.
The controller name is recentUpdate and method name is index. My question is how can I remove index method name from URL in codeigniter framework?
Hi First you just read the what is index.php work in codeigniter.
https://ellislab.com/codeigniter/user-guide/general/urls.html
For remove the index then you write routing rule in .htaccess file.
above the application folder
RewriteEngine On
RewriteBase /project_name/
RewriteRule ^(.*)$ index.php?/$1 [L]
add .htaccess file in your project.
Add code in it.
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Domain name in URL as variable

My .htaccess file has the following code:
RewriteEngine on
RewriteRule ^/?(.*)$ index.php?domain=$1 [L]
I'm trying to get domain names as variables from URLs like:
hxxp://www.example.com/www.domain.name or
hxxp://www.example.com/subdomain.domain.name or
hxxp://www.example.com/domain.name
but with $_GET['domain'] my variable is always 'index.php' and not the domain names.
With hxxp://www.example.com/domain/www.domain.name and .htaccess code
RewriteEngine on
RewriteRule ^domain/?(.*)$ index.php?url=$1 [L]
everything is OK, but I would like to remove the 'domain/' part from the URLs.
I've searched for this, but couldn't find anything. Could someone please help me with this?
something like
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?domain=$1 [L]
in this case $1 will be:
http://site/www.example.com $1 = www.example.com
http://site/www.example.com/xyz $1 = www.example.com/xyz
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/.](\.[^/]+)+$ index.php?domain=$0 [L]
This will rewrite any request with a URL path that contains at least one dot (foo.bar, foo.bar.baz, etc.) to your index.php.

Resources