Hiding controller and method from the URL but show only title name - php

I looked at the suggested answer(possible duplicate as stakoverflow says). It provided help but didn't solve the problem as it won't help create clean URL as per my needs. It has controller as part of the URL which I don't want to see as mentioned below.
I am developing a tutorial site in Codeigniter, and looking for a way to generate SEO friendly URL like
www.crdlabs.com/how-to-use-multidimensional-arrays
instead of
www.crdlabs.com/home/index/how-to-use-multidimensional-arrays.
Can anyone share his ideas? Thank you

First you need to hide the index.php from URL so create the .htaccess file on the root folder and add this code
.htaccess
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
Then open the routes.php file in the application/config/routes.php location
and add this
$route['how-to-use-multidimensional-arrays'] = 'home/how-to-use-multidimensional-arrays';
This will work as URL as SEO friendly

Add this to ur root .htaccess file :
RewriteRule ^(.*)$ index.php?title=$1 [NC,L,QSA]
Then in ur index.php get the title $title = $_GET['title'] and do what ever you like with it like do a query in ur database and return data based on the title !

Related

.htaccess Redirect any file in directory to index.php of same directory

I'm creating a blog, and would like to use the title of each entry as the URL.
The blog is pulling from a database and has code in a single php file to display the blog entry.
I would like any url like, domain.com/blog/this-is-the-title.html to redirect to
domain.com/blog/index.php
BUT, keep the URL in the browser bar as the original url.
EDIT...
domain.com/blog/anything-that-is-here.html
should redirect to domain.com/blog/index.php
But, still show domain.com/blog/anything-that-is-here.html in the browser address bar.
I hope this makes sense.
Hoping this is something that can be accomplished in.htaccess.
Thanks!
Rick
You are searching for the rewrite function from Apache.
Something like this should work for your case:
RewriteEngine on
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?title=$1
An URL like domain.com/blog/test-title will internally call your index.php with $_GET['title'] = "test-title".
Try using [P]
RewriteEngine on
RewriteRule ^(.*)$ / domain.com/blog/index.php [P]

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/

How to setup a permalink structure like wordpress with just postname after the domain name?

My title should be enough to make you understand that what I am actually trying to do. I just want to make a simple permalink structure like this:
example.com/post-name
I have written all the code to get user request, my code is
$permalink = trim($_SERVER['PHP_SELF'], "/");.
After that I have managed to get the user request when index.php is added in the URL like this:
example.com/index.php/post-name.
But this is not what I am trying to achieve, I just want to simple URL structure like this:
example.com/post-name.
I want a way so that I can get user request without any need of index.php in the URL.
Thanks, any help will be highly appreciated.
The answer is .htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?param=$1 [QSA,L]
See: Redirect everything to index.php

Dual URL Rewrite In PHP

Hello i am newbie so sorry in advance if i am asking a question in wrong manner
I am using CodeIgniter Framework .....i am stuck in URL rewrite .......
suppose example.com is my website.....URL rewrite is working in example.com/(here URL rewrite works)
for example
example.com/2014_12_12--->>link to page with data
example.com/2014_12_15--->> link to page with data
(Note its done with url rewrite and it is working fine)
Now i want that i modify my URL with some sort of texts....like that
example.com/2014_12_12--->>link to -->>example.com/random text from array/2014_12_12
Random text that i want to put it is just for optimization/seo purposes it has no link with data
Is it possible ? I'll appreciate :-)
Sorry for any inconvenience
Make below changes in application/config.php file
$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
Make .htacces file in your root directory using below code
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

URL Routing method for core php

I have web site in core php and I want to make my SEF URL.
In my web site for transferring control from one page to other i have used something like
header("Location: edit.php?id=12");
I read one article but couldn't get it how to implement it.
Link of article
So how i can make url to read like "mysite.com/usr/edit"
I read lot on google i got the idea that i need to do something in .htaccess but i dont know what needs to do with url in php so i dont have to change major code in site.
Thanks in advance.
You can do it with the .htaccess code given below:
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
after creating .htaccess file with above code. You will have all the stuff after the file name ex. "/usr/edit" in the GET parameter $GET['url']. After that you can do anything.
Here is the working example of my MVC framework: https://bitbucket.org/ManthanB/my-framework/
You can contact me for further information.

Categories