Redirect file extension (.htaccess) - php

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>

Related

Rewrite html file for php with .htaccess

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]

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]

For simple web application, how can you use ".main" as the URI extention in place of .php or .html ?

For simple web application, how can we use ".main" as the URI extention in place of .php or .html ? How can we change example.com/test.php to exmple.com/test.main, without actually renaming the file.
Thanks
You can use the apache extension mod_rewrite. Here's a sample of what you can do.
RewriteEngine On
RewriteRule ^(.*)\.main$ $1.php
This will take any request with the extension .main and actually serve the file with the same name but extension .php.
ModRewrite is a great solution for such things, however, to permanently have a different extension for you PHP files while they are still recognised and executed as PHP, use Apache's SetHandler module.
Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.main$ $1.php [R=301,L]
Also please see this Tutorial: An In Depth Guide to mod_rewrite for Apache http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/
In IIS, you would use the Manage Handlers script to assign the PHP FastCGI executable to .main file extensions.

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]

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