Rewrite .html to .php using mod_rewrite - php

I have a rewrite condition and rule which is.
RewriteCond %{DOCUMENT_ROOT}$0 !-f
RewriteRule ^[^*]+$ index.php [L]
I need to replace .html with .php in %{DOCUMENT_ROOT}$0.
The reason is, I am rewriting my url's to .html but when this file checks for an existing file it fails due to %{DOCUMENT_ROOT}$0 looking for file thefile.html,
I need it to look for thefile.php.

For rewriting .html extensions to existing .php files with the same name, try this rule instead of what you had:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ $1.php?%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [L]
What you have just rewrites any non-existent file to index.php

Solved!
RewriteCond %{DOCUMENT_ROOT}$0 !-f
RewriteRule ^(.*).html$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}$0 !-f
RewriteRule ^[^*]+$ index.php [L]
I had to use it twice to firstly, rewite all .html to .php then on the request for.php the second condition comes into play.

Related

CodeIgniter - removed index.php AND .php extension using a .htaccess

Before I get slapped with a "Duplicate Question," please note that I am inquiring as to how I can add on some mod_rewrite rules to my current .htaccess with the following:
Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
My issue is, I have files outside of the application directory that are not set up with a Controller. So, how can I flat-out remove all .php extensions with my current .htaccess, WHILST retaining the ability to remove the index.php (or, index) parameter from the URL?
EDIT: I have tried another RewriteCond and RewriteRule. CodeIgniter throws a 404, though, since it thinks it is a Controller.
According to Remove .php extension with .htaccess you should add
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
to your .htaccess BUT you should add it before the CodeIgniter rules like so:
Options +FollowSymLinks
RewriteEngine on
# Serve up non-CodeIgniter PHP files if they are requested without their extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
# CodeIgniter rewrite rules
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Since mod_rewrite processes and applies rules in order, you want non-CodeIgniter files to be tried first and then fall into the CodeIgniter rules.
I have no affiliation with this site and I am sure there are others like it but if you ever wanted to just test rewrite rules with various URLs then check out https://htaccess.madewithlove.be/
for removing index.php from URL in CodeIgniter. you must follow these steps.
1 . add the following code in .htaccess and .htaccess must be in the root folder.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
2 . change $config['index_page'] = 'index.php'; to $config['index_page'] = '';
you don't need to remove .php in CodeIgniter because that not exist.
if you want to learn! so use this code to remove .php extension
add this code to .htaccess
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

how to change url rewrite by .htacces with .php extention

my website is,
http://localhost/mywebsite/page.php?id=123
using .htaccess i changes my url like this
http://localhost/mywebsite/newpostof2016
but i want like this final url
localhost/mywebsite/newpostof2016.php
current using .htacces code is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)$ page.php?id=$1
does this work? It should add .php to all files even if they arent php files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]
N.B. there are many reasons as to why you wouldn't want this behavior. I can't think of a case where this would benefit UX
This rule should do it.
RewriteEngine On
RewriteRule ^mywebsite/newpostof2016\.php$ /mywebsite/page.php?id=123 [L]

Remove file extensions from my Php files

How can I remove .php extension from php files
htacess file
RewriteEngine On
RewriteCond
%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
any help is much appriciated, Thanks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ $1.php [NC,L]
If you go to example.com/test/ it will load example.com/test.php
For hat you should define an entry point.
Then you can rewrite your URLs to a parameter of a specified file in that case the index.php.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ index.php?url=$1 [NC,L]
Then you can filter that parameter in your script and show the correct content.
You have an URL something like:
www.example.de/test-test
Then everything is rewritten to your index.php. Other possibility if you rewrite to an existing file. You can do something like this:
RewriteRule ^(.*)/?$ $1.php [NC,L]
Then you fetch everything behind the slash and call an existing PHP file.
Remove .php extension with .htaccess

How to use mod_rewrite to redirect requests of username but rest to a frontend index?

I am trying to redirect requests of username to a script but the all rest to a specific index called frontend.php using a .htaccess and a mod_rewrite.
Unfortunately, this failed and I keep struggling:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ frontend.php [QSA,L]
RewriteRule ^/user/([^/]+)$ profile/profile.php?u=$1 [QSA,L]
Thanks for your help!
You can use:
RewriteEngine On
RewriteRule ^user/([^/]+)$ profile/profile.php?u=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ frontend.php [L]
.htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.
Have you tried to swap the last 2 lines against each other?

.htaccess redirect html to php and access to php files without .php extension

I wish to accomplish 2 things with this .htaccess file.
1) All requests to .html point to .php
eg. user goes to: http://www.mywebsite.com/contact.html
Browser loads: http://www.mywebsite.com/contact.php
2) Request to http://www.mywebsite.com/contact will load the contact.php
(This should apply to all pages not just the contact page.
Here is my .htaccess file.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
To be honest I have no idea what these are doing. I blended them together from a mismash of articles I read. Any help would be appreciated.
Try something like this. Follow the comment thread for security implications, but this is what you're asking to do
RewriteEngine On
RewriteBase /
# Redirect HTML to PHP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*).html$ $1.php [L]
# Otherwise, try PHP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ $1.php [L]
# Lastly, fallback to error page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . 404.php [L]
I need to explain something to you about htaccess file roles;
In this example, htaccess will try and match patterns, as you need in your example;
If a pattern is matched, then some action will happen;
you mark with () all the data than you need to be extracted from the url
For each rule, the first () will have the $1 id, the seccond () will have $2 and so on
if $1 is 'help' it will load the 'helpme.php' file maybe, and not the 'help.php' file; it will load the file that you want it to be loaded;
using $1, $2 ... you can pass parameters and values to the real/translated url request
in my example, i wanted to always use the index.php file, you will use whatever file you need
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)$ index.php?action=$1 [NC,L]
RewriteRule ^(member)-([0-9-]+)$ index.php?action=member&id=$2 [NC,L]
RewriteRule ^([a-zA-Z-]+)/([a-zA-Z-]+)-([0-9-]+)$ index.php?action=$1&saction=$2&sid=$3 [NC,L]
RewriteRule ^([a-zA-Z-]+)/([0-9-]+)$ index.php?action=$1&id=$2 [NC,L]
RewriteRule ^([0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/(.*).html$ index.php?action=details&id=$1&p1=$2&p2=$3&p3=$4 [NC,L]
If you are wanting all of your .html files to point to .php file a simple way of doing so would be
RewriteRule ([\w\d\-_]+)(\.html)? $1.php [L]
This allows you also to use sub-directories
contact.html would redirect to contact.php
/subdir/page.html would redirect to /subdir/page.php and so on

Categories