htaccess rewrite rule remove subdirectory and .php extension - php

I need to rewrite the url of my web and make it hide subfolder and the .php extension, like this:
original : http://192.168.100.5/project/controller/report.php?unit=1&year=2021
goal : http://192.168.100.5/project/report?unit=1&year=2021
My current rules which doesnt work
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ controller/$1.php [L]
How to achieve this? thankyou

You will need 2 .htaccess files.
site root .htaccess:
RewriteEngine On
# forward everything to controller/ if not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* controller/$0 [L]
controller/.htaccess:
RewriteEngine On
# add .php extension internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

Related

How to remove .php extension only for pages under a sub directory?

I am trying to remove .php extension for only pages under a subcategory folder of the website.
currently: example.com/blog/my-first-blog.php
what i want: example.com/blog/my-first-blog/
I have tried the following rule in .htaccess (placed in root) but it still shows .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/blog/$1.php -f
RewriteRule ^(.+?)/?$ /blog/$1.php [L]
The best way to achieve result you want is building a routing.
Firstable, you need to rewrite all traffic to your index.php:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L,QSA]
Then, you need to write router that will be parsing URL paths.
The simplest example of router is:
$url = urldecode(preg_replace('/\\?(.*)$/', '', $_SERVER['REQUEST_URI']));
if ($url == '/contact/about') {
include 'contact.php';
}
You can check how does it work in PHP frameworks to get a better solution, or use one of them.
I figured it out
I just had to create .htaccess file under the "/guide/" directory. with the following rule note RewriteBase /guide/ for making sure it only removes .php under "/blog/" folder
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Try this --
RewriteEngine on
RewriteBase /
RewriteCond %{http://www.example.in/} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.php $ http://www.example.in/$1 [R=301,L]
Try this
DirectorySlash Off
Options -MultiViews -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

Using htaccess to redirect user profiles

I am creating a social networking site. Using .htaccess I have successfully removed .php extensions from urls but I am not able to redirect domain.com/u.php?username=[username] to domain.com/u/[username]. Following is my .htaccess file:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
DirectoryIndex home.php
I have seen many examples and codes in stackoverflow but all of them redirect from domain.com/user.php?username=[username] to domain.com/[username]. But this would affect my website since I have removed .php extensions and I might end up with name clash. Thanks in advance.
You can put this code in your root htaccess
DirectoryIndex home.php
Options -MultiViews
RewriteEngine On
RewriteBase /
# redirect /u.php?username=XXX to /u/XXX
RewriteCond %{THE_REQUEST} \s/u\.php\?username=([^&\s]+)\s [NC]
RewriteRule ^ u/%1? [R=301,L]
# hide php extension (if file exists)
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/(.+)\.php(?:\?|\s) [NC]
RewriteRule ^ %1? [R=301,L]
# internally rewrite /u/XXX to /u.php?username=XXX
RewriteRule ^u/([^/]+)$ u.php?username=$1 [L]
# internally rewrite (if it exists) extensionless to php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)$ $1.php [L]

RewriteRule URL

I am having problems the .htaccess file using the RewriteRule. this is how my directory is right now example.com/pages/contact.php I want my directory to look like this example.com/contact/ Is there a way using the rewriterule to get hide the .php and the sub directory /pages/
This is the code i have right now:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^contact/$ /pages/contact.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
</IfModule>

Rewrite extension if file exists, redirect if file does not exist, with htaccess

Using .htaccess, I'd like to redirect files that do not exist to a controller page, and rewrite the extension of .php files that do exist to an .html extension. If a file exists and is an .html page, I'd like it to remain the same. Every time I try to inject the rewrite rule from .php to .html, I seem to mess up the redirect to the controller page. So I'm not sure where to go from here:
Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
ErrorDocument 404 /404.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
</IfModule>
Any help I'd be most grateful for.
Edit
I seem to have found most of the answer here (but I have to leave out the ReweriteBse or it doesn't work). The biggest issue is that now, my existing .html files don't work, it only serves my .php files with .html extensions and directs all else to the controller. Existing .html files go to my 404 page. I'd like to know how I can keep my existing .html files intact. My new code as follows:
RewriteEngine on
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
Try:
<IfModule mod_rewrite.c>
RewriteEngine on
# If a request for a php file is made, check that it's actually a php file then redirect the browser
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*?)\.php($|\ )
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*?)\.php$ /$1.html [L,R=301]
# If a request for an html file is made, check that it's a php file, and if so, serve the php file:
RewriteCond %{REQUEST_URI} ^/(.*?)\.html$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
# Everything else goes to the controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
</IfModule>
Try this (I have added a rewrite condition to avoid an infinite loop b yadding the parameter r=0 and testing if it exists) :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*&)?r=0(&.*)?$
RewriteRule ^(.*)\.php$ $1.html [L,R=301,QSA]
RewriteCond %{REQUEST_FILENAME} ^(.*)\.html$
RewriteCond %1.php -f
RewriteRule ^(.*)\.html$ $1.php?r=0 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
I'm sure there are better ways to do this, but the following works for my needs. The other answers provided didn't seem to work despite my attempts.
Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
ErrorDocument 404 /404.php
<IfModule mod_rewrite.c>
RewriteEngine on
# Check if .html file already exists -- if so, do nothing
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^.*$ - [NC,L]
# Check if .php file already exists -- if so, rewrite extension to .html
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]
# All else goes to the controller page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
</IfModule>

rewrite all the pages to page name only in my web site

I have created a php web site. Here I have implemented the clean URL in .htaccess file. I need to display mu link
mydomain/index.php to mydomain/index
So I have used
RewriteRule index index.php
But I have lot of files. So how will I write a common rule for this entire page?
E:g
RewriteRule hotels hotels.php
Thanks
try this:
#Setup
Options +FollowSymLinks
RewriteEngine On
#Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
#Your thing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

Categories