Yogesh Saroya
Yogesh Saroya

Reputation: 1495

how to change .php to .html using htaccess in cakephp

I am using cakephp in one of my projects. ( its not about Router::connect())

I have 2 folder into webroot. every folder content 50 folder (name with state) every state folder content related cities folder and every city folder have 5 .php file.

like

localhost/project_name/rooms/fl/tampa/abc.php  (/rooms{folder}/fl{folder}/tampa{folder}/ )
localhost/project_name/roommates/fl/tampa/xyz.php

Now i want to rewrite url like

localhost/project_name/rooms/fl/tampa/abc.html

localhost/project_name/roommates/fl/tampa/xyz.html

How can i do this via .htaccess in cakephp

Upvotes: 0

Views: 2306

Answers (5)

ChristopheBrun
ChristopheBrun

Reputation: 1207

If you want to turn a (.*).php url to (something).html, you just have to set the relevant rule in the htaccess.

RewriteRule ^(.*)\.php$ http://%{HTTP_HOST}/$1.html [L,R=301]

I guess you'd better be a bit specific since if the rule is too general, it could lead to unexpected results.

Provided the host of your site is just localhost, I'd try something like this :

RewriteRule ^project_name/rooms/(.*)\.php$ http://%{HTTP_HOST}/project_name/rooms/$1.html [L,R=301]
RewriteRule ^project_name/roommates/(.*)\.php$ http://%{HTTP_HOST}/project_name/rooms/$1.html [L,R=301]

Upvotes: 0

Jelmer
Jelmer

Reputation: 2693

Disclaimer: I understand that the OP asks for a htaccess solution, but I just want to share this as a possible answer since I don't know if the OP knows about the following method.


You don't have to use .htaccess in this case. CakePHP offers a method do this for you without having to modify the .htaccess.

echo $this->Html->link('Products', array(
    'controller' => 'products',
    'action' => 'index',
    'ext' => 'html' // <-- this is the notable part
));

And in your routes.php configure .html to be an allowed extension:

Router::parseExtensions('html');

http://book.cakephp.org/2.0/en/development/routing.html#file-extensions

Also, have a look at the following question on SO for some extra insights/tips: How can I append .html to all my URLs in cakephp?

Protip: you could extend the HtmlHelper with a custom HtmlHelper (like MyHtmlHelper) and set ext => html as the default behaviour for all links you create.

Ps. I guess (didn't test it!) this method is slower than a .htaccess method since this will be handled in PHP instead of directly in the request itself. If you know what I mean ...

Upvotes: 0

anubhava
anubhava

Reputation: 784898

Place this rule in /project_name/.htaccess:

RewriteEngine On
RewriteBase /project_name/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/project_name/$1\.php -f [NC]
RewriteRule ^(.+?)\.html$ $1.php [L,NC]

Upvotes: 0

Ryan
Ryan

Reputation: 14649

AddType application/x-httpd-php .html

Upvotes: 0

xdazz
xdazz

Reputation: 160833

You could do with:

RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php

Upvotes: 2

Related Questions