Removing file extensions & redirecting to index page - php

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]

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]

unable to hide .php extension. It worked only if i typed manually in url, How to fix it with .htaccess

My problem is that when I type the URL without .php extension (like as https://example.com/register) it worked, but when I click register link it redirect to https://example.com/register.php. I am trying to edit .htaccess file so that by default when I click register link URL should be without .php extension.
My go daddy .htaccess file code is
Options +MultiViews
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try this htaccess
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
Use another if not work
RewriteEngine On
# remove the index file
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index$ http://www.example.org/$1 [R=301,L]
# remove the .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^.]+)$ $1.php [L]
# redirect from .php to less php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.example.org/$1 [R=301,L]
apply this to your htaccess file
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]
it works for all(.php or no .php)
Options +MultiViews
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You've enabled MultiViews (part of mod_negotiation) and you are also trying to append the file extension using mod_rewrite. You should do one or the other, not both. They both do essentially the same thing. MultiViews is probably "Winning", or worse, creating a conflict.
Your existing code is essentially the same as:
Options +MultiViews
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Thanks to all for contribution, and being ashamed myself not to reply your valuable efforts because my boss was asign me a different project.
working code is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Additionally also remove .php extention from
Example all anchor tag of the page.
you can check here https://doxdocs.com/

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]

Rewrite url to site.php as site

Here is my files
index.php
blog.php
about.php
I am accessing it as
localhost/index.php or localhost
localhost/blog.php
localhost/about.php
How can i rewrite .htaccess so that i can access it by
localhost/
localhost/blog
localhost/about
Update
My image are not displaying once i applied the below provided .htaccess, can any give fix this ?
Simple try this in .htaccess file :
RewriteRule ^([a-zA-Z0-9_-]+)$ $1.html
Options -MultiViews # optional Depends on Server
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
EDIT:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
In your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

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