Replace .php with .html for all urls in files directory - php

How can I Replace .php with .html ?
I have all files with .php extension in my files directory
/files
foo.php
bar.php
I want browser to show them as
http://example.com/files/foo.html
http://example.com/files/bar.html
Is this possible by .htaccss?

Add this to your files/.htaccess
RewriteEngine on
RewriteBase /files/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /files/$1.php [NC,L]
this rewriteRule will redirect all .html requests to .php meaning that if you enter www.example.com/files/foo.htmlthen it will be internally redirected to www.example.com/files/foo.php and your browser will remain at www.example.com/files/foo.html

You need to use URL Rewrite on your .htaccess file, there is a post with the same question and is solved already here :
Rewrite

Try this
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]

Related

.htaccess mod_rewrite php extension and get querystring

I have the following in my .htaccess which basically removes .php extensions from url and redirects user to the url without the .php extension. However, if i have a querystring for lets say account.php, it will be account?user=john&id=12, I would like my htacess code below to be abe to let convert the querystring to forwardslashes.
Any ideas how I can do this? Thanks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
Create a .htaccess file with the code below
RewriteEngine on
RewriteRule product-categoryid-(.*)-productid-(.*)\.htm$ Your Root URl?categoryid=$1&productid=$2
Your Root URl
Root URL means like wwww.google.com/

Give param before filename with .htaccess and serve to php

I have very poor knowledge about .htaccess
Goal:
Param can be set before index like: www.example.com/param/index
. Param also can not be set (www.example.com/index).
In php i need get this param something like $_GET['param']
Orginal file structure are: www.example.com/index.php
Note. Index.php can by any file
Now i have
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
How to remove .php extension at filename end. How to continue this?
www.example.com can by any http, https, non-www, www domain
If I interpret the question correctly, you can use these rules in your root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^([^/]+)/?$ $1.php?param=en [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/$2\.php -f [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ $2.php?param=$1 [L,QSA]

After modifying my .htaccess file to hide the .php extension from the URL, some directory items are unable to be found

Recently, I got some help modifying my .htaccess file to hide the .php extension in the url. After I got it working, a few bugs occurred with my site. For whatever reason, any php page in a sub directory returns a 404 error. For example:
mywebsite.com/test/test.php (this 404's)
but
mywebsite.com/test/test.html (this works just fine)
Since this only started occurring after my .htaccess file was modified, I am assuming the problem lies there. The file contains as follows:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mywebsite.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /.+?\.php [NC]
RewriteRule ^(.+?)\.php$ /$1 [R=301,L,NE]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
ErrorDocument 404 /404.php
DirectoryIndex index.php
If there is any more information I can provide to help solve this, please let me know!
Thank you for all the help.
Try these rules in your root .htaccess:
ErrorDocument 404 /404.php
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=301,L,NE]
# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /.+?\.php [NC]
RewriteRule ^(.+?)\.php$ /$1 [R=301,L,NE]
# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
If you are intended to hide your PHP project structure to be accessed by public as directory tree ... you should consider adding a .htaccess file in the root of your project with just 1 line
Add the following line to your .htaccess file.
Options -Indexes

Prevent loading pages with .php file extension (only load without it)

I have a rewrite condition in my .htaccess file which removes the need for .php file extension
RewriteEngine On
RewriteRule ^([^.]+)$ $1.php
so http://site.com/blog opens http://site.com/blog.php
but if old users type /blog.php it will also load the page
is there a way to prevent or redirect pages with .php or any other file extention to the one without it?
i mean if user entered /blog.php or /blog.asp it should either fail to load or redirect to /blog (without extention)
A better way to accomplish this would be to only rewrite if a .php by that name exists. Otherwise throw 404 for the original URL. The second set of rules would take care of removing the extension and avoiding the redirect loop.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{THE_REQUEST} ^(?:GET|POST)\ /.*\.php\ HTTP.*$ [NC]
RewriteRule ^(.*)\.php$ $1 [R=301,L]
RewriteEngine On
RewriteRule ^([^.]+)$ $1.php [L]
RewriteRule ^(.*?)\.php$ $1 [R=301,L]
You can use the rule
RewriteRule ^(.+)\.php$ $1 [R=301,L]
This would cause apache to send back a redirect to the browser which would update it's URL to the one stripped from the extension. But make sure to place this rule in front of the one the redirects to .php internally
Try this:
RewriteRule ^(.*?)\.([a-z0-9]+)$ $1 [R=301,L]

Convert .php extention to .html for all files in particular folder using htaccess

I have folder called Pages which contains .php files. I want to convert .php extention to .html for this particular folder only.I have the .htaccess code to convert the extention
RewriteRule ^([a-z0-9_]+)\.html$ /index.php$1 [NC,L]
But I wanted to make changes for the Particular folder only.
Any ideas???
The .htaccess file relates to the folder in which it placed in and to all the other subfloders.
Therefore, if you want to apply some rewrite rules only to one folder, you need to put an htaccess in that specific folder.
it will change all php files to html files like foo.php to foo.html
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} (.).php
RewriteRule ^(.*).php $1.html [R=301,L]
RewriteRule ^(.*).html $1.php [L]
You do this by specifying the folder name after the RewriteBase line in your .htaccess as shown below.
RewriteEngine On
RewriteBase /sampleFolder/
RewriteCond %{THE_REQUEST} (.).php
RewriteRule ^(.*).php $1.html [R=301,L]
RewriteRule ^(.*).html $1.php [L]
convert .php extension to .html extension in subdirectory using .htaccess file (convert .php extension to .html extension for all files in particular folder using .htaccess file)
RewriteEngine on
RewriteBase /subdirectory name
Example
RewriteEngine on
RewriteBase /gallery
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]

Categories