CodeIgniter: Multiple Apps, remove .php from url - .htaccess

My question may look similar to others, but it's different as you'll se:
I have 5 CI apps running with the same System Folder, as described in CI User Guide.
Now, I want some .htacces file to remove just the .php from the url, like, as example: http://example.com/appName/controller/action instead of http://example.com/appName.php/controller/action (removing the .php portion of the url)
Can I do that? I have no experience with .htaccess files. Also, I'm developing the site in Windows 7 with XAMPP, to deploy the site to a LAMP server.
Thanks in advance

Try This
RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) appName/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ appName.php/$1 [QSA,L]

check code igniter manual. It has a chapter for that purpose http://codeigniter.com/user_guide/general/urls.html
Removing the index.php file
By default, the index.php file will be included in your URLs:
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

htaccess rewrite old php files to CodeIgniter file

I'm trying to redirect old site files toward new CodeIgniter site with .htaccess.
So, my old file
www.mysite.com/news/new?id=123
have to redirect to
www.mysite.com/news/new/123
For now, my .htaccess has inside usually code to remove index.php and it works fine.
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php?/$1
But when I try the old file redirect nothing works.
I think the issue's about CodeIgniter, becouse if I make a simple redirect to an external file, like phpinfo.php it works fine, but if a try the redirect to index.php (of CodeIgniter) it doesn't load index.php but try to load other. Also does if I delete .htaccess code for index.php (see upside).
So,
RewriteRule news/new.php phpinfo.php [L] # it works
but
RewriteRule news/new.php index.php [L] # it doesn't work
Codeigniter use REQUEST_URI Apache variable that can not change with RewriteRule and without redirection. So you can use _remap function in news controller to call your custom method.
Other way that can be doing with htaccess is enabling query string.
Try
RewriteRule news/new\?id=(.*)$ index.php/news/new/$1 [L]

Modify .htaccess for codeigniter URLS

I am trying to remove the index.php from the url in codeigniter. Rewrite mod scripts are all over the place but I can't find the .htaccess file!! I tried looking in the root directory and everywhere else, but no luck.
From what I read it should be in application folder and when I go there i find the .htaccess file and all it has is deny from all. This is not the same content every one else is sharing online before modification.
Please advise.
Actually you don't find it; you create it along side with your index.php file with this contents:
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.
Reference.
The solution is as follows ( for future references)
Create the .htaccess file in the root of the codeigniter application (where you got system, application, etc folders).
Paste this 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]
Open config.php and change index = "index.php" to "". Things should work now.

.htaccess redirect if a folder name in url of CakePHP

Building on this question: .htaccess redirect if folder name in url I'd like to know how to apply the .htaccess rules regarded there, in the .htaccess of the webroot of the CakePHP app. without generating server errors or missing the CakePHP rewrite functionality.
My CakePHP version is 1.2.10 and the .htaccess of the webroot is as follows:
#app/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
AddType audio/mpeg mp3
I need this because the application has an audio folder that has sub-folders with mp3s files that the application play them using soundmanager2. I want when some one try to access a mp3 file directly or from external website to be redirected to another page, but keeping the soundmanager2 able to play the file from my website page. So suppose the following:
http://mycake.com/audio/folderX/fileY.mp3
I want any access to http://mycake.com/audio/* to be redirected to http://mycake.com/mistakeSound.mp3
In a htaccess file the order of the rules is important, if the rules and conditions apply to the same requests. If the request URLs are processed by different rules, you can place the rules in any order.
In your case, you have real files in the other question and non-existing files and directories here in this question
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
This means, you can insert the other rules before or after the CakePHP rules.
I found the solution which is depend on placing .htaccess file in the regarded audio folder. The syntax of that .htaccess should be as follows:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mycake\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mycake\.com.*$ [NC]
RewriteRule \.(gif|jpg|mp3)$ - [R=401]
ErrorDocument 401 /e401.html
When someone try to access any mp3 file via, for example, http://mycake.com/audio/folderX/fileY.mp3. A 401 error will be generated.
However, I noticed that FDM is able to access and download this file. The redirect works only for web browsers.

CakePHP folders in webroot get redirected to app/webroot/folder

In CakePHP you can access files in webroot via domain/file
For example the favicon.ico file located at app/webroot/favicon.ico can be accessed from
example.com/favicon.ico
Directories on the other hand seem to have a gotcha.
If I have a directory called blog in the app/webroot/, then as long as I try to access it like:
example.com/blog/
Then it works as expected. However if I try to access it without the trailing slash:
example.com/blog
Then it gets redirected to:
example.com/app/webroot/blog/
This is undesired. I woult rather .com/folder get redirected to .com/folder/ instead of .com/app/webroot/folder/
Is there a way to set this up?
If you installed wordpress blog and cakephp in document root, you need to replace .htaccess file script with below script:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/YOUR_WORDPRESS_DIR.*
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^$ app/webroot/ [L]
RewriteCond %{REQUEST_URI} !^/YOUR_WORDPRESS_DIR.*
RewriteCond %{REQUEST_URI} !-d
RewriteRule (.*) app/webroot/$1 [L]
What about using the Router::Connect for blog to redirect directly to the good page? Of course this is not an automatic routine but it mighthelp you.
More info : http://book.cakephp.org/view/945/Routes-Configuration
I suppose there might be a parameter in the config core files, have you already had a look there?
You could try the instructions here

RewriteCond and RewriteRule newbie

I'm taking over a website for a client that is running on a custom built CMS (that I didn't write). I don't mess with .htaccess files usually because a lot of the hosting I do is on IIS, or I used WordPress as a CMS and don't have to worry about messing with the .htaccess file. Here's the contents of the file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ framework.php?%{QUERY_STRING}&resource=$1& [L]
I get what it's doing (sending all requests through the framework.php file). The client wants a WordPress blog added to their site. I'm placing it in a /blog/ folder. The problem is that because of the rewrite rules and conditions in the .htaccess file whenever I try to go /blog/ the other CMS freaks out because it doesn't like me trying to go there. My question is how do I write a rule/cond that tells apache to send all requests made to the /blog/ folder to the /blog/ folder, but keep all other requests piped through the framework.php file like it is now? Any help is appreciated, thanks!
You should be able to tell the main .htaccess file to ignore any /blog links by adding a RewriteCond to the main rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/blog(/?)
RewriteRule ^(.*)$ framework.php?%{QUERY_STRING}&resource=$1& [L]
A /blog link won't be sent to framework.php, and should serve from the /blog directory normally. The Wordpress .htaccess file in that directory will be invoked as it normally would, and should be able to handle Wordpress links from there.

Resources