I applied a rewrite rule to show my php files without the .php at the end of the file. This is what I have:
##Quitar extensión .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But the problem I have is that I can access that file with two ways. With or without the .php. For example: mywebsite.com/file.php and mywebsite.com/file both work but I just want the second one to work so I need to add something to my .htaccess file to redirect mywebsite.com/file.php to mywebsite.com/file or show a 404 error. But I think the first option is better.
Is it possible to combine my .htaccess code with what I want? And if so, what is needed to be added?
You can use this rule to remove .php extension
RewriteEngine on
#1)externally redirect from "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [L,R]
#2)internally map "/file" to "/file.php"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Related
I am working on a project where I decided to get rid of the .php extension from the URL of my app. I am successful using the htaccess code given below. But the problem is, the page still loads using .php extension if typed manually or loaded from the history.
My current .htaccess code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I want it to always load with www.example.com/dashboard except www.example.com/dashboard.php even if dashboard.php is manually typed.
UPDATED .htaccess code
RewriteEngine On
RewriteBase /dateapp/
RewriteRule ^([^\.]+).php$ $1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,END]
You can add redirect as first rule.
RewriteEngine On
RewriteRule ^([^\.]+).php$ /$1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,END]
You have to use END flag instead of L otherwise the redirects will end in infinite loop. This will work if the .htaccess is in your webroot folder.
In case you have .htaccess in 'folder' subfolder you will have to add RewriteBase
RewriteEngine On
RewriteBase /folder/
RewriteRule ^([^\.]+).php$ $1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,END]
You may use this code in dateapp/.htaccess:
RewriteEngine On
RewriteBase /dateapp/
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
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]
I know with .htaccess, you can request a file without the extension, like (page.php) can be requested as (page).
The following is the code in my .htaccess file to enable this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This code is only able to work when just (page) is requested. How do I make it so that by request, (page.php) is forcibly rewritten to just (page)? Is there anyway of doing this?
You can put this code in your htaccess (which has to be in root folder)
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/(.+)\.php(?:\s|\?) [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.*)/?$ /$1.php [L,QSA]
Note: i wrote this code to make it work with /some_page.php but also with /folder/subfolder/.../some_page.php
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
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