How to remove Controller and method URL in codeigniter - php

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]

Related

how to hide filename using .htaccess , create custom username on url

I want remove file name from url .
my current url is:
http://www.demo.com/user.php?name=joon
and I want to :
http://www.demo.com/joon
if I use this code
RewriteRule ^user/([a-zA-Z0-9_-]+)$ user.php?name=$1
output is
http://www.demo.com/user/joon
but I want
http://www.demo.com/joon
Try this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ /user.php?u=$1 [L]
RewriteCond is important here to avoid rewriting your existent directories to /user.php.
This will rewrite /user to /user.php?u=user .
You can achieve this using Apache URL Rewriting. You need to make changes to your .htaccess file. This blog post covers it nicely
https://expozit.wordpress.com/2012/07/25/apache-url-shortening/

Mod rewrite and dynamic URLs

i know this is a common question but i cannot figure this one out.
I have the following URL:
http://buildsanctuary.com/viewbuild.php?id=3&title=this_is_a_title&page=1
What i wish is the URL to be:
http://buildsanctuary.com/viewbuild/3/this_is_a_title/1
Normally for mod rewrites i would send the user to the link i want and let htaccess do all the work.
So in this case i have tried linking the users to the preferred URL style and rewriting the URL but to no avail.
Any help on how i should be handling this? I want to send the users to the preffered URL but then can i use htaccess to allow me to process the page and URL $_GET information in the same way as the normal dynamic URL?
I have tried the mod rewrite generators etc but nothing works.
This is what the gens have given me:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.php$ /viewbuild.php?id=$1&title=$2&page=$3 [L]
Thanks.
Fix the generator rule:
RewriteRule ^viewbuild/([^/]*)/([^/]*)/([^/]*)\.php$ /viewbuild.php?id=$1&title=$2&page=$3 [L,QSA]
However, I would do it this way:
RewriteRule ^([^/]*)/(.*) /$1.php/$2 [L]
and then map what you get in $_SERVER['PATH_INFO'] to you preference in PHP.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?id=$2&title=$3&page=$4 [L,QSA]
Should do the trick
^viewbuild/([0-9]+)/([a-z_]+)/-([0-9]+)/?$ viewbuild.php?id=$1&title=$2&page=$3 [NC,L]

How to change the url in codelginter

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]

htaccess change get variable to mysql username database

I was wondering if I'm supposed to use .htaccess to change a get variable to a value from the database (username).
so if there is
http://www.url.com/user.php?u=1
how do you conver it to
http://www.url.com/andrewliu
Thanks!
You need to change your user.php to take a username instead of a userid. Then you can use something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([a-z0-9_-]+)/?$ /user.php?u=$1 [L]
This passes the username through the u query string parameter, essentially: /user.php?u=andrewliu
Otherwise there's no way htaccess and mod_rewrite can know what the mapping is between user_id and username. Alternatively, you can write a database script and use RewriteMap to create a mapping for you:
RewriteMap usermap prg:/path/to/userscript
and in your htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([a-z0-9_-]+)/?$ /user.php?u=${usermap:$1} [L]
The last option, if you've only got, say, 5 users (or some small amount), you can make explicit rewrites:
RewriteEngine On
RewriteRule ^/?andrewliu$ /user.php?u=1 [L]
RewriteRule ^/?anotheruser$ /user.php?u=2 [L]
RewriteRule ^/?foousername$ /user.php?u=3 [L]
You need to use Mod-ReWrite. You can use this in your htaccess files but if you have access to your httpd.conf file it will prove to be quicker there.
You would ideally use the ?u=1 in your htaccess file and use that to find the the name and append the name onto the end. Otherwise you will have to search for the username and could be succesiptable to spelling mistakes etc etc.
RewriteRule ^/([0-9]+)/.*$ /user.php?u=$1 [L]
This is how Stackoverflow does it, try accessing this page without the number in the url and you will get a 404 page not found. Get the number right and what ever else you type after the forward slash will be exchanged for the correct words! This is much more convienent!
Use $GET in php to get the value of 'u'
Then use your logic to convert it to name.
Now just use redirect function to go to to that URL

Mod rewrite user URL

I'm lost here. I'm using this script to give users the opportunity to enter their username lijke this:domain/username
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ userpage.php?user=$1 [NC,L]
</IfModule>
This works fine. However, every user has pages I must link to: Video, Music, Images etc...
So I need something like:
domain/username/video
In php code it must be something like:
user.php?user=test&page=video
And one other question: What is the preferable way to link in this situation?
userpage.php?user=test&page=video
or
/test/video
And finally: Is it possible to deny the possibility to enter the url:
domain/userpage.php?user=test&page=video? Instead just always show: domain/test/video
Thanks in advance
I'm not 100% sure what you're asking? Do you need to change the rewrite rule to match the URL site.com/moonwalker/videos? You could try this:
RewriteRule ^([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Update
Just a quick note on the domain/member/videos URL structure. That could end up causing you problems in the future. For instance what if you decide to have a single page that shows all member videos? You'd probably want to URL to look something like site.com/members/videos. That's a problem, because the rewrite rule will also match that, but "members" isn't a member username.
I would probably structure my member page URLs like site.com/user/moonwalker/videos so it doesn't clash with future rewrite rules. You would change the above rewrite rule to this:
RewriteRule ^user/([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Then later on you can add a rewrite rule like:
RewriteRule ^members/(images|videos|music)/?$ allusers.php?page=$1 [NC,L]
To show all member videos.
Yes, it is possible by looking at the request line:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /userpage\.php[?\ ]
RewriteRule ^userpage\.php$ - [F]
This is necessary as the URL path could already be rewritten by another rule and thus using just RewriteRule would match those already rewritten requests too.

Categories