Rewrite .php to .aff and treat .aff as php - 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]

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 read source by RewriteRule [duplicate]

This question already has answers here:
How to show php-files as plain text in Apache
(5 answers)
Closed 8 years ago.
i want help in this problem
this command not work good can u help me
i have new folder on my website under this name
"TextPHP"
and i put this code in .htaccess
RewriteRule ^(.*) /home/myhome/public_html/$1.php [L,T=text/plain]
i want any one go to that folder can read any source from my php files
and this not work
i try H=text/plain
and i got error 500
also i try
php_value engine off
php_value engine off
not working :(
its there any other trick
and sorry about my english language so bad :p
thanks for helping
You don't specify the full path to the file in the rewrite rule, what you are specifiying is a new absolute URL or a URI relative to the web root. So if you want to make any URI go to a PHP file of teh same name, here is how you would do it:
RewriteEngine On
RewriteRule ^(.*) /$1.php [L]
No need to specify /home/myhome/public_html here.
This is a basic example however, as typically, this is much too broad of a rule, as it would prevent you from serving up images or other actual files from the web root (they would all end up with .php appended to the. So assuming this is actually what you want, you would commonly see something like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /$1.php [L]
This rule applies in cases when the URI does not match an actual filename or directory. Thus requests to /some_file.php would not be redirected to /some_file.php.php.
You can configure your webserver to show the source for *.phps files (e.g. make symlinks) with:
AddType application/x-httpd-php-source .phps
Or to disable PHP processing for that directory completely use:
RemoveHandler .php
RemoveType .php
Alternatively or additionally with:
php_flag engine off
On some setups (cgi / fastcgi) even:
Options -ExecCGI
If you wanna use mod_rewrite, then write a wrapper script for displaying them via readfile() (and some basename() filtering for security) and apply it like:
RewriteRule ^(.+\.php)$ showsrc.php?file=$1

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 to hide .php extension in .htaccess [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Remove .php extension with .htaccess
I'm trying to hide the .php file extension but for some reason can't get it to work. My latest attempt was the following:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^folder/([a-zA-Z_\-0-9]+)/?$ /folder/$1.php
</IfModule>
I have tried many different variances of code I have found online but still no luck. The .htaccess file is placed within the root directory.
I've used this:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless URL
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless PHP URLs
RewriteRule ^([^/.]+)$ $1.php [L]
See also: this question
The other option for using PHP scripts sans extension is
Options +MultiViews
Or even just following in the directories .htaccess:
DefaultType application/x-httpd-php
The latter allows having all filenames without extension script being treated as PHP scripts. While MultiViews makes the webserver look for alternatives, when just the basename is provided (there's a performance hit with that however).
1) Are you sure mod_rewrite module is enabled? Check phpinfo()
2) Your above rule assumes the URL starts with "folder". Is this correct? Did you acutally want to have folder in the URL? This would match a URL like:
/folder/thing -> /folder/thing.php
If you actually want
/thing -> /folder/thing.php
You need to drop the folder from the match expression.
I usually use this to route request to page without php (but yours should work which leads me to think that mod_rewrite may not be enabled):
RewriteRule ^([^/\.]+)/?$ $1.php [L,QSA]
3) Assuming you are declaring your rules in an .htaccess file, does your installation allow for setting Options (AllowOverride) overrides in .htaccess files? Some shared hosts do not.
When the server finds an .htaccess file (as specified by
AccessFileName) it needs to know which directives declared in that
file can override earlier access information.

Categories