How to shorten urls in codeigniter using .htaccess? - php

I want to know if there is any way to shorten urls in a codeigniter using .htaccess. Like for example I have example.com/user/account/settings, I would like to shorten the url to example.com/settings. In the same way we remove index.php from the address bar, is there a way to remove more items from the same?

It's done by routes.php file in your CodeIgniter application folder. Documentation.
For your example it would be really easy, just add a line to the file:
$route['settings'] = 'user/account/settings';

By default, the index.php file will be included in your URLs: e.g
example.com/index.php/news/article/my_article
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP request other than those for index.php, images, and robots.txt is treated as a request for your index.php file.

Related

PHP index.php File get other pages content from subdirectory location

How to get only an index.php in the server root directory and have all linked pages like /about.php /contact.php and so on located in a subdirectory like /pages/dist in the server? If someone visits example.com/about.php it get's the content from /pages/dist/[filename.php] ?
There are two ways that I have found to do it.
Using htaccess with regex:
Using rewrite engine, you link the base url to a folder like this:
RewriteEngine On
RewriteRule ^(?!index\.php)([\w-_]+)$ pages/dist/$1.php [QSA,L]
RewriteRule ^(?!index\.php)([\w-_]+\.php)$ pages/dist/$1 [QSA,L]
The second line is for files that end without the .php tag, and the third with.
What this does is if the user inputs in the url www.example.com/test.php it will show what is on the www.example.com/page/dist/test.php, even if the user strips the php tag, but if he inputs the index.php it will stay the same.
Using htaccess without regex:
This one is just for the simplicity, but if we where not to use regex it could look like this:
RewriteEngine ON
RewriteRule "test.php" "page/dist/test.php"
RewriteRule "test2.php" "page/dist/test2.php"
RewriteRule "test3.php" "page/dist/test3.php"
...
Please correct me if I'm wrong.

rewriting .htaccess to hide real page path

I want to hide real page path using RewriteCond inside .htaccess
As i wanted to give my user download link of a perticular file. Suppose
www.mnc.com/myBook.pdf
www.mnc.com/youBook.pdf
Now this myBook and youBook is inside of public/pdfdirectory. As you can see that i tried to hide the directory from Url.
Now how i set the .htaccess rule so that when a user tried to access www.mnc.com/myBook.pdf it will automatically set the url path to www.mnc.com/pdf/myBook.pdf so that directory could be hide in browser path.
You are asking for rewrite I assume and assuming you are using rules in root dir try with below,
RewriteEngine On
RewriteRule ^([^/]+)\.pdf$ /mnc/pdf/$1.pdf [L]
Try this simple one.
For redirection of such requests like
localhost/mnc/public/youBook.pdf to
localhost/mnc/public/pdf/youBook.pdf
1. if REQUEST_URI ends with .pdf
2. Flags L,NC,END is last rewrite,non case sensitive and for ending further redirection respectively.
Note: If you don't use END flag it will case loop of rewriting.
.htaccess code: You can try this here
RewriteEngine on
Options -MultiViews
RewriteCond %{REQUEST_URI} \.pdf$
RewriteRule ^(.*)\/([\w]+\.pdf)$ $1/pdf/$2 [L,NC,END]

htaccess friendly URL rewrite rule

I have recently change my hosting and i need a htaccess rewrite rule for my files. I tried many examples but no one really works for my case. I have never been really good in htaccess and on my older hosting i didn't really need anything it just worked but here is not. Basically i want that my PHP files are without extensions and treated like a directory. So for example i have a URLs like these:
www.domain.com/file1/{id}/{nick}
So for example:
www.domain.com/myfile1/104/username
www.domain.com/myotherfile/455/nick
File1 in this case is a PHP file and {id} and {nick} are changable. I have this structure on my entire site for many other PHP files etc. So if possible i want one universal rule for all files. I tried with htaccess to remove php extenstion etc but all I got is 404 error. Also URL in browser should stay friendly without PHP extension. So in my case if i rewrite my URL manually in:
www.domain.com/file1.php/{id}/{nick} it worked but i don't want to change all the links etc on my website. So all i want is to hide PHP extension and treat PHP files as directory.
Thanks
You can use this single and generic front controller rule in site root .htaccess:
AcceptPathInfo On
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/(.*)$ $1.php/$2 [L]

.htaccess to open php template file as requested URL

I'm trying to figure out a way to load a template php file AS a requested url/filename:
I have a directory with hundreds of pages, and would like to be able dynamically generate them from a template file rather than have hundreds of files containing the same code. Ideally there would be one php file that loads content from MySQL using basename(), where every url requested from a particular directory would open /gallery/template.php AS the requested url, such as /gallery/example.html.
I feel like this can probably be done using .htaccess and mod-rewrite, but I haven't found an example of it in action. I'm trying to avoid using GET, but if there is a better way to achieve this effect, I'm open to suggestions. Thank you.
You might want to use AJAX as a method for accomplishing what you want. Are you familiar with AJAX? If you post more details about your specifics and desired outcome, perhaps we can help you further.
Review these simple AJAX examples, and see this video resource.
Take a look at $_SERVER['PATH_INFO'] on the $_REQUEST manual page. Using that, you can get something like
http://example.com/index.php/path/to/your/template
to call index.php passing along the entire /path/to/your/template as a string in $_REQUEST['PATH_INFO'].
And in your .htaccess file, you can do the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
(rewrite rules taken from here. I haven't done the mod-rewrite part myself, so I can't guarantee this will work).
This will take something like http://example.com/path/to/your/template and have the server treat it as http://example.com/index.php/path/to/your/template, making it pass everything to your index.php file.
After that, how you load your templates is up to you.
I haven't test it, but here is an example:
RewriteEngine On
RewriteRule ^gallery/.*/?$ gallery/template.php [NC,L]
String 'gallery' must be in the requested URL.
Basically, you are wanting to execute a front controller pattern. If everything in your gallery subdirectory needs to be redirected (i.e. there are no images files or subdirectories which don't need to be routed to template.php), you can do a simple RewriteRule to achieve this. Just place this in .htaccess or your apache .conf file
RewriteEngine On
RewriteRule ^/?gallery/.*\.html$ /gallery/template.php [QSA,L]

htaccess mod_rewrite help

i have a domain with a website in a folder.
i.e
http://domain.com.au/website/page.php
i only have access to .htaccess files to make the rules.
i would like my url to look like this..
http://domain.com.au/page
so in essence i want to drop the subfolder its sitting in, and the php extention.
All links in my php are structured like this though
page link
this is the same for js and css. they are all referenced from the 'website' folder.
will the rewrite mean i have to change all my links?
priority is dropping the folder from the url, not so much the php extension.
1) You could move all the files to the root web directory and run the website from there.
2) You would have to manually modify every link to remove the website directory from the beginning and then use the .htaccess file to redirect the pages back to the appropriate page. If you're going to do it this way, you might as well remove the PHP extension...
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^domain.com.au$ [NC]
RewriteRule ^(.*)/ website/$1.php [L]
You may have to modify the website/$1.php part to include your entire root directory in some cases, but that should work as long as the .htaccess file is in the root web directory. That will redirect any page in the form 'http://domain.com.au/page' to 'http://domain.com.au/website/page.php' while keeping the URL the same in the address bar.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^page\_one/(.*)/([0-9]{1,6})/?$ webroot/products/products/view/$2 [L]
</IfModule>
this htaccess code redirects all trafic from www.domain.com/page_one/anything/numeral
to the page www.domain.com/webroot/products/products/view/same numeral
for your example, something like
RewriteRule ^page?$ website/page.php$2 [L]
note, this works for "page" as a string, if you need other functions , change it
for more insight, you might want to take a look at this link :
http://www.javascriptkit.com/howto/htaccess.shtml

Categories