Hiding extensions using .htaccess - php

What would be the correct method of using HTAccess to force .php to be hidden, including sub-folders. I've tried all of the other question answers.

Try with:
RewriteEngine on
# remove php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
# rewrite php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]

Related

Rewrite rule that will get rid of the .php file extension in my URL

I need a rewrite rule that will get rid of the .php file extension in my URL. I've tried the following. It doesn't work.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
According to this the following code should work for Removing PHP Extensions.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
To remove the .php extension completely , you can use the following rules :
RewriteEngine on
#1 redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} ([^.]+)\.php [NC]
RewriteRule ^ %1 [L,R]
#2 internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]

How to hide php file extension from url from all files?

I have a lot of php files, but I stuck in this that I want to hide php extension from url in for all files without the need to edit my code.
Another problem is that the website use $_GET to caught many parameters from links, headers, forms, is there a way to hide parameters from url or at least to change how it looks without effecting the website functionality.
I tried this but didnot work :
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+([^\?]+)\.php\?pid=([0-9]+)
RewriteRule ^ /%1/%2? [L,R]
RewriteCond %{THE_REQUEST} \ /+([^\?]+)\.php(\ |$)
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^\.]+)/([0-9]+)$ /$1.php?pid=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Try out below code in .htaccess file
# Turn on the rewrite engine
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Thanks.

htaccess add trailing slash if there isn't one

Below is my current htaccess file. It's set up to allow for no extensions and to 301.php and .htm to no extension.
I also need to add trailing slashes whenever there isn't one. There are plenty of topics on here answering that question but I can't seem to add it without messing something else up.
RewriteEngine On
# check to see if the request is for a PHP file and rewite to no extension:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
# redirect PHP or HTM to no extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.(php|htm?)
RewriteRule ^ /%1 [L,R=301]
Have it this way:
RewriteEngine On
# redirect PHP or HTM to no extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.(php|html?) [NC]
RewriteRule ^ /%1/ [L,R=301]
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
# check to see if the request is for a PHP file and rewite to no extension:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

Remove .php from URL - Specific case

This is my .htaccess file, everything works so far but I can't manage to remove .php extension from files, every code that I tried from other answers just threw 500 or 404 error. Please advise where and what to add. Structure of the folders is localhost/myfolder/somefile.php
Just to be clear - localhost/myfolder/ is a root for my project.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]
RewriteRule ^team-news/([0-9]+[/])?$ posts.php?p=$1&cat=Team\ News
RewriteRule ^product-news/([0-9]+[/])?$ posts.php? p=$1&cat=Product\ News
RewriteRule ^member-specials/([0-9]+[/])?$ posts.php?p=$1&cat=Member\ Specials
RewriteRule ^ambassador-blogs/([0-9]+[/])?$ posts.php?p=$1&cat=Ambassador\ Blogs
RewriteRule ^user/([0-9]+[/])?$ profile.php?id=$1
RewriteRule ^browse-all/([0-9]+[/])?$ searchall.php?p=$1
RewriteRule ^edit/([0-9]+[/])?$ edit.php?id=$1
RewriteRule ^articles/([0-9]+[/])?$ post.php?id=$1
This snippet will allow you to rewrite to remove php extensions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want your URL to have a trailing /, you can use this snippet:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Source
Removing extension, say; php or html in browser will let browser find it a little bit more effort to find the source file. if you need it so, this follows may help you:
UPDATED:
PHP:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/$ /$1.php
HTML:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)/$ /$1.html
those will remove all extensions in your files (both php and html).
Note: see if server enables mod rewrite module/extension.
You should be using two .htaccess files. The first should go in your localhost root (to redirect requests to myfolder), and the second should go into myfolder (to match up routes against your PHP files):
Root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]
myfolder .htaccess:
RewriteEngine On
RewriteBase /myfolder/
RewriteRule ^team-news/([0-9]+[/])?$ posts.php?p=$1&cat=Team\ News [L]
RewriteRule ^product-news/([0-9]+[/])?$ posts.php?p=$1&cat=Product\ News [L]
RewriteRule ^member-specials/([0-9]+[/])?$ posts.php?p=$1&cat=Member\ Specials [L]
RewriteRule ^ambassador-blogs/([0-9]+[/])?$ posts.php?p=$1&cat=Ambassador\ Blogs [L]
RewriteRule ^user/([0-9]+[/])?$ profile.php?id=$1 [L]
RewriteRule ^browse-all/([0-9]+[/])?$ searchall.php?p=$1 [L]
RewriteRule ^edit/([0-9]+[/])?$ edit.php?id=$1 [L]
RewriteRule ^articles/([0-9]+[/])?$ post.php?id=$1 [L]
Note how I have also included [L] to stop it from doing anything else once it has found a match.
Just below your 301 rule add this rule:
RewriteEngine On
RewriteBase /myfolder/
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

Good profile urls using .htaccess

I've three url types which are as follows:
http://www.mywebsite.com/myprofile.php?user_username=username
http://www.mywebsite.com/input.php?user_username=username
http://www.mywebsite.com/users.php?user_username=username
Currently, users have to type the whole address like (http://www.mywebsite.com/myprofile.php?user_username=username) to go to their profiles and the same with input and users.
What I want is if a user types http://www.mywebsite.com/profile/username, he would be automatically redirected to http://www.mywebsite.com/myprofile.php?user_username=username.
When a user types http://www.mywebsite.com/input/username, he would be redirected to
http://www.mywebsite.com/input.php?user_username=username.
When a user types http://www.mywebsite.com/username, he would be redirected to http://www.mywebsite.com/users.php?user_username=username.
I know that this can only be achieved through .htaccess. However, I've searched but with no fruitful result.
Any help please!
Update
I think that the following code is close to being correct. However, I'm not getting the correct result and the css is also getting messed a lot. Individually they are working but together they are messing up. Any help please.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /myprofile\.php\?user_username=(.*)\ HTTP
RewriteRule ^ /myprofile/%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user_username=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ myprofile.php?user_username=$1 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /user\.php\?user_username=(.*)\ HTTP
RewriteRule ^ //%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user_username=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ user.php?user_username=$1 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /input\.php\?user_username=(.*)\ HTTP
RewriteRule ^ /input/%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user_username=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ input.php?user_username=$1 [L]
Have it this way:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+input\.php\?user_username=([^\s&]+) [NC]
RewriteRule ^ /input/%1? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+myprofile\.php\?user_username=([^\s&]+) [NC]
RewriteRule ^ /profile/%1? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+user\.php\?user_username=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^input/([^/]+)/?$ input.php?user_username=$1 [NC,QSA,L]
RewriteRule ^profile/([^/]+)/?$ myprofile.php?user_username=$1 [NC,QSA,L]
RewriteRule ^([^/]+)/?$ users.php?user_username=$1 [NC,QSA,L]
Add these to the .htaccess file in your DOCUMENT_ROOT
RewriteEngine On
RewriteRule ^([^/]+)/?$ users.php?user_username=$1 [DPI,L]
RewriteRule ^input/([^/]+)/?$ input.php?user_username=$1 [DPI,L]
RewriteRule ^profile/([^/]+)/?$ myprofile.php?user_username=$1 [DPI,L]
Tested in Apache 2.2 and 2.4 :)
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

Categories