htaccess remove .php and .html from url - php

I have a problem whereby google has indexed some pages with the wrong URL.
The URL they are indexing is:
http://www.example.com/user/emp.php
and HTML URL:
http://www.example.com/login.html
I need it to redirect to:
http://www.example.com/user/emp
and HTML URL:
http://www.example.com/login
here is my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

First check, if there exists an appropriate target ending in .html or .php, then rewrite to that URL
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)$ $1.html [L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [L]

Replace it with this code in your htacess file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
And please remove .html and .php from all your links which you are calling for example.
Home
To
Home

Make sure you have the mod_rewrite module is installed, Check the output of phpinfo(); function to confirm. I think the following .htaccess is fine. Place the htaccess in the root of your application.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Read This on how to install mod_rewrite in php

# Remove .html-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)$ $1.html

Related

PHP rewriting URL to remove .php extension

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]

Removing file extensions & redirecting to index page

So I am trying to get my site to remove all file extensions so instead of https://multimap.xyz/error/id.php?=... it will just be https://multimap.xyz/error/id?=... . Simarly I should be able to do https://multimap.xyz/mail/index.php and it removes the trailing index.php to result in just https://multimap.xyz/mail/.
I'm trying to achieve this with both .php and .html file extensions and indexes and using .htaccess and this is what I have so far
php_flag display_errors off
ErrorDocument 404 https://multimap.xyz/error/id?=404
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
This works well for removing file extensions. So the .../error/id?=... works perfectly. However when trying to access https://multimap.xyz/mail/ it shows me an error page saying
The requested URL /email/.php was not found on this server.
Any help/suggestions are appreciated.
Thank you :)
You should check presence of .html and .php file before their rewrites.
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index|(\S+?))\.(?:php|html)[/\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

i have two type extension page of php first is .php 2nd .php3

I have two types of extension page of php first is .php and the second is .php3.
I am using this code which is removing .php3 type but also want to remove .php
RewriteOptions inherit
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php3 [NC,L]
i got my answer by testing, one its working.
RewriteOptions inherit
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php3 [NC,L]
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME}.php3 -f
RewriteRule ^([^\.]+)$ $1.php3 [QSA,NC,L]
Hope it helps!

Can't access index in folder when rewrite engine removes .php and .html

I have a little problem accessing index files when rewrite engine removes .php and .html...
I have an index in "public_html/user/username/index.php"
But it throws me to error page when I'm trying to access either
mydomain.com/user/username or
mydomain.com/user/username/index or
mydomain.com/user/username/index.php
When I'm trying to reach any file from folder in public_html folder let's say
public_html/user/username.php == mydomain.com/user/username <-- WORKS
What is going on?
My .htaccess :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Rewrite engine is already on and everything works perfectly...
You should check for presence of php and html files before adding those etenstions:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^.]+)/?$ $1.html [L]

Changing "index.php" Within The Address

I am developing a website in which i want to change my page home.php into page home
I tried this .htaccess code
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php page=/$1 [L,QSA]
Please help me.
Just this:
RewriteEngine On
RewriteRule ^(.*)$ $1.php [L,QSA]
Try this:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: php, htm, asp
This will work on local, but when you release your project don't forget to check if rewrite is enabled for the current server.

Categories