Rewrite html file for php with .htaccess - php

I need help with converting html with php .
So far My code is
RewriteEngine on
RewriteRule ([a-z]+).html /$1.php
This is working fine. but some real html pages have php scripts ,that doesnt run. how to fix this? thanks

You can use AddHandler
directive to change the mime type
of a file
Add the following line to your htaccess file
AddHandler application/x-httpd-php .html
Now all your html files will work as php.

Try this:
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]

Related

Rewrite .php to display .htm

I want to show a .php file as a .htm extension. This is what I thought would work, but it doesn't:
RewriteEngine On
RewriteBase /
RewriteRule ^contact\.php$ contact.htm [NC,R,L]
Any help would be greatly appreciated. Thank you.
Add this to your .htaccess file to allow PHP in HTML files:
# allow HTML files to process PHP
AddType application/x-httpd-php .html .htm
# now the rewrite can occur
RewriteEngine On
RewriteBase /
RewriteRule ^contact\.htm$ contact.php [NC,R,L]
With that in there you can rewrite the URL.
You are very close. You have got the rewrite rule parameters reversed - the first parameter is what you are listening for (content.html) and the second is what you want to load instead (contact.php). I have also removed the R flag so the server does this internally - with the R flag the server will send a command to the browser to force it redirect to the new location.
RewriteEngine On
RewriteBase /
RewriteRule ^contact\.htm$ contact.php [NC,L]

Redirect file extension (.htaccess)

I was wondering if it were possible to redirect certain ending file types to another extension. It's been done before on a site I visited, but I am unable to find out how.
For exmaple:
If my website had php files and the extension was www.example.com/testfile.php and I want it to show up on the URL as www.example.com/testfile.aspx, how would I do that?
Thanks
You could do something like this:
RewriteEngine On
RewriteRule ^(.*)\.aspx$ $1.php [L]
or if you would prefer to actually save the files as aspx on the server instead of redirecting to a PHP script, do something like:
AddHandler application/x-httpd-php .php .aspx
Use this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([^/]*)\.aspx$ $1.php [L]
</IfModule>

How to execute php files with .html extension?

i have problem with my site, when i try to convert php to html, i got this error
Not Found
and this is .htaccess
RewriteEngine on
RewriteRule ^(.*)\.php$ /ver1/$1.html [R=301,QSA,L]
all files in folder ver1
i see this post
.php url to .html url
but not work with me
i just need convert php to html and if i go to index.php
i go to index.html and make all url html
Rename your .php file to .html and add this line in your .htaccess
AddType application/x-httpd-php .html .htm
You need to add /ver1/ in both places - "^(.).php$" -> "^/ver1/(.).php$"
But that line just turns off the .php version - you never copied the line that tells it to actually serve the PHP files under the different extension (RewriteRule ^(.*).html$ $1.php)
RewriteEngine on
RewriteRule ^/ver1/(.*)\.html$ /ver1/$1.php
RewriteRule ^/ver1/(.*).php$ /ver1/$1.html [R=301,QSA,L]
The first rule will internally map .html to .php files and serve them directly to the client
The second rule will REDIRECT anything .php under /ver1/ to it's .html equivalent for SEO purposes
Edit - Warning - if you have any HTML forms that are action=POST data - you MUST update their action to point to the .html version - otherwise they will stop working (POST data is not redirected!)
Are you looking for "How do I rewrite .php to .html using .htaccess rules"????????????????
If yes, use
RewriteEngine on
RewriteRule ^(.*)\.html$ /ver1/$1.php [nc]
Try it on .htaccess:
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)\.html$ /$1.php
RewriteRule ^(.*)\.html$ $1.php [nc]

How do i render .php URL with .html extension

Hi i am using Codeigniter PHP framework.
Is there is any way to display .html extension rather than .php extension in URL?
Set your url suffix on your config.php found here
/application/config/config.php
$config['url_suffix'] = '.html'; //# line 60ish
I haven't actually tried it myself but see the section Adding a URL Suffix in this part of the user guide.
Just add following to Apache's config and it will process .html files with PHP:
AddType application/x-httpd-php .phtml
With that you can rename your .php files to .html.
Or you can use mod_rewrite to handle this on the fly
use a .htaccess file that does a url rewrite .html to the equivalent .php
if you are using codeigniter you should even be thinking of hiding any extension altogether.
If ur using APache. You can use URL Rewrite Rule.
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
As others said, creating a .htaccess file will allow you to rewrite .php to .html. The following code should do the trick:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L]
I am interested as to why you actually want to do this though. Codeigniter, by default, uses a segment based approach which is friendly and does away with query strings and file extensions. Using another htaccess rule, you can remove index.php from the url to make them even cleaner:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
So your urls can look like this: example.com/home, example.com/about etc. This is much prettier than having a file extension in the url.
More information about codeigniter URLs can be found here: http://codeigniter.com/user_guide/general/urls.html

Rewrite .php to .aff and treat .aff as php

On my website I have an affiliates/ folder.
Inside that I have showgames.php
I want to be able to access the file as www.website.com/affiliates/showgames.aff
Inside affiliates folder,I placed this .htaccess
AddType application/x-httpd-php .php .aff
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.php$ $1.aff [R=permanent]
For some reasons , it doesnt work .
You misunderstand what rewriting is.
You say you have a /affiliates/showgames.php page that you want to be accessible as /affiliates/showgames.aff. This means the second must be internally rewritten to the first when it's requested, not the other way around.
This also means that you do not need to interpret .aff files as PHP scripts. Just do this in affiliates' .htaccess:
RewriteRule ^(.*)\.aff$ $1.php [QSA]

Categories