how to turn *.htm to *.php in htaccess - php

I have read more than 3 questions here in stackoverflow about this, but all of them has one of the below problems:
It redirects php to htm (not found appears, I already have php files in place)
When you type *.htm, it will be redirected to *.php (I want visitors to see *.htm not *.php)
Let me boil it down for you guys: I need to type index.htm and apache redirects that to .php but in the address bar I still want to see .htm not .php
Any help or suggestion would be appreciated.

RewriteEngine on
RewriteRule ([^/]*)\.htm$ $1.php [PT]
Please review http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule for all of the available options, including other flags you may want to include - such as QSA.
Note that the RewriteEngine on line (as well as Options FollowSymLinks - see the docs linked to above) is necessary if you don't already have it elsewhere. You may also need AllowOverride FileInfo in your primary Apache configuration to enable the use of .htaccess files.
Note that the PT flag is not strictly necessary, as it is the default if Apache determines it can fulfill the request without a redirect.
Here is another result I just found from a Google Search that shows pretty much the same thing I'm describing here: http://corz.org/serv/tricks/htaccess2.php (specifically shows some rules around the .htm -> .php translation.)

Hi it's easily done: In your .htaccess you will have a redirect like this:
RewriteRule ^(.*).htm$ $1.php [QSA]
RewriteRule ^(.*).html$ $1.php [QSA]

Related

Go to file by link

I store data in text file.
And when user enter in address bar something like
my_syte.com/aaa - (without extension)- I need to file_get_contents aaa.txt file
my_syte.com/bbb - I need to file_get_contents bbb.txt file
Please advise the most powerful way of do it. Apache server.
Thanks
On Apache servers you can use mod-rewrite in .htaccess file:
RewriteEngine on
RewriteRule ^([a-zA-Z]+)$ /$1.txt [L]
if your files can contain - or _ or numbers then use:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ /$1.txt [L]
On nginx servers it's more complicated but some of them works with .htaccess. On other servers there may be entirely different approach. It's hard to help you without more informations.
As you said it's Apache, then use examples above. Either edit or create .htaccess file on your webroot (directory which is accessed by domain). First check if it were there (could be hidden) and if it exists then only edit it (add lines at the top).
If it doesn't exist, then create one by yourself.
Can you please give us some insights about your server? Apache nginx?
In Apache, you can achieve that with url rewriting.
Enable mod_rewrite in apache
Put the following line of code in .htaccess on the same location of my_site.com/
RewriteEngine on
RewriteRule ^/foo$ /foo.txt [PT]
to make it generic
RewriteEngine on
RewriteRule ^/*$ /foo.txt [PT]
Maybe I am wrong in sytax based on your specific server configuration. You need to make the best possible regular expression for this case.

Strange issue using Mod_rewrite and $_GET variable

After reading tons of SO questions, asking friends and so one, I'm coming here with a strange issue regarding Apache mod_rewrite.
I'm trying to catch http://api.server.com/.../results.php?id=X URL though a RewriteRule.
Quite simple you'll say, I know it, my .htaccess file content is :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^results/(.*)$ results.php?id=$1
results.php is quite simple for debugging reasons, looks like
var_dump($_GET);
But this script always return array 0 { }
Shall I specify that I've already tried to clear the flags, and change the (.*) class by others, without effects.
Thanks for your help.
You will need to disable MultiViews option here:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^results/(.*)$ results.php?id=$1 [L,QSA,NC]
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
Your rewrite rule does not match the URL you are using (http://api.server.com/customer/2/results.php).
The correct URL according to your rule and setup is:
http://api.server.com/customer/2/results/123
However, you mention having placed everything in the /2/ folder. If 2 is the ID you are trying to get, it cannot work -- URL rewriting only works with non-existing paths.
Instead you should place your .htacess and results.php file in the customer folder, and use the following URL:
http://api.server.com/customer/results/2
Or change your rule and URL to:
RewriteRule ^([0-9]+)/results$ results.php?id=$1
http://api.server.com/customer/2/results

Have to type .php to open menu link

Our site had a link:
http://www.mechanismdigital.com/about
It previously worked fine.
Now it does not, it is required to manually type .php at the end to get to open. For example
http://www.mechanismdigital.com/about.php
and that works.
why is this?
any thoughts?
I do know it's only limited to this one menu.
Add this line of code to your .htaccess file...
redirect /about /about.php
Change about and about.php accordingly.
Put this in your .htaccess file:
redirect /about /about.php
My guess is that your server config used to have MultiViews enabled and now it does not. Re-enable it in your Apache server config and you should be good to go.
Options MultiViews
You should be able to add this to a .htaccess file if you can't directly edit the server or virtual host configuration.
Someone probably mis-edited the .htaccess file.
That file make possible the redirections, which allows to have the called "nice urls" or "clean urls"
Read the wikipedia article about it: https://en.wikipedia.org/wiki/Clean_URL
Your contact url: http://www.mechanismdigital.com/contact was also broken. (I see you just fix it)
Maybe you had an .htaccess but it was hidden, or the options set on a local php (If you can't alter the /etc/php, well, somehow you had configured the redirection, so is logical to assume an htaccess file or a local php.ini file)
Now, the methods the other answers provide actually work, but require to manually add such lino for each php.
With something like this, you can make it for each php file there:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

How can I tell which page serves me?

I am on a website. the URL reads something like https://somesite.com/serve
I need to tell the name of the page that is serving me.
Like index.html, index.htm, etc...
It is fundamentally impossible for you find that out.
You want to know the file that is being served. Well probably you are facing url rewrite with htaccess or other techniques. To tell which file it is probably is impossible if only you manage to get framework (if it is framework) in which the page is made. Then you can read in documentation which is the file to which the requests are aimed. Most frameworks will have one or several of these files. For example codeignighter will have only index.php, while symfony 2 will have app.php and app_dev.php (and others if you want different environments). But normaly you cant know which file serves your request if url rewrite is made.
As mentioned #Dale you cant also beleave what urls say. Because you cant stick some extension at the end for it to look as file. Sometimes you can notice .php or more often .html / .htm at the end.
I'm not entirely sure what you mean. But I'm certain you can find it with $_SERVER (Documentation can be found here)
Good luck
If you just trying to find out the page name then you can do this:
<?php
$currentFile = $_SERVER["PHP_SELF"];
$parts = explode('/', $currentFile);
echo $parts[count($parts) - 1];
?>
probably then you have a .htaccess with rewrite rules which looks something like:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Else you probably would have query string instead which probably looked something like:
index.php?c=products&m=view&id=345
(Assuming server supports .htaccess, mod_rewrite, mod_headers)
Temporarily use .htaccess RewriteRule to reveal all or matching filenames in a header.
Example tags all php file headers with filename:
<IfModule mod_rewrite.c>
RewriteEngine on
<IfModule mod_headers.c>
RewriteRule .*\.php$ - [E=FILENAME:$0]
<FilesMatch ".php$">
Header set MY-COOL-FILENAME "filename=%{FILENAME}e"
</FilesMatch>
</IfModule>
</IfModule>
Links
Setting a filename inside the header with htaccess
View HTTP headers in Google Chrome?

PHP: Hide file extension from URL using .htaccess

I want to hide file extensions from a URL like
if the current URL is
http://localhost/salsgiver/administrator/menus.php?sect=about
then the new one will be exactly
http://localhost/salsgiver/administrator/menus/sect/about
and so on, similary if the current URL is
http://localhost/salsgiver/administrator/products.php?id=1
then the new one will be exactly
http://localhost/salsgiver/administrator/products/1
Or some thing different, so that the viewer could not guess the exact URL.
I searched Google and found some matter on
http://roshanbh.com.np/2008/02/hide-php-url-rewriting-htaccess.html
and also used, but it does not work and the mod_rewrite module is also enabled in Apache. And when I create the .htaccess file to secure a folder from all using
deny from all
it works fine.
You could do this, but mod_rewrite is much easier to use if you use a single index.php which then chooses which file to open (products.php or menus.php)
For a single index file:
RewriteRule ^(.*)$ index.php?query=$1 [L]
For multiple files:
RewriteRule ^(.*?)/(.*?)/(.*?)$ $1.php?$2=$3 [L]

Categories