How to change the url in codelginter - php

My url is http://mydomain.com/controllername/method/ .
Now I need the site url like.
http://mydomain.com/method.
mydomain name wants to access as a controller name .
for example. url like
www.testing.com/pages. 'testing' act as a controller in codeigniter.
Explain how to get the controller name from the domain name. and also how to remove the controller name(www.mydomain.com/controllername/method) from the url.
I need the url like www.controllername.com/method

In application/config/routes.php use this code
$route['method'] = 'controllername/method';
for routing.

Use Routes. Routes in codeigniter

You have to define routes in
application/config/routes.php.
http://ellislab.com/codeigniter/user-guide/general/routing.html
This may help.

IMO this would best be done with your .htaccess file instead of CI routes. I am not the best with these rules, so typo may exist, this is the general idea:
// If HOST contains www, strip www.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
// If URI contains DOMAIN.COM, it was probably just redirected, dont do it again!
RewriteCond %{REQUEST_URI} !^%{HTTP_HOST}
// Re-write http://domain.com/method to http://domain.com/domain.com/method
RewriteRule (.*) http://%{HTTP_HOST}/%{HTTP_HOST}/$1 [L]

Related

Custom URL in apache using get parameters

I'm trying to figure out how to rewrite urls using apache webserver and php.
The url below is the real nonrewritten url:
http://localhost:1337/rewritetest/index.php?id=12
And I want to reach it by
http://localhost:1337/rewritetest/index/12
My indexfile looks like this:
<?php
echo $_GET['id'];
?>
Is this possible? The "new" url doesn't include any parameter names so I guess I have to use an order of parameters instead but I dont know how to reach them in that case.
Below is as far I've come with my rewrite:
RewriteCond %{QUERY_STRING} id=([-a-zA-Z0-9_+]+)
RewriteRule ^/?index.php$ %1? [R=301,L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?id=$1 [L]
Anyone have an idea of what I'm doing wrong?
it's located in the same folder as index.php
So, given the .htaccess file is located at /rewritetest/.htaccess (as opposed to the document root ie. /.htaccess) then...
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?id=$1 [L]
If you request a URL of the form /rewritetest/index/12 then the above RewriteRule pattern won't actually match anything. It tries to match "index/12", but your pattern does not contain a slash so will fail. (Is the + inside the character class intentional?)
Try something like the following instead:
RewriteRule ^(index)/(\d+)$ $1.php?id=$2 [L]
This obviously specifically matches "index" in the URL. If you are always rewriting to index.php then you don't really need "index" in the URL - unless this means something different? This also assumes that the valuue of the id parameter consists only of digits.
To rewrite the more general .../<controller>/26 to .../<controller>.php?id=26 (as mentioned comments) then try something like:
RewriteRule ^([\w-]+)/(\d+)$ $1.php?id=$2 [L]
In per-directory .htaccess files the slash prefix is omitted on the URL-path that is matched by the RewriteRule pattern, so /? is not required. The above pattern also matches something for for the id, not anything. So, /index/ would not match.
If this is a new site then the "redirect" (from /index.php?id=12 back to /index/12) is not necessarily required. That's only really required if you are changing the URL structure on an existing site where old URLs already have inbound links and are indexed by search engines. In which case you could do something like the following before the internal rewrite:
RewriteBase /rewritetest/
RewriteRule %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^id=(\d+)
RewriteRule ^(index)\.php$ $1/%1 [R,L]
Or, for a more generic .../<controller>/26 to .../<controller>.php?id=26 (as above) then change the RewriteRule to:
RewriteRule ^([\w-]+)\.php$ $1/%1 [R,L]
The additional check against the REDIRECT_STATUS environment variable is to prevent a rewrite loop after having rewritten the URL to /index.php?id=12 earlier.

How to remove Controller and method URL in codeigniter

This is my current URL http://www.example.com/agent/index/$id,
and I want to change this http://www.example.com/demo_name(by agent name)
Please suggest your answer.
I'm not sure if I understand. But if you want omit the "index" in the url. You need to use a .htaccess file. This is a example:
RewriteEngine on
RewriteCond $1 !^(index.php|assets|robots.txt)
RewriteRule ^(.*)$ /directory/index.php/$1 [L]

Rewrite URL Parameter containing dot

I think that I am trying to achieve an impossible result.
The scenario is PURL-Mailing and I already got some URL's rewritten to fit the URL, sent to the customer.
The customer enters the site by the following domain: http://domain.com/UserName
The Variable UserName represents the GET-Variable, which equivalent to http://domain.com/index.php?user=UserName
I achieve this with the following rewrite Rules:
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?name=$1 [QSA]
#This works perfect and translates to http://domain.com/UserName
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?name=$1 [L]
#This achieves the goal but does not reflect in the URI I want:
#http://domain.com/UserName
To go further, there are also some Names containing a dot in the Name like A.Jackson that also need to be treated as UserName. As those are only 13 Name I could implement them manually. What I don't know is how I can prevent the part after the dot to be handled as a file extension. Is there a way to write a custom handle in *mod_rewrite* for those?
And if so, can anybody explain to me how?
Thanks in advance and best regards!
ok try below
RewriteCond %{REQUEST_FILENAME} !(img|anyother folders that you want to ignore|anyother folders that you want to ignore|...)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]*[\.]*[a-zA-Z0-9_-]+)[/]*$ test.php?name=$1 [L]
replace 'anyother folders that you want to ignore' with folder name that you want to ignore. Seperate each folders with '|'
You also have to provide full path to the CSS, image or any other links used in your web page when you using URL rewrite functions
Here is your fix
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]*[\.]*[a-zA-Z0-9_-]+)$ index.php?name=$1 [L]

CodeIgniter URL Routing

My current url is http://localhost/ci/ which is also set under config.php
but how do I open other pages in views without specifing index.php
http:/localhost/ci/Pages/view/login but not like this http:/localhost/ci/index.php/Pages/view/login
You should take a look at this
http://www.freewebmasterhelp.com/tutorials/htaccess/
you can do alot with .htaccess file
try putting this in your .htaccess file
RewriteEngine on
RewriteCond $1 ^(Pages|view|login|P[0-9]{2,8}) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
Use application/config/routes.php file.
Add URL Routing definition is very simple with it.
See this link for more information: http://codeigniter.com/user_guide/general/routing.html

How to remove Controller and function name from URL in CodeIgniter

I am having a serious issue with one application developed in CI.
Currently my URLs look like this
http://www.example.com/content/index/mission/
I want to remove /content/index/ from URL So, It should look something like this.
http://www.example.com/mission
I have routing and .htaccess method as well. But nothing seems to be working.
Here is my .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|images|css/js/style/system/feature_tab/robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]
I have also tried routing of CI by defining reouters in config/router.php But it was not working :(
For your specific example, you'll want to create a route (in application/config/routes.php) that maps
$route['mission/']
to
"content/index/mission"
In other words, $route['mission/'] = "content/index/mission";
See the CI documentation regarding URI routing for more info
You can go into application/config/routes.php and set your own URL routing rules. (i.e. use something totally different than Controller/Funcction). There should be an array called $route which lets you assign mappings of url => controller/function. Hope this helps.
Check out this guide, its right up you're alley:
http://codeigniter.com/user_guide/general/routing.html

Categories