How to hide .php extension in .htaccess [duplicate] - php

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.

Related

RewriteRule in htaccess fails, but it could be my host

I read that Self Sabotage is Not asking for help so here I am.
So, I have a site...it's working great using WAMP. It's working great on my current host. But I need to switch to a new host and now it's failing. I figured it's a .htaccess issue but now I'm not sure. On my current host I have no .htaccess file and it works great. On my localhost server using WAMP I had the same thing as on my new host but I just disabled the .htaccess file, renaming it to BAD.htaccess, and the site still works great. This is why I think it's a server-side problem and I need some help. On my WAMP server in vhosts I disabled +FollowSymLinks for that "domain". On my current host I had no easy way to do that so it's just whatever they gave me, but it works.
I am currently with Ionos and have switched to GreenGeeks, who use cPanel. So far I haven't found a vhosts file to edit to remove +FollowSymLinks, if that is even the problem.
Maybe it can be accomplished with .htaccess and if so here is what I need to do. First my current .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^poems$ poems.php [R]
#RewriteRule ^poems/$ poems.php
RewriteRule ^collections$ collections/ [R]
RewriteRule ^collections/$ collections.php
RewriteRule ^poem$ poem/ [R]
RewriteRule ^poem/$ poem.php
RewriteRule ^poem/([0-9]+)/([a-zA-Z])$ poem.php?num=$1&poem=$2 [NC,L]
RewriteRule ^collection$ collection/ [R]
RewriteRule ^collection/$ collection.php
# RewriteRule ^poem/([0=9]+)$ indpoem.php?num=$1 [NC,L]
With the first two setups I can go to example.com/poems and it will redirect or rewrite to example.com/poems.php but still look like example.com/poems. Same with collections. On the new host those rewrite rules do rewrite it but the URL or URI shows example.com/poems.php, which I don't want per current SEO standards. Still, I could live with that.
However, when I get to the next level... example.com/poem/#/poem-name it fails on my new host. I do have a file called poem.php which it should rewrite to. In that file I use the following to get the # and name...
$URL = explode("/",$_SERVER['REQUEST_URI']);
So I don't have to do a _GET.
As you can see I tried to do a RewriteRule to change it from the first to example.com/poem?111&name, but that just seems silly because on WAMP I don't have to do anything. I could try rewriting it to the same URL again, but I have a feeling that won't work. And if it does it will probably be poem.php/#/name/
Any thoughts on a server config I'm missing when using cPanel. I even tried doing
Options -FollowSymLinks
in my .htaccess file with no success.
Any help would be appreciated. My WAMP and the new host have all the most recent versions of Apache and PHP.
There are a few issues here...
For the site to work without the .htaccess file at all (on your WAMP dev server and current host) then MultiViews (part of mod_negotiation) must have been enabled. It is MultiViews that "rewrites" /foo to /foo.php.
If MultiViews is enabled then your current .htaccess file is essentially overridden since the MultiViews content-negotiation occurs before your mod_rewrite directives are processed so your RewriteRule patterns fail to match.
MultiViews is disabled by default on Apache, it needs to be "explicitly" enabled. Unfortunately, some shared hosts do enable this in the server config (which causes more problems than it fixes - if you are not expecting it.)
On the new host those rewrite rules do rewrite it but the URL or URI shows example.com/poems.php.
RewriteRule ^poems$ poems.php [R]
Because MultiViews is disabled and your directives externally "redirect" /poems to /poems.php. There is no "rewrite" here. The R (redirect) flag triggers an external redirect.
RewriteRule ^poem$ poem/ [R]
RewriteRule ^poem/$ poem.php
However, for /poem you are redirecting to /poem/ (appending the trailing slash) but you have omitted the L flag so processing continues and the request is further rewritten to /poem.php. But because you have already triggered a Redirect, the request is "redirected" (not rewritten) to /poem.php, again exposing the .php.
Redirects should nearly always include the L flag. In fact, all your rules should include the L flag for optimisation and to prevent accidental conflicts.
Why are you redirecting to append the trailing slash (as if this is the preferred canonical URL)? You make no mention of this in your question text and only one of your examples includes a trailing slash on the URL as far as I can see? So, what is the preferred/canonical URL? What URL are you linking to? Incidentally, MultiViews will not append a trailing slash - so either this redirect is not required, or your site is not actually "working great" on WAMP / current host without the .htaccess file. (?) (Personally, I would not use a trailing slash.)
RewriteRule ^poem/([0-9]+)/([a-zA-Z])$ poem.php?num=$1&poem=$2 [NC,L]
:
$URL = explode("/",$_SERVER['REQUEST_URI']);
Your PHP script parses the requested URL-path (ie. $_SERVER['REQUEST_URI']), it is not referencing the query string. However, the above RewriteRule directive is rewriting to a query string - which would seem to be entirely superfluous ("silly" - as you suggest). Because you are not using the query string in your PHP script, the request still "works" when using MultiViews.
According to your PHP script, you simply need to rewrite the request to poem.php, without a query string. (Which is what MultiViews does. Although, strictly speaking, MultiViews rewrites /poem/123/name to /poem.php/123/name - passing the additional URL-path as path-info to poem.php.)
This regex only matches a single letter in the 3rd (name) path segment so it will fail to match a requested URL of the form /poem/123/name, so the request is not rewritten to poem.php and the request fails (with a 404 I suspect).
This regex also does not match a trailing slash. (So, is the trailing slash really canonical?)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
These conditions are entirely superfluous where you have put them. RewriteCond directives only apply to the first RewriteRule directive that follows.
Options -FollowSymLinks
You need FollowSymLinks for mod_rewrite to work. Don't try to disable this. FollowSymLinks is actually the default Apache setting, so you only need to explicitly enable it (ie. +FollowSymLinks) in .htaccess if it has been disabled in the server config.
Solution
Personally, I would not use a trailing slash on the canonical URLs. (This is in line with most of your examples and the regex used in your rule.)
So, bringing the above points together:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# Canonical redirect to remove trailing slash (from non-directories)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ /$1 [R,L]
# Internal rewrites to ".php"
RewriteRule ^poems$ poems.php [L]
RewriteRule ^collections$ collections.php [L]
RewriteRule ^poem(/\d+/[\w-]+)?$ poem.php [L]
RewriteRule ^collection$ collection.php [L]
On the other hand, if the canonical URL should include a trailing slash then change it accordingly:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# Canonical redirect to append trailing slash to "all" URLs
RewriteRule !/$ ${REQUEST_URI}/ [R,L]
# Internal rewrites to ".php"
RewriteRule ^poems/$ poems.php [L]
RewriteRule ^collections/$ collections.php [L]
RewriteRule ^poem/(\d+/[\w-]+/)?$ poem.php [L]
RewriteRule ^collection/$ collection.php [L]
If you have many such URLs/pages then you can make this entirely "generic" without having to explicitly name each URL/file. ie. "poems", "collections", etc.
Alternatively, you simply enable MultiViews and let mod_negotiation rewrite the URLs. However, you will not be able to canonicalise the trailing slash or validate the request before rewriting and MultiViews applies to everything, not just your .php files, so potentially creates duplicate content. If you need to do any specific rewriting, such are rewriting the query string then MultiViews is likely to conflict.
Options +MultiViews

Hide .php without htaccess [duplicate]

How am I able to hide a .php extension in an URL address, so that this address:
http://www.thesite.com/somefile.php
would look like:
http://www.thesite.com/somefile
without the use of the .htaccess file. The reason for that being because I have many directories and would want to hide the extension on all those files in every directory. I have tried to set expose_php to off, and this still fails with error 404.
I am using PHP 5.3.10 and Apache server.
Although you specifically said no, using the .htaccess file would remove the .php extension from all PHP files in all subdirectories in a site. I think that is what you are going for.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
Putting that into the .htaccess file will remove all the .php file extensions. Or you could put it directly into the webserver's configuration files.
You can achieve this with URL rewriting. If you don't want to use .htaccess, you can write the rule in your host configuration file.

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 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

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